Skip to content

Commit d1a01f8

Browse files
peterkossman591
andauthored
docs: Add guide for local API setup, update api-usage slightly (#235)
* docs: Add API/OAuth 2 setup guide Also rename api to api-usage * docs: Cleanup api-usage page, add notes about 2.0 * docs: Apply suggestions from code review Co-authored-by: Stuart Olivera <stuart@stuartolivera.com> * docs: Reword API introduction to be more friendly - Clarifies what Postman and Paw are, with links to their product pages - Notes that this process works for remote instances as well, not just localhost - Removed trailing whitespace and extra newlines - Fixed a single typo * Verbiage and HM typo fixes * Added .DS_Store to .gitignore Co-authored-by: Stuart Olivera <stuart@stuartolivera.com>
1 parent 82a7ff4 commit d1a01f8

File tree

12 files changed

+96
-24
lines changed

12 files changed

+96
-24
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,6 @@
3333
/.idea
3434

3535
.generators
36+
37+
# macOS
38+
.DS_Store

docs/api-setup.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
---
2+
id: api-setup
3+
title: Setup Postman/Paw for API Testing
4+
---
5+
6+
This page will show you how to simulate API requests with [Postman](https://www.postman.com/) or [Paw](https://paw.cloud/).
7+
8+
These programs allow you to run API requests (`GET`, `POST`, and so on) in an isolated environment, which is much easier to debug in compared to an iOS or Android app, for example.
9+
10+
In order to make requests in either Postman or Paw, you need to setup _OAuth 2 authorization_, as in HackathonManager, each request needs to be authorized first. This guide discusses how to enable OAuth 2 for these apps.
11+
12+
This guide assumes you have already setup an admin account on a local HackathonManager instance. For more details on this, see the [main README](https://github.com/codeRIT/hackathon-manager#local-development).
13+
14+
> To make API requests on a live/remote instance, simply replace `localhost` in all following commands with your HackathonManager's URL.
15+
16+
Select an app below to jump directly to its guide:
17+
18+
- [Connecting with Postman](#initial-setup-with-postman)
19+
- [Connecting with Paw](#initial-setup-with-paw)
20+
21+
## OAuth2 Notes
22+
23+
- The application only supports `Authorization Code` and `Implicit Grant` authorization methods. It's suggested to use an `implicit` grant for native apps, as they are inherently trusted.
24+
- In a real application, the callback URL should have a specific scope that's not just `http://`, i.e., `brickhack-ios://` (for BrickHack's iOS application). However Postman would really be confused with that scheme, so for local development, it's not advised.
25+
- Client credentials must be sent in the body.
26+
- Browser authorization is currently not supported, at least in Postman. Not sure why! If you discover a reason, please open an Issue on the [Hackathon Manager](https://github.com/codeRIT/hackathon-manager) repo.
27+
28+
## Connecting with Postman
29+
30+
In HackathonManager, sign in to your admin account and click "Manage". Then, at the bottom left of the sidebar, click the **Doorkeeper** tab.
31+
32+
<img src="assets/api-setup/doorkeeper.png" width="300px" />
33+
34+
On this screen, click `New Application`.
35+
36+
Name it whatever you wish. Set the Redirect URI to `http://localhost:3000/apply`, uncheck `Confidential`, and set a scope to `main`. Then, click `Submit`.
37+
38+
> Note: There is [a bug](https://github.com/doorkeeper-gem/doorkeeper/issues/1307) in the current version of doorkeeper (5.0), where if you do not put a scope, all OAuth requests fail. This scope can be anything as long as it is not empty.
39+
40+
Now, go to Postman and configure the initial request as follows:
41+
42+
<img src="assets/api-setup/postman1.png" width="500px" />
43+
44+
Then, click the `Get Access Token` button.
45+
46+
On this screen, fill out the information but with your own keys from the image above.
47+
48+
<img src="assets/api-setup/postman2.png" width="500px" />
49+
50+
You should get a popup with your hackathon's sign in screen. Type in your admin credentials and log in.
51+
52+
On the next screen, select `Authorize`:
53+
54+
<img src="assets/api-setup/postman3.png" width="500px" />
55+
56+
You should see this resulting screen. On here, select `Use Token`:
57+
58+
<img src="assets/api-setup/postman4.png" width="600px" />
59+
60+
That's it! Now you can make authorized requests at whatever permission level your account has.
61+
62+
<img src="assets/api-setup/postman5.png" />
63+
64+
## Connecting with Paw
65+
66+
The process is very similar to above. To save time, here is a screenshot of the `auth` config screen:
67+
68+
<img src="assets/api-setup/paw1.png" />
69+
70+
To reset your login, remove the dynamic fields in `Refresh Token` and `Token`. If your web login is still cached, try clearing cookies (`Paw > Web Views > Clear Cookies`) and Cache (`Paw > Web Views > Clear Cache`).

docs/api.md renamed to docs/api-usage.md

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,43 @@
11
---
2-
id: api
2+
id: api-usage
33
title: API Usage
44
---
55

6-
Almost all of the functionality exposed to users can also be accessed as an API with OAuth credentials. This includes both public- and management-facing functionality.
6+
## Introduction
77

8-
**Please note:** HackathonManager's primary audience is the web interface.
8+
Almost all of the functionality exposed to users can also be accessed as an API with OAuth credentials. This includes both public and management-facing functionality.
99

10-
While functionality may co-exist for both browser and API usage, API-style support might be a little rough around some edges. If you stumble upon something that works a little different than usual (e.g. returns HTML instead of JSON), open an issue and we can figure it out.
10+
**Please note:** HackathonManager's primary audience is the web interface. The API is currently being rewritten to support more application-based functionality, however this is still a work in progress, and not all functions will be supported on version `2.0`.
1111

12-
## Endpoints
12+
If you run into any inconsistencies, feel free to open an issue on the [hackathon-manager](https://github.com/codeRIT/hackathon-manager) repo.
1313

14-
Endpoints are shared with regular page controllers for browser-style functionality. This leverages Ruby on Rails routing (for a deep dive, see [Rails Routing from the Outside In](https://guides.rubyonrails.org/routing.html)).
14+
## Endpoints
1515

16-
For example, listing bus lists:
16+
> Note that endpoints are currently being rewritten to support more user-based access, rather than hiding most information under an admin-level restriction.
1717
18-
- User-facing page: https://apply.brickhack.io/manage/bus_lists
19-
- API endpoint: https://apply.brickhack.io/manage/bus_lists.json
18+
Endpoints are shared with regular page controllers for browser-style functionality. This leverages Ruby on Rails routing (for a deep dive, see [Rails Routing from the Outside In](https://guides.rubyonrails.org/routing.html)).
2019

2120
Because Rails follows a standard CRUD-format, these endpoints become very REST-like.
2221

2322
For example:
2423

25-
- List all tags: `GET https://apply.brickhack.io/manage/trackable_tags.json`
26-
- View specific tag: `GET https://apply.brickhack.io/manage/trackable_tags/1.json`
27-
- Create tag: `POST https://apply.brickhack.io/manage/trackable_tags.json` (with a JSON body of parameters)
28-
- Update tag: `PATCH https://apply.brickhack.io/manage/trackable_tags/1.json` (with a JSON body of parameters)
29-
- Delete tag: `DELETE https://apply.brickhack.io/manage/trackable_tags/1.json`
24+
| Action | Reuqest |
25+
|-------------------|--------------------------------------------------------------------------------------------------|
26+
| List all tags | `GET https://your-hackathon.io/manage/trackable_tags.json` |
27+
| View specific tag | `GET https://your-hackathon.io/manage/trackable_tags/1.json` |
28+
| Create tag | `POST https://your-hackathon.io/manage/trackable_tags.json` (with body parameters) |
29+
| Update tag | `PATCH https://your-hackathon.io/manage/trackable_tags/1.json` (with body parameters) |
30+
| Delete tag | `DELETE https://your-hackathon.io/manage/trackable_tags/1.json` |
3031

3132
For a full list of endpoints, run `bin/rails routes` locally. This utility, provided by Ruby on Rails, lists all possible paths you can route too (along with their respective HTTP method).
3233

33-
**Note: datatable endpoints** are highly coupled to [Datatables](https://datatables.net) functionality and are not easy to use.
34+
> Note: Datatable endpoints are highly coupled to [Datatables](https://datatables.net) functionality and are not easy to use.
35+
36+
> Note: The following endpoints are currently being rewritten to allow for less restrictions, i.e., bypassing the `/manage/`-level permission.
3437
3538
Example for questionnaire management endpoints:
3639

3740
```
38-
...
3941
Prefix Verb URI Pattern Controller#Action
4042
datatable_manage_questionnaires POST /manage/questionnaires/datatable(.:format) manage/questionnaires#datatable
4143
check_in_manage_questionnaire PATCH /manage/questionnaires/:id/check_in(.:format) manage/questionnaires#check_in
@@ -52,10 +54,9 @@ update_acc_status_manage_questionnaire PATCH /manage/questionnaires/:id/updat
5254
PUT /manage/questionnaires/:id(.:format) manage/questionnaires#update
5355
DELETE /manage/questionnaires/:id(.:format) manage/questionnaires#destroy
5456
datatable_manage_checkins POST /manage/checkins/datatable(.:format)
55-
...
5657
```
5758

58-
## Request & response
59+
## Request & Response
5960

6061
Most endpoints can operate with JSON requests & responses. Simply provide `.json` at the end of the URL to be returned JSON, and provide `Content-Type: application/json` when using POST or PATCH with a JSON body.
6162

@@ -72,13 +73,11 @@ end
7273
- This "show" endpoint is the natural mapping for something like `GET /manage/questionnaires/1.json`
7374
- Because `respond_with` is used, it will return JSON when `.json` is used in the URL
7475

75-
# Authentication
76-
77-
Authentication is implemented with OAauth 2, provided by the [Doorkeeper](https://github.com/doorkeeper-gem/doorkeeper) gem.
76+
## Authentication
7877

79-
An OAuth app should be created by an admin at https://apply.your-hackathon.com/oauth/applications. From there, you can implement a standard OAuth 2 flow.
78+
Authentication is implemented with OAauth 2, provided by the [Doorkeeper](https://github.com/doorkeeper-gem/doorkeeper) gem. For full Doorkeeper endpoints + docs, see the [Doorkeeper wiki](https://github.com/doorkeeper-gem/doorkeeper/wiki/API-endpoint-descriptions-and-examples).
8079

81-
For Doorkeeper endpoints + docs, see https://github.com/doorkeeper-gem/doorkeeper/wiki/API-endpoint-descriptions-and-examples
80+
See the [Setup Postman/Paw for API Testing](api-setup.md) page for information on using those tools for testing the API locally.
8281

8382
Once appropriate authentication credentials are retrieved, they should be provided on all API requests.
8483

docs/assets/api-setup/authorize.png

120 KB
Loading

docs/assets/api-setup/doorkeeper.png

79.4 KB
Loading

docs/assets/api-setup/paw1.png

284 KB
Loading

docs/assets/api-setup/postman1.png

118 KB
Loading

docs/assets/api-setup/postman2.png

155 KB
Loading

docs/assets/api-setup/postman3.png

445 KB
Loading

docs/assets/api-setup/postman4.png

120 KB
Loading

0 commit comments

Comments
 (0)