-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathClient.php
More file actions
148 lines (129 loc) · 4.61 KB
/
Client.php
File metadata and controls
148 lines (129 loc) · 4.61 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php declare(strict_types = 1);
namespace JasonRoman\NbaApi\Client;
use Doctrine\Common\Annotations\AnnotationRegistry;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\TransferStats;
use JasonRoman\NbaApi\Request\AbstractNbaApiRequest;
use JasonRoman\NbaApi\Response\NbaApiResponse;
use JasonRoman\NbaApi\Response\ResponseType;
use Psr\Http\Message\ResponseInterface;
use Symfony\Component\Validator\Validation;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Component\Validator\ValidatorBuilderInterface;
/**
* Main Client class that processes requests.
*/
class Client
{
/**
* @var GuzzleClient
*/
protected $guzzle;
/**
* @var bool
*/
protected $validateRequest;
/**
* @var ValidatorBuilderInterface
*/
protected $validator;
/**
* Create the guzzle client from the child class's config/headers, and any overridden parameters.
* Instantiate the validator in case validation is to be performed later.
*
* @param array $config any additional Guzzle configuration that overrides any defaults
* @param bool $validateRequest whether to validate requests with the Symfony Validator
* @param GuzzleClient|null $guzzle if specified, overrides the default client that would be created
* @param ValidatorInterface|null $validator if specified, overrides the default validator that would be created
*/
public function __construct(
array $config = [],
$validateRequest = true,
GuzzleClient $guzzle = null,
ValidatorInterface $validator = null
) {
// this is required so that annotations can be used for validation
AnnotationRegistry::registerLoader('class_exists');
$this->validateRequest = $validateRequest;
// initialize a new Guzzle client if one was not passed in
$this->guzzle = $guzzle ?? new GuzzleClient($config);
// initialize the validator if one was not passed in
$this->validator = $validator
?? Validation::createValidatorBuilder()->enableAnnotationMapping()->getValidator();
}
/**
* @param AbstractNbaApiRequest $request
* @param array $config
* @return NbaApiResponse
* @throws \Exception if validation fails
*/
public function request(AbstractNbaApiRequest $request, array $config = []): NbaApiResponse
{
// validate the request if specified, and throw an exception if invalid
if ($this->validateRequest) {
$violations = $this->validator->validate($request);
if (count($violations)) {
throw new \Exception((string) $violations);
}
}
// override the default application/json accept headers based on the response type
$acceptHeadersExtra = (in_array($request->getResponseType(), ResponseType::TYPES))
? ['Accept' => ResponseType::ACCEPT_HEADERS[$request->getResponseType()]]
: [];
// return the response from the Guzzle request
return $this->apiRequest(
$request->getMethod(),
$request->getEndpoint(),
array_merge(
['query' => $request->getQueryParams()],
['headers' => array_merge($request->getHeaders(), $acceptHeadersExtra)],
array_merge($request->getConfig(), $config),
// for testing purposes
['on_stats' => function (TransferStats $stats) {
//dump($stats->getRequest());
}]
)
);
}
/**
* @param mixed $method
* @param string $uri
* @param array $options
* @return NbaApiResponse
* @throws ClientException
*/
public function apiRequest($method, $uri, array $options = []): NbaApiResponse
{
$response = $this->guzzle->request($method, $uri, $options);
return $this->responseWrapper($response);
}
/**
* Get whether the request is to be validated.
*
* @return bool
*/
public function getValidateRequest(): bool
{
return $this->validateRequest;
}
/**
* Set whether the request should be validated.
*
* @param bool $validateRequest
* @return $this
*/
public function setValidateRequest(bool $validateRequest)
{
$this->validateRequest = $validateRequest;
return $this;
}
/**
* @param ResponseInterface $response
* @return NbaApiResponse
*/
protected function responseWrapper(ResponseInterface $response): NbaApiResponse
{
return new NbaApiResponse($response);
}
}