10000 Improve readme · Lucky-Loek/Symfony-API-Example@fd1401a · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Dec 17, 2019. It is now read-only.

Commit fd1401a

Browse files
author
Loek van der Linde
committed
Improve readme
1 parent b006afc commit fd1401a

File tree

1 file changed

+94
-31
lines changed

1 file changed

+94
-31
lines changed

README.md

Lines changed: 94 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@ A small example of how an API could be written in Symfony. This project allow a
77

88
All output is standardized so that it is easy to parse in any language on any environment.
99

10+
This branch features an implementation of [JSON Web Tokens](https://jwt.io/) for authenticating users.
11+
1012
## Features
1113

14+
- JWT Authentication
15+
- [GET token](#get-a-token)
1216
- Data retrieval
1317
- [GET all entities](#get-all-entities)
1418
- [GET entity by id](#get-entity-by-id)
@@ -20,20 +24,23 @@ All output is standardized so that it is easy to parse in any language on any en
2024

2125
## URLS
2226

23-
```
24-
// GET
27+
```bash
28+
# POST retrieve a token
29+
symfony.app/api/token
30+
31+
# GET car
2532
symfony.app/car
2633

27-
// GET by id
34+
# GET car by id
2835
symfony.app/car/{id}
2936

30-
// POST new
37+
# POST new car
3138
symfony.app/car
3239

33-
// PATCH update
40+
# PATCH update car
3441
symfony.app/car/{id}
3542

36-
// DELETE remove
43+
# DELETE remove car
3744
symfony.app/car/{id}
3845
```
3946

@@ -55,19 +62,57 @@ symfony.app/car/{id}
5562
### Testing
5663

5764
1. Run `php bin/console doc:mig:mig --env=test`
58-
2. Run `composer test`
65+
2. Run `php bin/console doc:fixtures:load --env=test`
66+
3. Run `composer test`
5967

60-
For more information which tests are run, please refer to the `"test"` section of `composer.json`
68+
For more information which tests are run, please refer to the `"test"` section of `composer.json`.
69+
You can run the given tests separately, i.e. `composer behat` or `composer phpunit`
6170

6271
## Technical Docs
6372

73+
### Error messages
74+
75+
All error messages have the same format, so they can be easily parsed in any language:
76+
77+
```JSON
78+
{
79+
"error": {
80+
"code": 401,
81+
"message": "Not privileged to request the resource."
82+
}
83+
}
84+
```
85+
86+
### GET a token
87+
88+
#### Request
89+
```bash
90+
$ curl --request POST \
91+
--url http://symfony.app/api/token \
92+
--header 'password: unsafepassword' \
93+
--header 'username: admin'
94+
```
95+
96+
#### Response
97+
```JSON
98+
{
99+
"token": "eyJhbGciOiJSUzI1NiJ9.eyJ1c2VybmFtZSI6ImFkbWluIiwiZXhwIjoxNDg0NTgyNTc4LCJpYXQiOjE0ODQ1ODE5Nzh9.t31C2jYVHWybZ2szEFwkGEzspYFyg9BlTyolnYtznnm8eFPIZI00hZPYCPFX2Ka7-gBFb3keM_2WVhfXKvreQpaFzge2HQ1lfMgVBCCUsxoiESUo6qCkna0Vb6ttv1qLyBRAqui_ijjANaAqEgO648vnIP0BMOYkjzw9-jNJNRQ25Bv4Y7bc_LGcGJQc2wGlg5sxWqMYhHwwCncBNPpdwTj9e9WULGBv0U1Hc_8I5eCrQFrCJGeQaKnEiy1GKXdRCSqwfCqEDrbXhgkBGygUbPGAYrfU8SnrtxFRI_EN92PByo2rjpy_M5gL-Md6czN5xDSxJHmswValR-I1ga1WkqEf194erD7KJmRRXUpz1HwNDWPDm1RJfzVgj0vTlW7kCKdLqGkkvaVnPuToxLhAPnp-kfdFkprtND0J8CajdiKaYVia4DwOjK4w_lbnfLMzZp6s6o7eKQ4h7_vkZAGu_DA0f6fVOuGQc5cqef_1oMqbKKrhVWL4xMg9wovpkAm_AF-iii-cjaXejArKzZ_4sKku5fc7BleSIHH0sXXLWlE_bI6ftc3AAxTl1buIOwpqrKDwlU_YfO8d9YkuZCRG-I0B8Nu0hfW6qh3jwIaqlqaAP6ZqAfAk8Sd6cQw8eqSqjhFjtSKA2J-DYn4lP2DC-0-_6ydj8sl3pB-DV7MEVVI"
100+
}
101+
```
102+
64103
### GET all entities
65-
```shell
66-
# Request
104+
105+
#### Request
106+
107+
```bash
67108
$ curl --request GET \
68-
--url http://symfony.app/car
69-
70-
# Response
109+
--url http://symfony.app/car \
110+
--header 'Authorization: Bearer {token}'
111+
```
112+
113+
#### Response
114+
115+
```JSON
71116
[
72117
{
73118
"id": 1,
@@ -86,12 +131,17 @@ $ curl --request GET \
86131
```
87132

88133
### GET entity by id
89-
```shell
90-
# Request
134+
135+
#### Request
136+
137+
```bash
91138
$ curl --request GET \
92-
--url http://symfony.app/car/1
93-
94-
# Response
139+
--url http://symfony.app/car/{id} \
140+
--header 'Authorization: Bearer {token}'
141+
```
142+
143+
#### Response
144+
```JSON
95145
{
96146
"id": 1,
97147
"brand": "Ford",
@@ -101,18 +151,23 @@ $ curl --request GET \
101151
```
102152

103153
### POST new entity
104-
```shell
105-
# Request
154+
155+
#### Request
156+
157+
```bash
106158
$ curl --request POST \
107159
--url http://symfony.app/car \
108160
--header 'content-type: application/json' \
161+
--header 'Authorization: Bearer {token}'
109162
--data '{
110163
"brand": "Ford",
111164
"name": "Mustang",
112165
"year": 1972
113166
}'
114-
115-
# Response
167+
```
168+
169+
#### Response
170+
```JSON
116171
{
117172
"id": 1,
118173
"brand": "Ford",
@@ -122,16 +177,20 @@ $ curl --request POST \
122177
```
123178

124179
### PATCH update existing entity
125-
```shell
126-
# Request
180+
181+
#### Request
182+
```bash
127183
$ curl --request PATCH \
128-
--url http://symfony.app/car/1 \
184+
--url http://symfony.app/car/{id} \
129185
--header 'content-type: application/json' \
186+
--header 'Authorization: Bearer {token} \
130187
--data '{
131188
"year": 2016
132189
}'
133-
134-
# Response
190+
```
191+
192+
#### Response
193+
```JSON
135194
{
136195
"id": 1,
137196
"brand": "Ford",
@@ -141,12 +200,16 @@ $ curl --request PATCH \
141200
```
142201
143202
### DELETE remove existing entity
144-
```shell
145-
# Request
203+
204+
#### Request
205+
```bash
146206
$ curl --request DELETE \
147-
--url http://symfony.app/car/1
207+
--url http://symfony.app/car/{id} \
208+
--header 'Authorization: Bearer {token}
209+
```
148210

149-
# Response
211+
#### Response
212+
```JSON
150213
{
151214
"message": "Car deleted"
152215
}

0 commit comments

Comments
 (0)
0