|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace ItkDev\F2ApiClient\Client; |
| 6 | + |
| 7 | +use ItkDev\F2ApiClient\Exception\RuntimeException; |
| 8 | +use ItkDev\F2ApiClient\Model\CaseFile; |
| 9 | +use Symfony\Component\HttpClient\HttpClient; |
| 10 | +use Symfony\Component\OptionsResolver\OptionsResolver; |
| 11 | +use Symfony\Contracts\HttpClient\HttpClientInterface; |
| 12 | +use Symfony\Contracts\HttpClient\ResponseInterface; |
| 13 | + |
| 14 | +class ApiClient |
| 15 | +{ |
| 16 | + protected const string METHOD_GET = 'GET'; |
| 17 | + protected const string METHOD_POST = 'POST'; |
| 18 | + |
| 19 | + private readonly array $options; |
| 20 | + private ?HttpClientInterface $client = null; |
| 21 | + |
| 22 | + public function __construct(array $options) |
| 23 | + { |
| 24 | + $resolver = new OptionsResolver(); |
| 25 | + $this->configureOptions($resolver); |
| 26 | + |
| 27 | + $this->options = $resolver->resolve($options); |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * @return array<string, array{href: string, title: string}> |
| 32 | + * @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface |
| 33 | + * @throws \Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface |
| 34 | + * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface |
| 35 | + * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface |
| 36 | + * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface |
| 37 | + */ |
| 38 | + public function getServiceIndex(): array |
| 39 | + { |
| 40 | + $path = '/F2Rest/ServiceIndex'; |
| 41 | + $response = $this->client()->request(self::METHOD_GET, $path, [ |
| 42 | + 'headers' => [ |
| 43 | + 'Accept' => 'application/json', |
| 44 | + ], |
| 45 | + ]); |
| 46 | + |
| 47 | + return $response->toArray(); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * @param array $query |
| 52 | + * @return CaseFile[] |
| 53 | + * @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface |
| 54 | + * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface |
| 55 | + * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface |
| 56 | + * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface |
| 57 | + */ |
| 58 | + public function searchCases(array $query): array |
| 59 | + { |
| 60 | + // $query = []; |
| 61 | + $path = '/F2Rest/search/cases'; |
| 62 | + // @todo /F2Rest/ServiceIndex.json reports |
| 63 | + // |
| 64 | + // "http://cbrain.com/casefile/rel/case-search": { |
| 65 | + // "href": "/F2Rest/search/cases", |
| 66 | + // "title": "Case file search" |
| 67 | + // }, |
| 68 | + // |
| 69 | + // which refers to the actual search URL: /F2Rest/searches/cases |
| 70 | + $path = '/F2Rest/searches/cases'; |
| 71 | + // $path = $this->getRequestUrl('http://cbrain.com/casefile/rel/case-search'); |
| 72 | + $response = $this->request(self::METHOD_GET, $path, [ |
| 73 | + 'query' => $query, |
| 74 | + ]); |
| 75 | + |
| 76 | + $items = []; |
| 77 | + $sxe = new \SimpleXMLElement($response->getContent()); |
| 78 | + foreach ($sxe->entry as $entry) { |
| 79 | + $items[] = new CaseFile($entry); |
| 80 | + } |
| 81 | + |
| 82 | + return $items; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * @return array{access_token: string, token_type: string} |
| 87 | + */ |
| 88 | + protected function getAccessToken(): array |
| 89 | + { |
| 90 | + // @todo Check existing access token is not expired. |
| 91 | + |
| 92 | + $client = $this->client(); |
| 93 | + $response = $client->request(self::METHOD_POST, '/F2Rest/oauth2/token', [ |
| 94 | + 'auth_basic' => [ |
| 95 | + $this->options['api_username'], |
| 96 | + $this->options['api_secret'], |
| 97 | + ], |
| 98 | + 'headers' => [ |
| 99 | + 'accept' => 'application/json', |
| 100 | + ], |
| 101 | + 'body' => [ |
| 102 | + 'grant_type' => 'password', |
| 103 | + 'username' => $this->options['f2_username'], |
| 104 | + ], |
| 105 | + ]); |
| 106 | + |
| 107 | + /** @var array{access_token: string, token_type: string} $token */ |
| 108 | + $token = $response->toArray(); |
| 109 | + |
| 110 | + // @todo Store access token. |
| 111 | + |
| 112 | + return $token; |
| 113 | + } |
| 114 | + |
| 115 | + protected function request(string $method, string $path, array $options = []): ResponseInterface |
| 116 | + { |
| 117 | + $accessToken = $this->getAccessToken(); |
| 118 | + |
| 119 | + return $this->client()->request( |
| 120 | + $method, |
| 121 | + $path, |
| 122 | + $options |
| 123 | + + [ |
| 124 | + 'auth_bearer' => $accessToken['access_token'], |
| 125 | + ], |
| 126 | + ); |
| 127 | + } |
| 128 | + |
| 129 | + protected function client(): HttpClientInterface |
| 130 | + { |
| 131 | + if (null === $this->client) { |
| 132 | + $this->client = HttpClient::create([ |
| 133 | + 'base_uri' => $this->options['api_uri'], |
| 134 | + ]); |
| 135 | + } |
| 136 | + |
| 137 | + return $this->client; |
| 138 | + } |
| 139 | + |
| 140 | + protected function configureOptions(OptionsResolver $resolver): void |
| 141 | + { |
| 142 | + $resolver->setRequired([ |
| 143 | + 'api_uri', |
| 144 | + 'api_username', |
| 145 | + 'api_secret', |
| 146 | + 'f2_username', |
| 147 | + ]); |
| 148 | + } |
| 149 | + |
| 150 | + protected function getRequestUrl(string $rel): string |
| 151 | + { |
| 152 | + // @todo Cache this! |
| 153 | + $index = $this->getServiceIndex(); |
| 154 | + |
| 155 | + $url = $index[$rel]['href'] ?? null; |
| 156 | + if (null === $url) { |
| 157 | + throw new RuntimeException(sprintf('Cannot get rel %s', $rel)); |
| 158 | + } |
| 159 | + |
| 160 | + return $url; |
| 161 | + } |
| 162 | +} |
0 commit comments