|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace ItkDev\F2ApiClient; |
| 4 | + |
| 5 | +use Symfony\Component\HttpClient\HttpClient; |
| 6 | +use Symfony\Component\OptionsResolver\OptionsResolver; |
| 7 | +use Symfony\Contracts\HttpClient\HttpClientInterface; |
| 8 | +use Symfony\Contracts\HttpClient\ResponseInterface; |
| 9 | + |
| 10 | +class ApiClient |
| 11 | +{ |
| 12 | + protected const string METHOD_GET = 'GET'; |
| 13 | + protected const string METHOD_POST = 'POST'; |
| 14 | + |
| 15 | + private readonly array $options; |
| 16 | + private HttpClientInterface $client; |
| 17 | + |
| 18 | + public function __construct( |
| 19 | + array $options, |
| 20 | + ) { |
| 21 | + $resolver = new OptionsResolver(); |
| 22 | + $this->configureOptions($resolver); |
| 23 | + |
| 24 | + $this->options = $resolver->resolve($options); |
| 25 | + } |
| 26 | + |
| 27 | + public function getServiceIndex(): array |
| 28 | + { |
| 29 | + $path = '/F2Rest/ServiceIndex'; |
| 30 | + $response = $this->request(self::METHOD_GET, $path); |
| 31 | + |
| 32 | + return $response->toArray(); |
| 33 | + } |
| 34 | + |
| 35 | + public function searchCases(array $query): array |
| 36 | + { |
| 37 | + // $query = []; |
| 38 | + $path = '/F2Rest/search/cases'; |
| 39 | + // @todo /F2Rest/ServiceIndex.json reports |
| 40 | + // |
| 41 | + // "http://cbrain.com/casefile/rel/case-search": { |
| 42 | + // "href": "/F2Rest/search/cases", |
| 43 | + // "title": "Case file search" |
| 44 | + // }, |
| 45 | + // |
| 46 | + // but the actual path is /F2Rest/searches/cases! |
| 47 | + $path = '/F2Rest/searches/cases'; |
| 48 | + $response = $this->request(self::METHOD_GET, $path, [ |
| 49 | + 'query' => $query, |
| 50 | + ]); |
| 51 | + |
| 52 | + return $response->toArray(); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * @return array{'access_token': string, 'token_type': string} |
| 57 | + */ |
| 58 | + protected function getAccessToken(): array |
| 59 | + { |
| 60 | + // @todo Check existing access token is not expired. |
| 61 | + |
| 62 | + $client = $this->client(); |
| 63 | + $response = $client->request(self::METHOD_POST, '/F2Rest/oauth2/token', [ |
| 64 | + 'auth_basic' => [ |
| 65 | + $this->options['api_username'], |
| 66 | + $this->options['api_secret'], |
| 67 | + ], |
| 68 | + 'headers' => [ |
| 69 | + 'accept' => 'application/json', |
| 70 | + ], |
| 71 | + 'body' => [ |
| 72 | + 'grant_type' => 'password', |
| 73 | + 'username' => $this->options['f2_username'], |
| 74 | + ], |
| 75 | + ]); |
| 76 | + |
| 77 | + $token = $response->toArray(); |
| 78 | + |
| 79 | + // @todo Store access token. |
| 80 | + |
| 81 | + return $token; |
| 82 | + } |
| 83 | + |
| 84 | + protected function request(string $method, string $path, array $options = []): ResponseInterface |
| 85 | + { |
| 86 | + $accessToken = $this->getAccessToken(); |
| 87 | + |
| 88 | + $cmd = 'curl --verbose --header "Authorization: Bearer '.$accessToken['access_token'].'" '.escapeshellarg($this->options['api_uri'].$path.'?'.http_build_query($options['query'] ?? [])); |
| 89 | + echo PHP_EOL, PHP_EOL, $cmd, PHP_EOL, PHP_EOL; |
| 90 | + |
| 91 | + $cmd .= ' --header "Accept: application/json"'; |
| 92 | + echo PHP_EOL, PHP_EOL, $cmd, PHP_EOL, PHP_EOL; |
| 93 | + |
| 94 | + return $this->client()->request($method, $path, $options + [ |
| 95 | + 'auth_bearer' => $accessToken['access_token'], |
| 96 | + ]); |
| 97 | + } |
| 98 | + |
| 99 | + protected function client(): HttpClientInterface |
| 100 | + { |
| 101 | + if (!isset($this->client)) { |
| 102 | + $this->client = HttpClient::create([ |
| 103 | + 'base_uri' => $this->options['api_uri'], |
| 104 | + ]); |
| 105 | + } |
| 106 | + |
| 107 | + return $this->client; |
| 108 | + } |
| 109 | + |
| 110 | + protected function configureOptions(OptionsResolver $resolver): void |
| 111 | + { |
| 112 | + $resolver->setRequired([ |
| 113 | + 'api_uri', |
| 114 | + 'api_username', |
| 115 | + 'api_secret', |
| 116 | + 'f2_username', |
| 117 | + ]); |
| 118 | + } |
| 119 | +} |
0 commit comments