Skip to content

Commit ee9b936

Browse files
6543jolheiserlafriks
authored andcommitted
[Docs] Add guide on howto develop API (#9587)
* draft 1 * add suggestions thanks to @bagasme @techknowlogick @davidsvantesson * http methods * use permalinks * Apply suggestions from code review Co-Authored-By: John Olheiser <[email protected]> * Apply suggestions from code review Co-Authored-By: John Olheiser <[email protected]> * code format + add to INDEX Co-authored-by: John Olheiser <[email protected]> Co-authored-by: Lauris BH <[email protected]>
1 parent bedd7b2 commit ee9b936

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

CONTRIBUTING.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
- [Code review](#code-review)
1313
- [Styleguide](#styleguide)
1414
- [Design guideline](#design-guideline)
15+
- [API v1](#api-v1)
1516
- [Developer Certificate of Origin (DCO)](#developer-certificate-of-origin-dco)
1617
- [Release Cycle](#release-cycle)
1718
- [Maintainers](#maintainers)
@@ -177,6 +178,43 @@ To maintain understandable code and avoid circular dependencies it is important
177178
- **templates:** Golang templates for generating the html output.
178179
- **vendor:** External code that Gitea depends on.
179180

181+
## API v1
182+
183+
The API is documented by [swagger](http://try.gitea.io/api/swagger) and is based on [GitHub API v3](https://developer.github.com/v3/).
184+
Thus, Gitea´s API should use the same endpoints and fields as GitHub´s API as far as possible, unless there are good reasons to deviate.
185+
If Gitea provides functionality that GitHub does not, a new endpoint can be created.
186+
If information is provided by Gitea that is not provided by the GitHub API, a new field can be used that doesn't collide with any GitHub fields.
187+
188+
Updating an existing API should not remove existing fields unless there is a really good reason to do so.
189+
The same applies to status responses. If you notice a problem, feel free to leave a comment in the code for future refactoring to APIv2 (which is currently not planned).
190+
191+
All expected results (errors, success, fail messages) should be documented
192+
([example](https://github.com/go-gitea/gitea/blob/c620eb5b2d0d874da68ebd734d3864c5224f71f7/routers/api/v1/repo/issue.go#L319-L327)).
193+
194+
All JSON input types must be defined as a struct in `models/structs/`
195+
([example](https://github.com/go-gitea/gitea/blob/c620eb5b2d0d874da68ebd734d3864c5224f71f7/modules/structs/issue.go#L76-L91))
196+
and referenced in
197+
[routers/api/v1/swagger/options.go](https://github.com/go-gitea/gitea/blob/c620eb5b2d0d874da68ebd734d3864c5224f71f7/routers/api/v1/swagger/options.go).
198+
They can then be used like the following:
199+
([example](https://github.com/go-gitea/gitea/blob/c620eb5b2d0d874da68ebd734d3864c5224f71f7/routers/api/v1/repo/issue.go#L318)).
200+
201+
All JSON responses must be defined as a struct in `models/structs/`
202+
([example](https://github.com/go-gitea/gitea/blob/c620eb5b2d0d874da68ebd734d3864c5224f71f7/modules/structs/issue.go#L36-L68))
203+
and referenced in its category in `routers/api/v1/swagger/`
204+
([example](https://github.com/go-gitea/gitea/blob/c620eb5b2d0d874da68ebd734d3864c5224f71f7/routers/api/v1/swagger/issue.go#L11-L16))
205+
They can be used like the following:
206+
([example](https://github.com/go-gitea/gitea/blob/c620eb5b2d0d874da68ebd734d3864c5224f71f7/routers/api/v1/repo/issue.go#L277-L279))
207+
208+
In general, HTTP methods are chosen as follows:
209+
* **GET** endpoints return requested object and status **OK (200)**
210+
* **DELETE** endpoints return status **No Content (204)**
211+
* **POST** endpoints return status **Created (201)**, used to **create** new objects (e.g. a User)
212+
* **PUT** endpoints return status **No Content (204)**, used to **add/assign** existing Obejcts (e.g. User) to something (e.g. Org-Team)
213+
* **PATCH** endpoints return changed object and status **OK (200)**, used to **edit/change** an existing object
214+
215+
216+
An endpoint which changes/edits an object expects all fields to be optional (except ones to identify the object, which are required).
217+
180218

181219
## Developer Certificate of Origin (DCO)
182220

0 commit comments

Comments
 (0)