44
55namespace Setono \DAO \Client ;
66
7+ use InvalidArgumentException ;
78use Psr \Http \Client \ClientExceptionInterface ;
89use Psr \Http \Client \ClientInterface as HttpClientInterface ;
910use Psr \Http \Message \RequestFactoryInterface ;
10- use Psr \Http \Message \StreamFactoryInterface ;
1111use Safe \Exceptions \JsonException ;
1212use Safe \Exceptions \StringsException ;
1313use function Safe \json_decode ;
14- use Setono \DAO \Exception \RequestFailedException ;
14+ use Setono \DAO \Exception \ApiException ;
15+ use Setono \DAO \Exception \NotOkStatusCodeException ;
16+ use Webmozart \Assert \Assert ;
1517
1618final class Client implements ClientInterface
1719{
@@ -25,11 +27,6 @@ final class Client implements ClientInterface
2527 */
2628 private $ requestFactory ;
2729
28- /**
29- * @var StreamFactoryInterface
30- */
31- private $ streamFactory ;
32-
3330 /**
3431 * @var string
3532 */
@@ -48,47 +45,29 @@ final class Client implements ClientInterface
4845 public function __construct (
4946 HttpClientInterface $ httpClient ,
5047 RequestFactoryInterface $ requestFactory ,
51- StreamFactoryInterface $ streamFactory ,
5248 string $ customerId ,
5349 string $ password ,
5450 string $ baseUrl = 'https://api.dao.as '
5551 ) {
5652 $ this ->httpClient = $ httpClient ;
5753 $ this ->requestFactory = $ requestFactory ;
58- $ this ->streamFactory = $ streamFactory ;
5954 $ this ->customerId = $ customerId ;
6055 $ this ->password = $ password ;
6156 $ this ->baseUrl = $ baseUrl ;
6257 }
6358
6459 /**
65- * @param string $endpoint
66- * @param array $params
67- *
68- * @return array
60+ * {@inheritdoc}
6961 *
7062 * @throws ClientExceptionInterface
7163 * @throws JsonException
7264 * @throws StringsException
65+ * @throws InvalidArgumentException
7366 */
7467 public function get (string $ endpoint , array $ params = []): array
7568 {
76- return $ this ->sendRequest ('GET ' , $ endpoint , $ params );
77- }
69+ Assert::notContains ($ endpoint , '? ' , 'Do not add query parameters to the endpoint. Instead use the third argument, $params ' );
7870
79- /**
80- * @param string $method
81- * @param string $endpoint
82- * @param array $params
83- *
84- * @return array
85- *
86- * @throws ClientExceptionInterface
87- * @throws JsonException
88- * @throws StringsException
89- */
90- private function sendRequest (string $ method , string $ endpoint , array $ params = []): array
91- {
9271 $ params = array_merge ([
9372 'kundeid ' => $ this ->customerId ,
9473 'kode ' => $ this ->password ,
@@ -99,18 +78,18 @@ private function sendRequest(string $method, string $endpoint, array $params = [
9978
10079 $ url = $ this ->baseUrl .'/ ' .$ endpoint .'? ' .http_build_query ($ params , '' , '& ' , PHP_QUERY_RFC3986 );
10180
102- $ request = $ this ->requestFactory ->createRequest ($ method , $ url );
81+ $ request = $ this ->requestFactory ->createRequest (' GET ' , $ url );
10382
10483 $ response = $ this ->httpClient ->sendRequest ($ request );
10584
10685 if (200 !== $ response ->getStatusCode ()) {
107- throw new RequestFailedException ($ request , $ response , $ response ->getStatusCode ());
86+ throw new NotOkStatusCodeException ($ request , $ response , $ response ->getStatusCode ());
10887 }
10988
11089 $ data = (array ) json_decode ((string ) $ response ->getBody (), true );
11190
11291 if (isset ($ data ['status ' ]) && 'FEJL ' === $ data ['status ' ]) {
113- throw new RequestFailedException ($ request , $ response , $ response ->getStatusCode ());
92+ throw new ApiException ($ request , $ response , $ response ->getStatusCode (), $ data [ ' fejlkode ' ], $ data [ ' fejltekst ' ] );
11493 }
11594
11695 return $ data ;
0 commit comments