11<?php
2+ /** @noinspection PhpFullyQualifiedNameUsageInspection */
23
34/**
45 * Hostinger API PHP SDK
1213
1314namespace Hostinger \Api ;
1415
15- use InvalidArgumentException ;
1616use GuzzleHttp \Client ;
1717use GuzzleHttp \ClientInterface ;
1818use GuzzleHttp \Exception \ConnectException ;
1919use GuzzleHttp \Exception \GuzzleException ;
2020use GuzzleHttp \Exception \RequestException ;
2121use GuzzleHttp \Psr7 \Request ;
2222use GuzzleHttp \RequestOptions ;
23+ use Symfony \Component \Serializer \Encoder \JsonEncoder ;
24+ use Symfony \Component \Serializer \Exception \ExceptionInterface ;
25+ use Symfony \Component \Serializer \SerializerInterface ;
2326use Hostinger \ApiException ;
2427use Hostinger \Configuration ;
25- use Hostinger \HeaderSelector ;
2628use Hostinger \ObjectSerializer ;
2729
2830class BillingCatalogApi
@@ -31,16 +33,15 @@ class BillingCatalogApi
3133
3234 protected Configuration $ config ;
3335
34- protected HeaderSelector $ headerSelector ;
36+ protected SerializerInterface $ serializer ;
3537
3638 public function __construct (
37- ?ClientInterface $ client = null ,
3839 ?Configuration $ config = null ,
39- ?HeaderSelector $ selector = null ,
40+ ?ClientInterface $ client = null ,
4041 ) {
41- $ this ->client = $ client ?: new Client ();
4242 $ this ->config = $ config ?: Configuration::getDefaultConfiguration ();
43- $ this ->headerSelector = $ selector ?: new HeaderSelector ();
43+ $ this ->client = $ client ?: new Client ();
44+ $ this ->serializer = ObjectSerializer::getSerializer ();
4445 }
4546
4647 public function getConfig (): Configuration
@@ -49,83 +50,68 @@ public function getConfig(): Configuration
4950 }
5051
5152 /**
52- * Operation getCatalogItemListV1
53- *
54- * Get catalog item list
55- *
56- * @return \Hostinger\Model\BillingV1CatalogCatalogItemCollection|\Hostinger\Model\InlineObject1|\Hostinger\Model\InlineObject
57- * @throws ApiException on non-2xx response or if the response body is not in the expected format
58- * @throws InvalidArgumentException
59- * @throws GuzzleException
60- */
61- public function getCatalogItemListV1 (?string $ category = null , ?string $ name = null , ): \Hostinger \Model \BillingV1CatalogCatalogItemCollection |\Hostinger \Model \InlineObject1 |\Hostinger \Model \InlineObject
53+ * @throws ExceptionInterface
54+ * @throws ApiException
55+ * @throws GuzzleException
56+ */
57+ public function getCatalogItemListV1 (?string $ category = null , ?string $ name = null ): \Hostinger \Model \BillingV1CatalogCatalogItemCollection
6258 {
63- $ request = $ this ->getCatalogItemListV1Request ($ category , $ name , );
59+ $ query = http_build_query (
60+ array_filter ([
61+ 'category ' => $ category ,
62+ ])
63+ );
64+
65+ $ query = http_build_query (
66+ array_filter ([
67+ ', '
68+ 'name' => $ name ,
69+ ])
70+ );
71+
72+ $ request = new Request (
73+ method: 'GET ' ,
74+ uri: '/api/billing/v1/catalog ' . $ query . $ query ,
75+ headers: $ this ->getHeaders (),
76+ );
6477
6578 try {
6679 $ response = $ this ->client ->send ($ request , $ this ->createHttpClientOption ());
6780 } catch (RequestException $ e ) {
68- if ($ this ->config ->shouldThrowException ()) {
69- throw ApiException::fromRequestException ($ e );
70- } else {
71- $ response = $ e ->getResponse ();
72- }
81+ throw ApiException::fromRequestException ($ e );
7382 } catch (ConnectException $ e ) {
7483 throw ApiException::fromConnectException ($ e );
7584 }
7685
77- $ statusCode = $ response ->getStatusCode ();
78- $ returnType = null ;
79-
80- switch ($ statusCode ) {
81- case 200 :
82- $ returnType = '\Hostinger\Model\BillingV1CatalogCatalogItemCollection ' ;
83- break ;
84- case 401 :
85- $ returnType = '\Hostinger\Model\InlineObject1 ' ;
86- break ;
87- case 500 :
88- $ returnType = '\Hostinger\Model\InlineObject ' ;
89- break ;
86+ return $ this ->serializer ->deserialize ($ response ->getBody ()->getContents (), \Hostinger \Model \BillingV1CatalogCatalogItemCollection::class, JsonEncoder::FORMAT );
87+ }
88+
89+ private function buildResourcePath (string $ path , ...$ values ): string
90+ {
91+ foreach ($ values as $ value ) {
92+ if (is_array ($ value )) {
93+ $ value = implode (', ' , $ value );
94+ }
95+
96+ $ path = str_replace ('{ ' . 'BillingCatalog ' . '} ' , $ value , $ path );
9097 }
9198
92- return ObjectSerializer:: deserialize ( $ response -> getBody ()-> getContents (), $ returnType ) ;
99+ return $ path ;
93100 }
94101
95102 /**
96- * Create request for operation 'getCatalogItemListV1'
97- *
98- * @throws InvalidArgumentException
103+ * @return array<string, string>
99104 */
100- protected function getCatalogItemListV1Request (? string $ category = null ,? string $ name = null , ): Request
105+ private function getHeaders ( ): array
101106 {
102- $ resourcePath = '/api/billing/v1/catalog ' ;
103-
104- $ body = null ;
105- $ query = ObjectSerializer::toQueryValue (
106- $ category ,
107- 'category ' , // param base name
108- 'string ' , // openApiType
109- 'form ' , // style
110- true , // explode
111- false // required
112- );
113- $ query = ObjectSerializer::toQueryValue (
114- $ name ,
115- 'name ' , // param base name
116- 'string ' , // openApiType
117- 'form ' , // style
118- true , // explode
119- false // required
120- );
121-
122- return $ this ->buildRequest ('GET ' , $ resourcePath , $ body , $ query );
107+ return [
108+ 'Authorization ' => 'Bearer ' . $ this ->config ->getAccessToken (),
109+ 'Content-Type ' => 'application/json ' ,
110+ 'User-Agent ' => $ this ->config ->getUserAgent (),
111+ ];
123112 }
124113
125- /**
126- * @return array<string, mixed>
127- */
128- protected function createHttpClientOption (): array
114+ private function createHttpClientOption (): array
129115 {
130116 $ options = [];
131117 if ($ this ->config ->getDebug ()) {
@@ -137,36 +123,4 @@ protected function createHttpClientOption(): array
137123
138124 return $ options ;
139125 }
140-
141- /**
142- * @param array<string, string> $query
143- */
144- protected function buildRequest (
145- string $ httpMethod ,
146- string $ resourcePath ,
147- ?string $ body = null ,
148- array $ query = [],
149- string $ contentType = 'application/json ' ,
150- ): Request {
151- $ headers = $ this ->headerSelector ->selectHeaders (
152- accept: ['application/json ' ],
153- contentType: $ contentType ,
154- isMultipart: false
155- );
156- $ headers ['User-Agent ' ] = $ this ->config ->getUserAgent ();
157-
158- // this endpoint requires Bearer authentication (access token)
159- if (!empty ($ this ->config ->getAccessToken ())) {
160- $ headers ['Authorization ' ] = 'Bearer ' . $ this ->config ->getAccessToken ();
161- }
162-
163- $ query = ObjectSerializer::buildQuery ($ query );
164-
165- return new Request (
166- $ httpMethod ,
167- $ this ->config ->getHost () . $ resourcePath . ($ query ? "? $ query " : '' ),
168- $ headers ,
169- $ body
170- );
171- }
172126}
0 commit comments