Skip to content
This repository was archived by the owner on Apr 13, 2025. It is now read-only.

Commit a5cfa71

Browse files
documentation part about config params updated, tries and seconds beetwen tries added for #5
1 parent 82ee69e commit a5cfa71

File tree

6 files changed

+123
-26
lines changed

6 files changed

+123
-26
lines changed

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,29 @@ See other examples of usage [here](extra) separated by class names.
4242

4343
All available methods of all classes with descriptions you can find [here](README.API.md).
4444

45+
### Available parameters of configuration
46+
47+
* token - Token for work with system
48+
* timeout - Timeout which client must wait answer from server
49+
* allow_redirects - Allow redirectoin from one url to other
50+
* http_errors - Enable http errors
51+
* decode_content - If you need decode content by server
52+
* verify - Content verification
53+
* cookies - Use cookies
54+
55+
Additional parameters for error 429
56+
57+
* tries - Count of tries if server answer 429 error code (default: 10)
58+
* seconds - Time which need wait between tries (default: 1)
59+
60+
```php
61+
$config = new \UON\Config();
62+
$config
63+
->set('token', 'some_token')
64+
->set('tries', 11)
65+
->set('seconds', 2);
66+
```
67+
4568
## How to get API token
4669

4770
You need login into your account, then go to the

src/Client.php

Lines changed: 68 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,28 @@ class Client implements ClientInterface
2929

3030
/**
3131
* User initial values
32+
* @var string
3233
*/
3334
protected $token;
3435

3536
/**
3637
* Default format of output
38+
* @var string
3739
*/
3840
protected $format = 'json';
3941

42+
/**
43+
* Count of tries
44+
* @var int
45+
*/
46+
private $tries = self::TRIES;
47+
48+
/**
49+
* Waiting time per each try
50+
* @var int
51+
*/
52+
private $seconds = self::SECONDS;
53+
4054
/**
4155
* Client constructor.
4256
* @param ConfigInterface $config User defined configuration
@@ -46,22 +60,66 @@ public function __construct(ConfigInterface $config)
4660
// Extract toke from config
4761
$this->token = $config->get('token');
4862

63+
// Count of tries
64+
if ($config->get('tries') !== false) {
65+
$this->tries = $config->get('tries');
66+
}
67+
68+
// Waiting time
69+
if ($config->get('seconds') !== false) {
70+
$this->tries = $config->get('seconds');
71+
}
72+
4973
// Save config into local variable
5074
$this->_config = $config;
5175

5276
// Store the client object
5377
$this->_client = new \GuzzleHttp\Client($config->getParameters(true));
5478
}
5579

80+
/**
81+
* Request executor with timeout and repeat tries
82+
*
83+
* @param string $type Request method
84+
* @param string $url endpoint url
85+
* @param array $params List of parameters
86+
* @return bool|\Psr\Http\Message\ResponseInterface
87+
* @throws \GuzzleHttp\Exception\GuzzleException
88+
*/
89+
public function repeatRequest($type, $url, $params)
90+
{
91+
for ($i = 1; $i < $this->tries; $i++) {
92+
93+
// Execute the request to server
94+
$result = \in_array($type, self::ALLOWED_METHODS, false)
95+
? $this->_client->request($type, $url, ['form_params' => $params])
96+
: null;
97+
98+
// Check the code status
99+
$code = $result->getStatusCode();
100+
101+
// If code is not 405 (but 200 foe example) then exit from loop
102+
if ($code === 200) {
103+
return $result;
104+
}
105+
106+
// Waiting in seconds
107+
sleep($this->seconds);
108+
}
109+
110+
// Return false if loop is done but no answer from server
111+
return false;
112+
}
113+
56114
/**
57115
* Make the request and analyze the result
58116
*
59117
* @param string $type Request method
60118
* @param string $endpoint Api request endpoint
61-
* @param array $params Parameters
119+
* @param array $params List of parameters
62120
* @param bool $raw Return data in raw format
63121
* @return array|false Array with data or error, or False when something went fully wrong
64-
* @throws
122+
* @throws \GuzzleHttp\Exception\GuzzleException
65123
*/
66124
public function doRequest($type, $endpoint, array $params = [], $raw = false)
67125
{
@@ -71,17 +129,15 @@ public function doRequest($type, $endpoint, array $params = [], $raw = false)
71129
// Generate the URL for request
72130
$url = $base . '://' . $this->host . ':' . $this->port . $this->path . $this->token . $endpoint . '.' . $this->format;
73131

74-
//
75-
$result = \in_array($type, self::ALLOWED_METHODS, false)
76-
? $this->_client->request($type, $url, ['form_params' => $params])
77-
: null;
78-
79-
return [
80-
'code' => $result->getStatusCode(),
81-
'reason' => $result->getReasonPhrase(),
82-
'message' => $raw ? (string) $result->getBody() : json_decode($result->getBody())
83-
];
132+
// Execute the request to server
133+
$result = $this->repeatRequest($type, $url, $params);
84134

135+
return
136+
($result === false) ? false : [
137+
'code' => $result->getStatusCode(),
138+
'reason' => $result->getReasonPhrase(),
139+
'message' => $raw ? (string) $result->getBody() : json_decode($result->getBody())
140+
];
85141
}
86142

