Code Monkey home page Code Monkey logo

rest-api-assessment's Introduction

REST API Assessment

This is a simple PHP application providing a REST API.

Looking for the Laravel version of this application? Click here

Run Locally

Clone the project

  git clone https://github.com/ammarkero/rest-api-assessment.git

Go to the project directory

  cd rest-api-assessment

Install dependencies

  composer install

Start the server

  php -S localhost:8888 -t public

List of APIs

Usage/Example

The REST API to the app is described below.

Get list of Users

Request

GET /api/v1/user/all

curl \
-i \
-H 'Accept: application/json' \
http://localhost:8888/api/v1/user/all

Response

{
    "status":200,
    "message":"OK",
    "data":[
        {
        "id":1,
        "name":"Jake Smith",
        "email":"[email protected]",
        "password":"$2y$10$e\/pbdN7ji1UWp\/GheWPKJOaKF7zG14RNj2doLqP3CX7GHhkFFJGs2"
        },
        {
        "id":2,
        "name":"Donato Padberg",
        "email":"[email protected]",
        "password":"$2y$10$Ob7Rz3kUa0\/mqsQNHJKxdeFTwhDptoClk6hASn8aunp8sRyfqlkXi"
        }
    ]
}

Create a new User

Request

POST /api/v1/user/create

url \
-i -X POST \
-H 'Accept: application/json' \
-H 'Content-Type:application/json' \
-d '{"name": "Xavier", "email": "[email protected]","password":"12345678"}' \
http://localhost:8888/api/v1/user/create

Response

{
    "status": 201,
    "message": "User successfully created",
    "data": {
        "id": 3,
        "name": "Xavier",
        "email": "[email protected]",
        "password": "$2y$10$eAvte8C7yxnJk/nLqzzHN.xGIIDY81rn3CoDJCZNDAWuEDjZ1DWhu"
    }
}

Get a specific User

Request

GET /api/v1/user/single?:id

curl \
-i \
-H 'Accept: application/json' \
http://localhost:8888/api/v1/user/single?id=4

Response

{
    "status": 200,
    "message": "OK",
    "data": {
        "id": 4,
        "name": "Rebeca Terry",
        "email": "[email protected]",
        "password": "$2y$10$.b1vaE0DKJ0k/SMTfR2IIe/HZ/urLaumrXJzZk3Fev2pzZjkRUHWe"
    }
}

Update a specific User

Request

PUT /api/v1/user/update

curl \
-i -X PUT \
-H 'Accept: application/json' \
-H 'Content-Type:application/json' \
-d '{"id": "1","name": "Sara","email": "[email protected]","password": "abc1234567"} \
http://localhost:8888/api/v1/user/update

Response

{
    "status": 200,
    "message": "OK",
    "data": {
        "id": 1,
        "name": "Sara",
        "email": "[email protected]",
        "password": "$2y$10$4ajTn60a7slI6OgXQiI8OulFgmp6LOkOp8iQFVen\/y62YMSrzLDyi"
    }
}

Create a new User's role

Request

POST /api/v1/user/role/create

curl \
-i -X POST \
-H 'Accept: application/json' \
-H 'Content-Type:application/json' \
-d '{"email": "[email protected]","role_id": "1"}'\
http://localhost:8888/api/v1/user/role/create

Response

{
    "status": 200,
    "message": "OK",
    "data": [
        {
            "title": "Admin"
        }
    ]
}

Get list of User's role

Request

GET /api/v1/user/role?:id

curl \
-i \
-H 'Accept: application/json' \
http://localhost:8888/api/v1/user/role?id=1

Response

{
    "status": 200,
    "message": "OK",
    "data": [
        {
            "title": "Admin"
        }
    ]
}

Delete a specific User

Request

DELETE /api/v1/user/delete

curl \
-i -X DELETE \
-H 'Accept: application/json' \
-H 'Content-Type:application/json' \
-d '{"id": "13"}' \
http://localhost:8888/api/v1/user/delete

