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