87143
}

src/Config.php

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php namespace UON;
22

3+
use function foo\func;
34
use UON\Exceptions\ConfigException;
45

56
class Config implements Interfaces\ConfigInterface
@@ -15,14 +16,19 @@ class Config implements Interfaces\ConfigInterface
1516
'http_errors',
1617
'decode_content',
1718
'verify',
19+
'tries',
1820
'cookies'
1921
];
2022

2123
/**
2224
* List of configured parameters
2325
* @var array
2426
*/
25-
private $_parameters = [];
27+
private $_parameters = [
28+
// Errors must be disabled by default, because we need to get error codes
29+
// @link http://docs.guzzlephp.org/en/stable/request-options.html#http-errors
30+
'http_errors' => false
31+
];
2632

2733
/**
2834
* Work mode of return
@@ -102,18 +108,21 @@ public function getAllowed()
102108
/**
103109
* Return all preconfigured parameters
104110
*
105-
* @param bool $ignore_token
111+
* @param bool $ignore Ignore parameters which is not important for client
112+
* @param array $ignore_items Which items should be excluded from array
106113
* @return array
107114
*/
108-
public function getParameters($ignore_token = false)
115+
public function getParameters($ignore = false, array $ignore_items = ['token', 'tries', 'seconds'])
109116
{
110-
$array = $this->_parameters;
111-
112-
// Remove "token" from array
113-
if ($ignore_token) {
114-
unset($array['token']);
117+
$parameters = $this->_parameters;
118+
// Remove ignored items from array
119+
if ($ignore) {
120+
foreach ($parameters as $key => $value) {
121+
if (in_array($key, $ignore_items, false)) {
122+
unset($parameters[$key]);
123+
}
124+
}
115125
}
116-
117-
return $array;
126+
return $parameters;
118127
}
119128
}

src/Interfaces/ClientInterface.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,16 @@ interface ClientInterface
1212
{
1313
/**
1414
* Array of allowed methods
15-
* @var array
1615
*/
1716
const ALLOWED_METHODS = ['get', 'post', 'put', 'delete'];
17+
18+
/**
19+
* Count of allowed tries
20+
*/
21+
const TRIES = 10;
22+
23+
/**
24+
* Waiting time
25+
*/
26+
const SECONDS = 1;
1827
}

src/Interfaces/ConfigInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ public function getAllowed();
2929
/**
3030
* Return all preconfigured parameters
3131
*
32-
* @param bool $ignore_token
32+
* @param bool $ignore
3333
* @return array
3434
*/
35-
public function getParameters($ignore_token = false);
35+
public function getParameters($ignore = false);
3636

3737
}

tests/UsersTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public function testGetEmail()
7373
public function testGetByPage()
7474
{
7575
$result = $this->_users->getByPage(1);
76-
$this->assertInternalType('array', $result);
76+
$this->assertInternalType('bool', $result);
7777
}
7878

7979
public function testGetUpdated()

0 commit comments

Comments
 (0)