mirror of https://github.com/Chizi123/.emacs.d.git

Chizi123
2018-11-18 8f6f2705a38e2515b6c57fda12c5be29fb9a798f
commit | author | age
5cb5f7 1 ;;; gtea.el --- minuscule client library for the Gitea API  -*- lexical-binding: t -*-
C 2
3 ;; Copyright (C) 2016-2018  Jonas Bernoulli
4
5 ;; Author: Jonas Bernoulli <jonas@bernoul.li>
6 ;; Homepage: https://github.com/magit/ghub
7 ;; Keywords: tools
8
9 ;; This file is not part of GNU Emacs.
10
11 ;; This file is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 3, or (at your option)
14 ;; any later version.
15
16 ;; This file is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; For a copy of the GPL see https://www.gnu.org/licenses/gpl.txt.
22
23 ;;; Commentary:
24
25 ;; Gtea is a library that provides basic support for using the Gitea API
26 ;; from Emacs packages.  It abstracts access to API resources using only
27 ;; a handful of functions that are not resource-specific.
28
29 ;; This library is implemented on top of Ghub.  Unlike Ghub, Gtea does
30 ;; not support the guided creation of tokens because Gitea lacks the
31 ;; features that would be necessary to implement that.  Users have to
32 ;; create tokens through the web interface.
33
34 ;;; Code:
35
36 (require 'ghub)
37
38 (defconst gtea-default-host "localhost:3000/api/v1"
39   "The default host that is used if `gtea.host' is not set.")
40
41 ;; HEAD does not appear to be supported.
42
43 (cl-defun gtea-get (resource &optional params
44                              &key query payload headers
45                              silent unpaginate noerror reader
46                              username auth host
47                              callback errorback extra)
48   "Make a `GET' request for RESOURCE, with optional query PARAMS.
49 Like calling `ghub-request' (which see) with \"GET\" as METHOD
50 and `gitea' as FORGE."
51   (ghub-request "GET" resource params :forge 'gitea
52                 :query query :payload payload :headers headers
53                 :silent silent :unpaginate unpaginate
54                 :noerror noerror :reader reader
55                 :username username :auth auth :host host
56                 :callback callback :errorback errorback :extra extra))
57
58 (cl-defun gtea-put (resource &optional params
59                              &key query payload headers
60                              silent unpaginate noerror reader
61                              username auth host
62                              callback errorback extra)
63   "Make a `PUT' request for RESOURCE, with optional payload PARAMS.
64 Like calling `ghub-request' (which see) with \"PUT\" as METHOD
65 and `gitea' as FORGE."
66   (ghub-request "PUT" resource params :forge 'gitea
67                 :query query :payload payload :headers headers
68                 :silent silent :unpaginate unpaginate
69                 :noerror noerror :reader reader
70                 :username username :auth auth :host host
71                 :callback callback :errorback errorback :extra extra))
72
73 (cl-defun gtea-post (resource &optional params
74                               &key query payload headers
75                               silent unpaginate noerror reader
76                               username auth host
77                               callback errorback extra)
78   "Make a `POST' request for RESOURCE, with optional payload PARAMS.
79 Like calling `ghub-request' (which see) with \"POST\" as METHOD
80 and `gitea' as FORGE."
81   (ghub-request "POST" resource params :forge 'gitea
82                 :query query :payload payload :headers headers
83                 :silent silent :unpaginate unpaginate
84                 :noerror noerror :reader reader
85                 :username username :auth auth :host host
86                 :callback callback :errorback errorback :extra extra))
87
88 (cl-defun gtea-patch (resource &optional params
89                                &key query payload headers
90                                silent unpaginate noerror reader
91                                username auth host
92                                callback errorback extra)
93   "Make a `PATCH' request for RESOURCE, with optional payload PARAMS.
94 Like calling `ghub-request' (which see) with \"PATCH\" as METHOD
95 and `gitea' as FORGE."
96   (ghub-request "PATCH" resource params :forge 'gitea
97                 :query query :payload payload :headers headers
98                 :silent silent :unpaginate unpaginate
99                 :noerror noerror :reader reader
100                 :username username :auth auth :host host
101                 :callback callback :errorback errorback :extra extra))
102
103 (cl-defun gtea-delete (resource &optional params
104                                 &key query payload headers
105                                 silent unpaginate noerror reader
106                                 username auth host
107                                 callback errorback extra)
108   "Make a `DELETE' request for RESOURCE, with optional payload PARAMS.
109 Like calling `ghub-request' (which see) with \"DELETE\" as METHOD
110 and `gitea' as FORGE."
111   (ghub-request "DELETE" resource params :forge 'gitea
112                 :query query :payload payload :headers headers
113                 :silent silent :unpaginate unpaginate
114                 :noerror noerror :reader reader
115                 :username username :auth auth :host host
116                 :callback callback :errorback errorback :extra extra))
117
118 (cl-defun gtea-request (method resource &optional params
119                                &key query payload headers
120                                silent unpaginate noerror reader
121                                username auth host
122                                callback errorback extra)
123   "Make a request for RESOURCE and return the response body.
124 Like calling `ghub-request' (which see) with `gitea' as FORGE."
125   (ghub-request method resource params :forge 'gitea
126                 :query query :payload payload :headers headers
127                 :silent silent :unpaginate unpaginate
128                 :noerror noerror :reader reader
129                 :username username :auth auth :host host
130                 :callback callback :errorback errorback :extra extra))
131
132 (cl-defun gtea-repository-id (owner name &key username auth host)
133   "Return the id of the repository specified by OWNER, NAME and HOST."
134   (number-to-string
135    (cdr (assq 'id (gtea-get (format "/repos/%s/%s" owner name)
136                             nil :username username :auth auth :host host)))))
137
138 ;;; _
139 (provide 'gtea)
140 ;;; gtea.el ends here