-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.php
More file actions
94 lines (82 loc) · 2.32 KB
/
Copy pathClient.php
File metadata and controls
94 lines (82 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
namespace WeDesignIt\Sendy;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Exception\GuzzleException;
use Psr\Http\Message\ResponseInterface;
class Client
{
/**
* @var string
*/
//protected string $baseUrl = 'https://portal.keendelivery.com/api/v3/';
// will become:
protected string $baseUrl = 'https://app.sendy.nl/api/';
/**
* @var GuzzleClient
*/
protected GuzzleClient $client;
/**
* Personal access tokens may be requested from the user profile section in Sendy.
*
* @var string
*/
private string $token;
/**
* Client constructor.
*
* @param string $token
*/
public function __construct(string $token)
{
$this->token = $token;
$this->client = new GuzzleClient([
'base_uri' => $this->baseUrl,
'headers' => [
'Authorization' => 'Bearer ' . $this->token,
'Accept' => 'application/json',
'User-Agent' => 'sendy-php-api-client/1.0 (github.com/wedesignit/sendy-php-api-client)',
],
]);
}
/**
* @param string $method
* @param string $uri
* @param array $options
*
* @return array|string Array if the response was JSON, raw response body otherwise.
* @throws GuzzleException
*/
public function request(
string $method,
string $uri,
array $options = []
): array|string
{
$response = $this->rawRequest($method, $uri, $options);
$contents = $response->getBody()->getContents();
// fallback to application/json as this is, the default return type
$default = 'application/json';
if (stristr(($response->getHeader('Content-Type')[0] ?? $default), 'application/json') !== false) {
$array = json_decode($contents, true);
return (array) $array;
} else {
return $contents;
}
}
/**
* @param string $method
* @param string $uri
* @param array $options
*
* @return ResponseInterface
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function rawRequest(
string $method,
string $uri,
array $options = []
): ResponseInterface
{
return $this->client->request($method, $uri, $options);
}
}