12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- <?php
- namespace DNB;
- use Symfony\Component\HttpClient\HttpClient;
- use Symfony\Contracts\HttpClient\HttpClientInterface;
- final class Client {
- private HttpClientInterface $client;
- private ClientConfig $clientConfig;
- private object $auth;
- public function __construct(ClientConfig $clientConfig) {
- $this->clientConfig = $clientConfig;
- $this->client = HttpClient::create(
- [
- 'headers' => [
- 'cache-control' => 'no-cache',
- ]
- ]
- );
- $this->auth = $this->authenticate();
- }
- private function getUrl(string $endpoint, string $path): string {
- return sprintf(
- 'https://%s.azurewebsites.net/%s', rtrim($endpoint, '/'), ltrim($path, '/')
- );
- }
- public function authenticate(): object {
- $request = $this->client->request(
- 'POST',
- $this->getUrl($this->clientConfig->get()->auth_endpoint, '/connect/token'), [
- 'body' => [
- 'grant_type' => 'client_credentials',
- 'client_id' => $this->clientConfig->get()->id,
- 'client_secret' => $this->clientConfig->get()->secret
- ]
- ]
- );
- $result = $request->toArray();
- return new class (
- $result['access_token'],
- $result['token_type']
- ) {
- private string $token;
- private string $type;
- public function __construct(
- string $token,
- string $type
- ) {
- $this->token = $token;
- $this->type = $type;
- }
- public function getHeader(): string {
- return sprintf("%s %s", $this->type, $this->token);
- }
- };
- }
- private function getAuthClient(): HttpClientInterface {
- return $this->client->withOptions([
- 'headers' => [
- 'Authorization' => $this->auth->getHeader(),
- ]
- ]);
- }
- public function orderProspectus(
- Prospectus $prospectus
- ): bool {
- $ret = $this->getAuthClient()->request(
- "POST",
- 'https://uat-process-externalpart.azurewebsites.net/api/v1/autoprospect/orderprospect', [
- 'json' => $prospectus->toPostParams(),
- ]
- );
- var_dump($ret->getContent(false));
- return false;
- }
- }
|