-
Notifications
You must be signed in to change notification settings - Fork 30
/
gitlab-http.el
115 lines (90 loc) · 3.91 KB
/
gitlab-http.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
;;; gitlab-http.el --- HTTP tools
;; Copyright (C) 2014, 2015, 2016 Nicolas Lamirault <[email protected]>
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License
;; as published by the Free Software Foundation; either version 2
;; of the License, or (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program; if not, write to the Free Software
;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
;; 02110-1301, USA.
;;; Commentary:
;;; Code:
(require 'json)
(require 'request)
(require 's)
(require 'gitlab-api)
(defun gitlab--get-rest-uri (uri)
"Retrieve the Gitlab API complete url using the API version.
`URI` is the api path."
(if (gitlab--get-host)
(s-concat (gitlab--get-host) "/api/" gitlab-api-version "/" uri)
(error (signal 'gitlab-error '("Gitlab host unknown.")))))
(defun gitlab--get-headers ()
"Return the HTTP headers for Gitlab API."
(let ((headers (list (cons "connection" "close"))))
(if (not (s-blank? gitlab-token-id))
(add-to-list 'headers (cons "PRIVATE-TOKEN" gitlab-token-id)))))
(defun gitlab--perform-get-request (uri params)
"Doc string URI PARAMS."
(let* ((response (request (gitlab--get-rest-uri uri)
:type "GET"
:headers (gitlab--get-headers)
:sync t
:params params
;;:data params
:parser 'json-read)))
response))
(defun gitlab--perform-post-request (uri params)
"Doc string URI PARAMS."
(let ((response (request (gitlab--get-rest-uri uri)
:type "POST"
:headers (gitlab--get-headers)
:sync t
:data params
:parser 'json-read)))
response))
(defun gitlab--perform-put-request (uri params)
"Doc string URI PARAMS."
(let ((response (request (gitlab--get-rest-uri uri)
:type "PUT"
:headers (gitlab--get-headers)
:sync t
:data params
:parser 'json-read)))
response))
(defun perform-gitlab-request (type uri params status-code)
"Doc string TYPE URI PARAMS STATUS-CODE."
(let ((response
(cond ((string= type "POST")
(gitlab--perform-post-request uri params))
((string= type "GET")
(gitlab--perform-get-request uri params))
((string= type "PUT")
(gitlab--perform-put-request uri params)))))
(if (= status-code (request-response-status-code response))
(request-response-data response)
(lwarn '(gitlab)
:error "HTTP %s Error %s on URI: %s"
type
(request-response-status-code response)
uri))))
;; (defmacro with-gitlab-request (uri params status-code response-data &rest body)
;; "Execute the forms in BODY using URI and PARAMS for HTTP request.
;; If HTTP response code isn't STATUS-CODE, raise a `gitlab-http-error'.
;; Otherwise set RESPONSE-DATA to HTTP response content."
;; `(let ((response
;; (gitlab--perform-get-request ,uri ,params)))
;; (if (= ,status-code (request-response-status-code response))
;; (let ((,response-data (request-response-data response)))
;; ,@body)
;; (error
;; (signal 'gitlab-http-error
;; (list (request-response-status-code response)
;; (request-response-data response)))))))
(provide 'gitlab-http)
;;; gitlab-http.el ends here