Skip to content

Commit

Permalink
PHPDoc GuzzleException Throwable (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurshlain committed Apr 11, 2024
1 parent cc85a59 commit b73e6dc
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/CleanClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Dadata;

use GuzzleHttp\Exception\GuzzleException;

class CleanClient extends ClientBase
{
const BASE_URL = "https://cleaner.dadata.ru/api/v1/";
Expand All @@ -11,6 +13,9 @@ public function __construct($token, $secret)
parent::__construct(self::BASE_URL, $token, $secret);
}

/**
* @throws GuzzleException
*/
public function clean($name, $value)
{
$url = "clean/$name";
Expand All @@ -19,6 +24,9 @@ public function clean($name, $value)
return $response[0];
}

/**
* @throws GuzzleException
*/
public function cleanRecord($structure, $record)
{
$url = "clean";
Expand Down
11 changes: 10 additions & 1 deletion src/ClientBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Dadata;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;

abstract class ClientBase
{
public $client;
Expand All @@ -16,19 +19,25 @@ public function __construct($baseUrl, $token, $secret = null)
if ($secret) {
$headers["X-Secret"] = $secret;
}
$this->client = new \GuzzleHttp\Client([
$this->client = new Client([
"base_uri" => $baseUrl,
"headers" => $headers,
"timeout" => Settings::TIMEOUT_SEC
]);
}

/**
* @throws GuzzleException
*/
protected function get($url, $query = [])
{
$response = $this->client->get($url, ["query" => $query]);
return json_decode($response->getBody(), true);
}

/**
* @throws GuzzleException
*/
protected function post($url, $data)
{
$response = $this->client->post($url, [
Expand Down
32 changes: 32 additions & 0 deletions src/DadataClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Dadata;

use GuzzleHttp\Exception\GuzzleException;

class DadataClient
{
private $cleaner;
Expand All @@ -15,51 +17,81 @@ public function __construct($token, $secret)
$this->suggestions = new SuggestClient($token, $secret);
}

/**
* @throws GuzzleException
*/
public function clean($name, $value)
{
return $this->cleaner->clean($name, $value);
}

/**
* @throws GuzzleException
*/
public function cleanRecord($structure, $record)
{
return $this->cleaner->cleanRecord($structure, $record);
}

/**
* @throws GuzzleException
*/
public function findAffiliated($query, $count = Settings::SUGGESTION_COUNT, $kwargs = [])
{
return $this->suggestions->findAffiliated($query, $count, $kwargs);
}

/**
* @throws GuzzleException
*/
public function findById($name, $query, $count = Settings::SUGGESTION_COUNT, $kwargs = [])
{
return $this->suggestions->findById($name, $query, $count, $kwargs);
}

/**
* @throws GuzzleException
*/
public function geolocate($name, $lat, $lon, $radiusMeters = 100, $count = Settings::SUGGESTION_COUNT, $kwargs = [])
{
return $this->suggestions->geolocate($name, $lat, $lon, $radiusMeters, $count, $kwargs);
}

/**
* @throws GuzzleException
*/
public function getBalance()
{
return $this->profile->getBalance();
}

/**
* @throws GuzzleException
*/
public function getDailyStats($date = null)
{
return $this->profile->getDailyStats($date);
}

/**
* @throws GuzzleException
*/
public function getVersions()
{
return $this->profile->getVersions();
}

/**
* @throws GuzzleException
*/
public function iplocate($ip, $kwargs = [])
{
return $this->suggestions->iplocate($ip, $kwargs);
}

/**
* @throws GuzzleException
*/
public function suggest($name, $query, $count = Settings::SUGGESTION_COUNT, $kwargs = [])
{
return $this->suggestions->suggest($name, $query, $count, $kwargs);
Expand Down
10 changes: 10 additions & 0 deletions src/ProfileClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Dadata;

use DateTime;
use GuzzleHttp\Exception\GuzzleException;

class ProfileClient extends ClientBase
{
Expand All @@ -13,13 +14,19 @@ public function __construct($token, $secret)
parent::__construct(self::BASE_URL, $token, $secret);
}

/**
* @throws GuzzleException
*/
public function getBalance()
{
$url = "profile/balance";
$response = $this->get($url);
return $response["balance"];
}

/**
* @throws GuzzleException
*/
public function getDailyStats($date = null)
{
$url = "stat/daily";
Expand All @@ -31,6 +38,9 @@ public function getDailyStats($date = null)
return $response;
}

/**
* @throws GuzzleException
*/
public function getVersions()
{
$url = "version";
Expand Down
17 changes: 17 additions & 0 deletions src/SuggestClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Dadata;

use GuzzleHttp\Exception\GuzzleException;

class SuggestClient extends ClientBase
{
const BASE_URL = "https://suggestions.dadata.ru/suggestions/api/4_1/rs/";
Expand All @@ -11,6 +13,9 @@ public function __construct($token, $secret = null)
parent::__construct(self::BASE_URL, $token, $secret);
}

/**
* @throws GuzzleException
*/
public function findAffiliated($query, $count = Settings::SUGGESTION_COUNT, $kwargs = [])
{
$url = "findAffiliated/party";
Expand All @@ -20,6 +25,9 @@ public function findAffiliated($query, $count = Settings::SUGGESTION_COUNT, $kwa
return $response["suggestions"];
}

/**
* @throws GuzzleException
*/
public function findById($name, $query, $count = Settings::SUGGESTION_COUNT, $kwargs = [])
{
$url = "findById/$name";
Expand All @@ -29,6 +37,9 @@ public function findById($name, $query, $count = Settings::SUGGESTION_COUNT, $kw
return $response["suggestions"];
}

/**
* @throws GuzzleException
*/
public function geolocate($name, $lat, $lon, $radiusMeters = 100, $count = Settings::SUGGESTION_COUNT, $kwargs = [])
{
$url = "geolocate/$name";
Expand All @@ -43,6 +54,9 @@ public function geolocate($name, $lat, $lon, $radiusMeters = 100, $count = Setti
return $response["suggestions"];
}

/**
* @throws GuzzleException
*/
public function iplocate($ip, $kwargs = [])
{
$url = "iplocate/address";
Expand All @@ -52,6 +66,9 @@ public function iplocate($ip, $kwargs = [])
return $response["location"];
}

/**
* @throws GuzzleException
*/
public function suggest($name, $query, $count = Settings::SUGGESTION_COUNT, $kwargs = [])
{
$url = "suggest/$name";
Expand Down

0 comments on commit b73e6dc

Please sign in to comment.