Response

{
    "status": 200,
    "message": "User successfully deleted",
    "data": []
}

User Login

Request JWToken and store login_timestamp value in user_logs table

Request

POST /api/v1/auth/login

curl \
-i -X POST \
-H 'Accept: application/json' \
-H 'Content-Type:application/json' \
-d '{"email": "[email protected]","password":"abc1234567"}' \
http://localhost:8888/api/v1/auth/login

Response

{
    "status": 200,
    "message": "User access token generated.",
    "data": {
        "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJsb2NhbGhvc3Q6ODg4OCIsInN1YiI6MSwiaWF0IjoxNjg3NDk1MDE5LCJleHAiOjE2ODc0OTUzMTl9.PxFcZrSWLtgQZdxtm8C6XpZkHm5URSXisOqFJ52vUz8"
    }
}

User Logout

store logout_timestamp value in user_logs table

Request

POST /api/v1/auth/logout

curl \ 
-i -X POST \
-H 'Accept: application/json' \
-H 'Content-Type:application/json' \
-H "Authorization: Bearer {token}" \
http://localhost:8888/api/v1/auth/logout

Response

{
    "status": 200,
    "message": "User logged out successfully",
    "data": []
}

Get data from external API call

Request

GET /api/v1/external-data/read

curl \
-i \
-H 'Accept: application/json' \
http://localhost:8888/api/v1/external-data/read

Response

{
    "status": 200,
    "message": "External data retrieved successfully",
    "data": [
        {
            "userId": 1,
            "id": 1,
            "title": "delectus aut autem",
            "completed": false
        },
        {
            "userId": 1,
            "id": 2,
            "title": "quis ut nam facilis et officia qui",
            "completed": false
        },
        {
            "userId": 1,
            "id": 2,

            ...

Store data from external API call

Request

POST /api/v1/external-data/read

curl \
-i -X POST \
-H 'Accept: application/json' \
-H 'Content-Type: application/json' \
http://localhost:8888/api/v1/external-data/store

Response

{
    "status": 200,
    "message": "20 external data retrieved and stored successfully",
    "data": []
}

Store Post's image

Request

POST /api/v1/post/image/store

curl \
-i -X POST \
-H 'Accept: application/json' \
-H 'Content-Type: application/json' \
-d '{"post_id": "1","image_path": "placeholder-2.jpg"}'
http://localhost:8888/api/v1/post/image/store

Response

{
    "status": 200,
    "message": "Image stored successfully",
    "data": []
}

Get Post's image(s)

Request

GET /api/v1/post/image/read?:id

curl \
-i \
-H 'Accept: application/json' \
http://localhost:8888/api/v1/post/image?id=1

Response

{
    "status": 200,
    "message": "OK",
    "data": [
        {
            "image_path": "unicorn.jpg"
        },
        {
            "image_path": "cat.jpg"
        }
    ]
}

Status Codes

Response returns the following status codes in its API:

Status Code Description
200 OK
201 CREATED
400 BAD REQUEST
404 NOT FOUND
429 TOO MANY REQUESTS
500 INTERNAL SERVER ERROR

App Configuration

// config.php

return [
  'database' => [
    'host' => 'localhost',
    'port' => 3306,
    'dbname' => 'rest_api_db',
    'charset' => 'utf8mb4'
  ],
  'services' => [
    'jwt' => [
      'secret_key' => 'your-secret-key',
      'expiry_time' => 5 * 60, // Expiry time in seconds (5 minutes)
    ]
  ]
];

Running Tests

To run tests, run the following command

composer test

or

vendor/bin/phpunit

Useful Resources

  • Locate (and pick one) and import SQL Dump into your database.
root
|
|- rest_api_db.sql
|- rest_api_db.gz
|
  • Locate and import Postman Collection to test API calls via Postman.
root
|
|- rest_api_postman_collection.json
|

rest-api-assessment's People

Contributors

ammarkero avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.