aaron-lin/internal-api

There is no license information available for the latest version (dev-main) of this package.

dev-main 2023-05-26 07:34 UTC

This package is auto-updated.

Last update: 2024-11-26 11:19:29 UTC


README

Introduction

This document describes the internal API of the application. The internal API is used in communication of backend. The internal API is not exposed to the public and is only used by the backend.

Authorization

The followinig API are authorized with Bearer-type tokens. Remote servers must send API key through HTTP header Authorization with Bearer-type token. The API key is generated by backend and is unique for each remote server. The API key is used to identify the remote server. Hashed API keys are stored in file system. The API key is generated by the following command:

$NewApiKey = random_bytes(40);
$HashedApiKey = crypt($newApiKey, 'salt');  //server only stores hashed API key

And the API keys storage file example is here.

1. Client Cache

1.1 Create Cache on Client

POST /iapi/cache

Remote server request server to create cache on server file system.

Request: String in HTTP Header, Parameter(s) in HTTP Body

Header

Authorization: Bearer b01fd1c05d93e77a887fa6c8c91088bb7f053fb220eaf87d51e5efa30fea9d25

Body

fromDB: user_info.intro_json
storePath: ./cache/user-intro
keys: id001,id002,id003
filenames: about-us.json,contact-us.json,privacy-policy.json

Success Response: String in HTTP Header

Status: 200 OK
[
    "/var/www/cache/user-intro/about-us.json",
    "/var/www/cache/user-intro/contact-us.json",
    "/var/www/cache/user-intro/privacy-policy.json"
]

Error Response

Status: 400 Bad Request
{
    "status": "error",
    "message": "keys and filenames count not match"
}

2. File Transfer

2.1 Download File

GET /iapi/file

Remote server download file from server.

Request: String in HTTP Header, Parameter(s) in Query String

Header

Authorization: Bearer b01fd1c05d93e77a887fa6c8c91088bb7f053fb220eaf87d51e5efa30fea9d25

Query String

https://example.com/iapi/file?filename=public/images/1.jpg

Success Response: String in HTTP Header

Status: 200 OK
Content-Type: image/jpg

Error Response

Status: 400 Bad Request
{
    "status": "error",
    "message": "keys and filenames count not match"
}

2.2 Upload File

POST /iapi/file

Remote server upload file to server.

Request: String in HTTP Header, Parameter(s) in HTTP Body

Header

Authorization: Bearer b01fd1c05d93e77a887fa6c8c91088bb7f053fb220eaf87d51e5efa30fea9d25

Body

content: (binary)
filename: ./public/images/1.jpg

Success Response: String in HTTP Header

Status: 204 No Content

Error Response

Status: 400 Bad Request
{
    "status": "error",
    "message": "keys and filenames count not match"
}