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-05-26 10:15:54 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

Name Type Description
Authorization string (Required) Bearer type token,token content is API Key
Authorization: Bearer b01fd1c05d93e77a887fa6c8c91088bb7f053fb220eaf87d51e5efa30fea9d25

Body

Name Type Description
fromDB string (Required) source database table and column name
storePath string (Required) the path of directory storing caches
keys string (Required) the primary keys of caching data, splited by comma, the number of keys must be identical with the number of filenames
filenames string (Required) the filename of caches, splited by comma, the number of filenames must be identical with the number of keys
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

Name Type Description
Authorization string (Required) Bearer type token,token content is API Key
Authorization: Bearer b01fd1c05d93e77a887fa6c8c91088bb7f053fb220eaf87d51e5efa30fea9d25

Query String

Name Type Description
filename string (Required) path and filename of downloading file
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

Name Type Description
Authorization string (Required) Bearer type token,token content is API Key
Authorization: Bearer b01fd1c05d93e77a887fa6c8c91088bb7f053fb220eaf87d51e5efa30fea9d25

Body

Name Type Description
content binary (Required) uploading file
filename string (Required) path and filename of uploading file
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"
}