From 5a67350a373fd4c059f0af282405ba0a1aefedad Mon Sep 17 00:00:00 2001 From: Vitaliy Parhomenko Date: Thu, 14 Jan 2021 13:57:43 +0200 Subject: [PATCH] - add districts supprort - add langueges support - add get city by id - add get region by id --- src/Api.php | 19 +++++++++++ src/Api/Adverts.php | 8 ++--- src/Api/Categories.php | 6 ++-- src/Api/Cities.php | 37 ++++++++++++++++++++-- src/Api/Districts.php | 71 ++++++++++++++++++++++++++++++++++++++++++ src/Api/Languages.php | 43 +++++++++++++++++++++++++ src/Api/Messages.php | 2 +- src/Api/Regions.php | 34 +++++++++++++++++++- src/Api/Threads.php | 4 +-- src/Api/Users.php | 2 +- src/IOlxApi.php | 2 ++ 11 files changed, 213 insertions(+), 15 deletions(-) create mode 100644 src/Api/Districts.php diff --git a/src/Api.php b/src/Api.php index d374700..76c038e 100644 --- a/src/Api.php +++ b/src/Api.php @@ -3,6 +3,8 @@ namespace Parhomenko\Olx; use GuzzleHttp\Client; +use Parhomenko\Olx\Api\Districts; +use Parhomenko\Olx\Api\Languages; use Parhomenko\Olx\Api\User; use Parhomenko\Olx\Api\Categories; use Parhomenko\Olx\Api\Adverts; @@ -19,9 +21,11 @@ class Api implements IOlxApi private $categories = null; private $adverts = null; private $cities = null; + private $districts = null; private $regions = null; private $currencies = null; private $users = null; + private $languages = null; public function __construct( string $base_uri, array $credentials, bool $update_token = false ) { @@ -68,6 +72,14 @@ public function cities() return is_null($this->cities ) ? new Cities( $this->user, $this->guzzleClient ) : $this->cities; } + /** + * @return Districts + */ + public function districts() + { + return is_null($this->districts ) ? new Districts( $this->user, $this->guzzleClient ) : $this->districts; + } + /** * @return Currencies */ @@ -83,4 +95,11 @@ public function users(){ return is_null($this->users) ? new Users( $this->user, $this->guzzleClient ) : $this->users; } + /** + * @return Languages + */ + public function languages(){ + return is_null($this->languages) ? new Languages( $this->user, $this->guzzleClient ) : $this->languages; + } + } \ No newline at end of file diff --git a/src/Api/Adverts.php b/src/Api/Adverts.php index 7e711ae..4a9e6a5 100644 --- a/src/Api/Adverts.php +++ b/src/Api/Adverts.php @@ -37,7 +37,7 @@ public function get( int $id ) : array ] ); $advert = json_decode( $response->getBody()->getContents(), true ); - if( !isset( $advert['data'] ) ) throw new \Exception( 'Got empty response | Get OLX.ua advert' ); + if( !isset( $advert['data'] ) ) throw new \Exception( 'Got empty response | Get OLX advert' ); return $advert['data']; @@ -79,7 +79,7 @@ public function getAll( int $offset = 0, int $limit = null, int $external_id = n $adverts = json_decode( $response->getBody()->getContents(), true ); - if( !isset( $adverts['data'] ) ) throw new \Exception( 'Got empty response | Get all OLX.ua adverts' ); + if( !isset( $adverts['data'] ) ) throw new \Exception( 'Got empty response | Get all OLX adverts' ); return $adverts['data']; @@ -110,7 +110,7 @@ public function create( array $params ) : array ] ); $advert = json_decode( $response->getBody()->getContents(), true ); - if( !isset( $advert['data'] ) ) throw new \Exception( 'Got empty response | Create OLX.ua advert: ' .$params['title'] ); + if( !isset( $advert['data'] ) ) throw new \Exception( 'Got empty response | Create OLX advert: ' .$params['title'] ); return $advert['data']; @@ -141,7 +141,7 @@ public function update( int $id, array $params ) : array ] ); $advert = json_decode( $response->getBody()->getContents(), true ); - if( !isset( $advert['data'] ) ) throw new \Exception( 'Got empty response | Update OLX.ua advert: ' .$params['title'] ); + if( !isset( $advert['data'] ) ) throw new \Exception( 'Got empty response | Update OLX advert: ' .$params['title'] ); return $advert['data']; diff --git a/src/Api/Categories.php b/src/Api/Categories.php index 1ebfc74..0ee9386 100644 --- a/src/Api/Categories.php +++ b/src/Api/Categories.php @@ -36,7 +36,7 @@ public function get( int $category_id ) : array $categories = json_decode( $response->getBody()->getContents(), true ); - if( !isset( $categories['data'] ) ) throw new \Exception( 'Got empty response | Get OLX.ua category: ' .$category_id ); + if( !isset( $categories['data'] ) ) throw new \Exception( 'Got empty response | Get OLX category: ' .$category_id ); return $categories['data']; @@ -62,7 +62,7 @@ public function getAll( int $parent_id = 0 ) : array $categories = json_decode( $response->getBody()->getContents(), true ); - if( !isset( $categories['data'] ) ) throw new \Exception( 'Got empty response | Get all OLX.ua categories, parent_id: ' .$parent_id ); + if( !isset( $categories['data'] ) ) throw new \Exception( 'Got empty response | Get all OLX categories, parent_id: ' .$parent_id ); return $categories['data']; @@ -89,7 +89,7 @@ public function attributes( int $category_id ) : array $attributes = json_decode( $response->getBody()->getContents(), true ); - if( !isset( $attributes['data'] ) ) throw new \Exception( 'Got empty response | Get OLX.ua category attributes: ' .$category_id ); + if( !isset( $attributes['data'] ) ) throw new \Exception( 'Got empty response | Get OLX category attributes: ' .$category_id ); return $attributes['data']; diff --git a/src/Api/Cities.php b/src/Api/Cities.php index 2121c4d..a75ea77 100644 --- a/src/Api/Cities.php +++ b/src/Api/Cities.php @@ -18,6 +18,12 @@ public function __construct( User $user, Client $guzzleClient ) $this->guzzleClient = $guzzleClient; } + /** + * @param int $offset + * @param int|null $limit + * @return array + * @throws \Exception + */ public function getAll(int $offset = 0, int $limit = null) : array { try { @@ -34,12 +40,37 @@ public function getAll(int $offset = 0, int $limit = null) : array $cities = json_decode( $response->getBody()->getContents(), true ); - if( !isset( $cities['data'] ) ) - throw new \Exception( 'Got empty response | Get all OLX.ua cities' ); + if( !isset( $cities['data'] ) ) throw new \Exception( 'Got empty response | Get all OLX cities' ); return $cities['data']; - } catch ( \Exception $e ){ + }catch( \Exception $e ){ + throw $e; + } + } + + /** + * @param int $city_id + * @return array + * @throws \Exception + */ + public function get(int $city_id) : array + { + try { + $response = $this->guzzleClient->request('GET', self::OLX_CITIES_URL .'/' .$city_id, [ + 'headers' => [ + 'Authorization' => $this->user->getTokenType() .' ' .$this->user->getAccessToken(), + 'Version' => self::API_VERSION + ] + ]); + + $data = json_decode( $response->getBody()->getContents(), true ); + + if( !isset( $data['data'] ) ) throw new \Exception( 'Got empty response | Get all OLX cities' ); + + return $data['data']; + + }catch( \Exception $e ){ throw $e; } } diff --git a/src/Api/Districts.php b/src/Api/Districts.php new file mode 100644 index 0000000..0428dfd --- /dev/null +++ b/src/Api/Districts.php @@ -0,0 +1,71 @@ +user = $user; + $this->guzzleClient = $guzzleClient; + } + + /** + * @return array + * @throws \Exception + */ + public function getAll() : array + { + try { + $response = $this->guzzleClient->request('GET', self::OLX_DISTRICTS_URL, [ + 'headers' => [ + 'Authorization' => $this->user->getTokenType() .' ' .$this->user->getAccessToken(), + 'Version' => self::API_VERSION + ] + ]); + + $data = json_decode( $response->getBody()->getContents(), true ); + + if( !isset( $data['data'] ) ) throw new \Exception( 'Got empty response | Get all OLX districts' ); + + return $data['data']; + + } catch ( \Exception $e ){ + throw $e; + } + } + + /** + * @param int $district_id + * @return array + * @throws \Exception + */ + public function get(int $district_id) : array + { + try { + $response = $this->guzzleClient->request('GET', self::OLX_DISTRICTS_URL .'/' .$district_id, [ + 'headers' => [ + 'Authorization' => $this->user->getTokenType() .' ' .$this->user->getAccessToken(), + 'Version' => self::API_VERSION + ] + ]); + + $data = json_decode( $response->getBody()->getContents(), true ); + + if( !isset( $data['data'] ) ) throw new \Exception( 'Got empty response | Get all OLX district' ); + + return $data['data']; + + } catch ( \Exception $e ){ + throw $e; + } + } +} \ No newline at end of file diff --git a/src/Api/Languages.php b/src/Api/Languages.php index a64af9b..b418d99 100644 --- a/src/Api/Languages.php +++ b/src/Api/Languages.php @@ -2,7 +2,50 @@ namespace Parhomenko\Olx\Api; +use GuzzleHttp\Client; + class Languages { + const API_VERSION = '2.0'; + const OLX_LANGUAGES_URL = '/api/partner/languages'; + + private $user; + private $guzzleClient; + + /** + * Languages constructor. + * @param User $user + * @param Client $guzzleClient + */ + public function __construct( User $user, Client $guzzleClient ) + { + $this->user = $user; + $this->guzzleClient = $guzzleClient; + } + + /** + * @return array + * @throws \Exception + */ + public function getAll() : array + { + try { + $response = $this->guzzleClient->request('GET', self::OLX_LANGUAGES_URL, [ + 'headers' => [ + 'Authorization' => $this->user->getTokenType() .' ' .$this->user->getAccessToken(), + 'Version' => self::API_VERSION + ] + ]); + + $data = json_decode($response->getBody()->getContents(), true); + + if( !isset( $data['data'] ) ) + throw new \Exception( 'Got empty response | Get all OLX regions' ); + + return $data['data']; + } catch ( \Exception $e ){ + throw $e; + } + } } \ No newline at end of file diff --git a/src/Api/Messages.php b/src/Api/Messages.php index f2dfc62..d77e9ff 100644 --- a/src/Api/Messages.php +++ b/src/Api/Messages.php @@ -47,7 +47,7 @@ public function get( int $thread_id, int $offset = 0, int $limit = null ) : arra $messages = json_decode( $response->getBody()->getContents(), true ); - if( !isset( $messages['data'] ) ) throw new \Exception( 'Got empty response | Get all OLX.ua thread messages' ); + if( !isset( $messages['data'] ) ) throw new \Exception( 'Got empty response | Get all OLX thread messages' ); return $messages['data']; diff --git a/src/Api/Regions.php b/src/Api/Regions.php index c4955ba..db3c76a 100644 --- a/src/Api/Regions.php +++ b/src/Api/Regions.php @@ -18,6 +18,10 @@ public function __construct( User $user, Client $guzzleClient ) $this->guzzleClient = $guzzleClient; } + /** + * @return array + * @throws \Exception + */ public function getAll() : array { try { @@ -31,7 +35,7 @@ public function getAll() : array $regions = json_decode($response->getBody()->getContents(), true); if( !isset( $regions['data'] ) ) - throw new \Exception( 'Got empty response | Get all OLX.ua regions' ); + throw new \Exception( 'Got empty response | Get all OLX regions' ); return $regions['data']; @@ -39,4 +43,32 @@ public function getAll() : array throw $e; } } + + /** + * @param int $region_id + * @return array + * @throws \Exception + */ + public function get( int $region_id ) : array + { + try { + $response = $this->guzzleClient->request('GET', self::OLX_REGIONS_URL .'/' .$region_id, [ + 'headers' => [ + 'Authorization' => $this->user->getTokenType() .' ' .$this->user->getAccessToken(), + 'Version' => self::API_VERSION + ] + ]); + + $data = json_decode($response->getBody()->getContents(), true); + + if( !isset( $data['data'] ) ){ + throw new \Exception( 'Got empty response | Get all OLX regions' ); + } + + return $data['data']; + + } catch ( \Exception $e ){ + throw $e; + } + } } \ No newline at end of file diff --git a/src/Api/Threads.php b/src/Api/Threads.php index 3955f3e..5c1d3ef 100644 --- a/src/Api/Threads.php +++ b/src/Api/Threads.php @@ -38,7 +38,7 @@ public function get( int $thread_id ) : array $thread = json_decode( $response->getBody()->getContents(), true ); - if( !isset( $thread['data'] ) ) throw new \Exception( 'Got empty response | Get OLX.ua thread' ); + if( !isset( $thread['data'] ) ) throw new \Exception( 'Got empty response | Get OLX thread' ); return $thread['data']; @@ -80,7 +80,7 @@ public function getAll( int $offset = 0, int $limit = null, int $advert_id = nul $threads = json_decode( $response->getBody()->getContents(), true ); - if( !isset( $threads['data'] ) ) throw new \Exception( 'Got empty response | Get all OLX.ua threads' ); + if( !isset( $threads['data'] ) ) throw new \Exception( 'Got empty response | Get all OLX threads' ); return $threads['data']; diff --git a/src/Api/Users.php b/src/Api/Users.php index a0af476..2b03c0c 100644 --- a/src/Api/Users.php +++ b/src/Api/Users.php @@ -71,7 +71,7 @@ public function get( int $user_id ) : array $user_info = json_decode( $response->getBody()->getContents(), true ); - if( !isset( $user_info['data'] ) ) throw new \Exception( 'Got empty response | Get OLX.ua advert' ); + if( !isset( $user_info['data'] ) ) throw new \Exception( 'Got empty response | Get OLX advert' ); return $user_info['data']; diff --git a/src/IOlxApi.php b/src/IOlxApi.php index 609d87f..de89e32 100644 --- a/src/IOlxApi.php +++ b/src/IOlxApi.php @@ -8,5 +8,7 @@ public function categories(); public function adverts(); public function regions(); public function cities(); + public function districts(); public function currencies(); + public function users(); } \ No newline at end of file