stefna / api-client-runtime
Base package for api clients
Requires
- php: ^8.0
- psr/http-client: ^1.0
- psr/http-factory: ^1.0
- psr/http-message: ^1.0
- psr/log: ^1.1 || ^2.0 || ^3.0
- starburst/utils: ^1.3
Requires (Dev)
- phpstan/phpstan: ^1.10
- stefna/codestyle: ^1.12.1
- stefna/http-client: ^1.0
Suggests
- kriswallsmith/buzz: For psr-18 implemenation
- nyholm/psr7: For psr-7 and psr-17 implemenations
This package is auto-updated.
Last update: 2024-12-19 16:01:56 UTC
README
Base classes to make it easier to create apis.
Used by Stefna OpenApi Generator among others.
Usage
To create an api-client you will need 2 classes
First is a ServerConfiguration
class that contains information about the api you want to connect to
Example:
class ServerConfiguration extends AbstractServerConfiguration { /** @var string[] */ protected array $serverUris = [ 'production' => 'https://api.example.com', 'staging' => 'https://staging.api.example.com', ]; protected string $selectedBaseUri = 'production'; protected SecurityScheme $securityScheme; protected SecurityValue $securityValue; public function __construct(SecurityValue $token) { $this->securityScheme = new ApiKeySecurityScheme('access-token', 'X-Api-Token', 'header'); $this->securityValue = $token; } public function getBaseUri(): string { return $this->serverUris[$this->selectedBaseUri]; } public function selectServer(string $name): void { $this->selectedBaseUri = $name; } public function getSecurityScheme(string $ref): ?SecurityScheme { return $this->securityScheme; } public function getSecurityValue(string $ref): ?SecurityValue { return $this->securityValue; } }
And the second one is the actual api-client
final class Service extends AbstractService { public function getNews(string $lang): array { $response = $this->doRequest(new \Stefna\ApiClientRuntime\Endpoint\Endpoint( 'GET', '/news/' . $lang, security: ['access-token'], )); return $this->parseJsonResponse($response); } public function sendNotification(string $to, string $from, string $text) { $response = $this->doRequest(new \Stefna\ApiClientRuntime\Endpoint\Endpoint( 'POST', '/notification', new \Stefna\ApiClientRuntime\RequestBody\JsonData([ 'to' => $to, 'from' => $from, 'text' => $text, ]), security: ['access-token'], )); return $this->parseJsonResponse($response); } public function sendPostData(array $postData): bool { $response = $this->doRequest(new \Stefna\ApiClientRuntime\Endpoint\Endpoint( 'POST', '/post-endpoint', new \Stefna\ApiClientRuntime\RequestBody\PostData($postData) security: ['access-token'], )); return $this->parseJsonResponse($response); } public static function createWithToken(string $token): self { return static::create(new ServerConfiguration(AuthSecurityValue::raw($token))); } }
Consuming api service
When consuming the api-client you can create it with 2 different methods
Simple creation
The simple way is to use the static create
method.
But for that method to work you need to have nyholm/psr7
and kriswallsmith/buzz
installed since that's the default
psr implementations we use
$service = Service::create(new ServerConfiguration(...)); // use service
Create with custom psr implementations
If you want you can provide your own Client and Request implementations
$service = new Service( new ServerConfiguration(...), new GuzzleHttp\Client(), new GuzzleHttp\Psr7\HttpFactory(), );
License
View the LICENSE file attach to this project.