Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add proxy for dadata client #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ Create API client instance:
> $dadata = new \Dadata\DadataClient($token, $secret);
```

Create API client instance with proxy:

```php
> $token = "Replace with Dadata API key";
> $secret = "Replace with Dadata secret key";
> $proxy = "http://proxy-host/";
> $dadata = new \Dadata\DadataClient($token, $secret, $proxy);
```

Then call API methods as specified below.

## Postal Address
Expand Down
4 changes: 2 additions & 2 deletions src/CleanClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ class CleanClient extends ClientBase
{
const BASE_URL = "https://cleaner.dadata.ru/api/v1/";

public function __construct($token, $secret)
public function __construct($token, $secret, $proxy = '')
{
parent::__construct(self::BASE_URL, $token, $secret);
parent::__construct(self::BASE_URL, $token, $secret, $proxy);
}

public function clean($name, $value)
Expand Down
10 changes: 7 additions & 3 deletions src/ClientBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ abstract class ClientBase
{
public $client;

public function __construct($baseUrl, $token, $secret = null)
public function __construct($baseUrl, $token, $secret = null, $proxy = '')
{
$headers = [
"Content-Type" => "application/json",
Expand All @@ -16,11 +16,15 @@ public function __construct($baseUrl, $token, $secret = null)
if ($secret) {
$headers["X-Secret"] = $secret;
}
$this->client = new \GuzzleHttp\Client([
$options = [
"base_uri" => $baseUrl,
"headers" => $headers,
"timeout" => Settings::TIMEOUT_SEC
]);
];
if ($proxy) {
$options['proxy'] = $proxy;
}
$this->client = new \GuzzleHttp\Client($options);
}

protected function get($url, $query = [])
Expand Down
8 changes: 4 additions & 4 deletions src/DadataClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ class DadataClient
private $profile;
private $suggestions;

public function __construct($token, $secret)
public function __construct($token, $secret, $proxy = '')
{
$this->cleaner = new CleanClient($token, $secret);
$this->profile = new ProfileClient($token, $secret);
$this->suggestions = new SuggestClient($token);
$this->cleaner = new CleanClient($token, $secret, $proxy = '');
$this->profile = new ProfileClient($token, $secret, $proxy = '');
$this->suggestions = new SuggestClient($token, $proxy = '');
}

public function clean($name, $value)
Expand Down
4 changes: 2 additions & 2 deletions src/ProfileClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ class ProfileClient extends ClientBase
{
const BASE_URL = "https://dadata.ru/api/v2/";

public function __construct($token, $secret)
public function __construct($token, $secret, $proxy = '')
{
parent::__construct(self::BASE_URL, $token, $secret);
parent::__construct(self::BASE_URL, $token, $secret, $proxy = '');
}

public function getBalance()
Expand Down
4 changes: 2 additions & 2 deletions src/SuggestClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ class SuggestClient extends ClientBase
{
const BASE_URL = "https://suggestions.dadata.ru/suggestions/api/4_1/rs/";

public function __construct($token)
public function __construct($token, $proxy = '')
{
parent::__construct(self::BASE_URL, $token, null);
parent::__construct(self::BASE_URL, $token, null, $proxy);
}

public function findAffiliated($query, $count = Settings::SUGGESTION_COUNT, $kwargs = [])
Expand Down