An API exercise
Run docker-compose to start local development.
$ docker-compose up
Run create_database task to create local (docker) database.
$ php tasks/create_database.php 127.0.0.1 root password
Get token from base64 encoded colon separated username and password.
base64_encode($username . ':' . $password)
Include Authorization basic token with every request.
$ Authorization: Basic dGVzdGVyOmV4ZXJjaXNl
Register user API endpoint.
Request
url: /user,
method: POST,
headers:
"Content-Type": "application/x-www-form-urlencoded"
"Authorization": "Basic dGVzdGVyOmV4ZXJjaXNl"
payload:
"email": "string"
"forename": "string"
"surname": "string"
Example
[POST] http://localhost/user
payload: { "email": "test@tester.com", "forename": "Test", "surname": "Tester" }
Failures
- Email field is required and cannot be empty
- Forename field is required and cannot be empty
- Surname field is required and cannot be empty
- Invalid email format
- Invalid forename. Only letters and white space allowed.
- Invalid surname. Only letters and white space allowed.
- User with requested email already exists
Find user API endpoint.
Request
url: /user,
method: GET,
header:
"Authorization": "Basic dGVzdGVyOmV4ZXJjaXNl"
params:
"id": "string"
"email": "string"
Examples
[GET] http://localhost/user
[GET] http://localhost/user?id=1
[GET] http://localhost/user?email=test@tester.com
Edit user API endpoint.
Request
url: /user/{id},
method: PUT,
headers:
"Content-Type": "application/x-www-form-urlencoded"
"Authorization": "Basic dGVzdGVyOmV4ZXJjaXNl"
payload:
"email": "string"
"forename": "string"
"surname": "string"
Example
[PUT] http://localhost/user/1
payload: { "email": "test@tester.com", "forename": "New", "surname": "Tester" }
Failures
- Email field is required and cannot be empty
- Forename field is required and cannot be empty
- Surname field is required and cannot be empty
- Invalid email format
- Invalid forename. Only letters and white space allowed.
- Invalid surname. Only letters and white space allowed.
- User with requested identifier not found
- User with requested email already exists
Remove user API endpoint.
Request
url: /user/{id},
method: DELETE,
header:
"Authorization": "Basic dGVzdGVyOmV4ZXJjaXNl"
Example
[DELETE] http://localhost/user/1
Failures
- User with requested identifier not found
Use vagrant as alternative for local development.
$ cd vagrantfile
$ vagrant up
Run create_database task to create local (vagrant) database.
$ php tasks/create_database.php 192.168.79.79 root password
Point to new host in database config in /config/development/settings.php
'database' => [
'host' => '192.168.79.79',
'dbname' => 'an_api_exercise',
'username' => 'root',
'password' => 'password',
],
The MIT License (MIT). Please see License File for more information.