Skip to content

Commit

Permalink
- add districts supprort
Browse files Browse the repository at this point in the history
- add langueges support
- add get city by id
- add get region by id
  • Loading branch information
vt-parhomenko committed Jan 14, 2021
1 parent 24e57b6 commit 5a67350
Show file tree
Hide file tree
Showing 11 changed files with 213 additions and 15 deletions.
19 changes: 19 additions & 0 deletions src/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 )
{
Expand Down Expand Up @@ -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
*/
Expand All @@ -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;
}

}
8 changes: 4 additions & 4 deletions src/Api/Adverts.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];

Expand Down Expand Up @@ -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'];

Expand Down Expand Up @@ -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'];

Expand Down Expand Up @@ -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'];

Expand Down
6 changes: 3 additions & 3 deletions src/Api/Categories.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];

Expand All @@ -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'];

Expand All @@ -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'];

Expand Down
37 changes: 34 additions & 3 deletions src/Api/Cities.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
}
}
Expand Down
71 changes: 71 additions & 0 deletions src/Api/Districts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace Parhomenko\Olx\Api;

use GuzzleHttp\Client;

class Districts
{
const API_VERSION = '2.0';
const OLX_DISTRICTS_URL = '/api/partner/districts';

private $user;
private $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_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;
}
}
}
43 changes: 43 additions & 0 deletions src/Api/Languages.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
2 changes: 1 addition & 1 deletion src/Api/Messages.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];

Expand Down
34 changes: 33 additions & 1 deletion src/Api/Regions.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ public function __construct( User $user, Client $guzzleClient )
$this->guzzleClient = $guzzleClient;
}

/**
* @return array
* @throws \Exception
*/
public function getAll() : array
{
try {
Expand All @@ -31,12 +35,40 @@ 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'];

} catch ( \Exception $e ){
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;
}
}
}
4 changes: 2 additions & 2 deletions src/Api/Threads.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];

Expand Down Expand Up @@ -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'];

Expand Down
2 changes: 1 addition & 1 deletion src/Api/Users.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];

Expand Down
2 changes: 2 additions & 0 deletions src/IOlxApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

0 comments on commit 5a67350

Please sign in to comment.