The /api/client
endpoint offers an interface for provisioning new clients within the system.
- POST
The endpoint expects a JSON body in the following format:
{
"id": "YOUR_CLIENT_ID"
}
id
: (String) A unique identifier for the client.
If the client is successfully provisioned, the endpoint will send back a JSON object echoing the client's ID:
{
"id": "YOUR_CLIENT_ID"
}
To provision a new client with the ID "abc123", a POST request can be executed as follows:
curl -X POST http://[your-server-address]/api/client -H "Content-Type: application/json" -d '{"id": "abc123"}'
For a successful client provisioning, the expected response would be:
{
"id": "abc123"
}
- The
provision_client
function is an asynchronous operation that fetches the database instance from the application's state, and then attempts to insert a new client using the providedid
. - This function integrates with the
Database
struct to handle interactions with the SQLite database. The database connection is encapsulated in anArc<Mutex<SqliteConnection>>
to allow safe concurrent access. - The
insert_client
method in theDatabase
struct attempts to insert the client into theclients
table, ignoring any potential conflicts.
- This endpoint does not require client authentication, as indicated by the comment
// no client auth needed
. - The application uses SQLx, a popular asynchronous database library in the Rust ecosystem, for handling database operations. The
migrate!()
macro from SQLx is used to run database migrations.
The /api/tokens
endpoint facilitates the issuance of a unique token for a specified client within the system.
- POST
The endpoint expects a JSON body structured as:
{
"client_id": "SPECIFIED_CLIENT_ID"
}
client_id
: (String) This represents the unique identifier of the client for which the token should be generated.
Once a token is successfully generated, the endpoint returns a JSON object containing the token and the associated client ID:
{
"id": "GENERATED_TOKEN",
"client_id": "SPECIFIED_CLIENT_ID"
}
- The
issue_token
function is asynchronous and extracts theclient_id
from the incoming JSON payload. - It generates a unique token using the UUID v4 method.
- It then tries to insert the newly generated token, along with the client_id, into the database using the
insert_token
method of theDatabase
struct. - This function interfaces with the
Database
struct to handle interactions with the SQLite database. The database connection is encapsulated in anArc<Mutex<SqliteConnection>>
to ensure safe concurrent access. - The
insert_token
method of theDatabase
struct attempts to insert the token and associated client_id into thetokens
table, sidestepping any potential conflicts.
To request a token for a client with the ID "client123", you can execute the following POST request:
curl -X POST http://[your-server-address]/api/tokens -H "Content-Type: application/json" -d '{"client_id": "client123"}'
In the case of a successful token generation, the expected response could be:
{
"id": "1a2b3c4d-5e6f-7g8h-9i0j-1k2l3m4n5o6p",
"client_id": "client123"
}
- As with the earlier endpoint, this one does not necessitate client authentication (
// no client auth needed
).