-
Notifications
You must be signed in to change notification settings - Fork 5
/
DmmClient.php
159 lines (139 loc) · 3.92 KB
/
DmmClient.php
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
149
150
151
152
153
154
155
156
157
158
159
<?php
namespace Dmm;
use Dmm\HttpClients\HttpClientInterface;
use Dmm\HttpClients\GuzzleHttpClient;
use Dmm\HttpClients\CurlHttpClient;
use Dmm\HttpClients\StreamHttpClient;
use Dmm\Exceptions\DmmSDKException;
/**
* Class DmmClient
*
* @package Dmm
*/
class DmmClient
{
/**
* @const string Production API URL.
*/
const BASE_API_URL = "https://api.dmm.com/affiliate/v3";
/**
* @const integer The timeout in seconds for a normal request.
*/
const DEFAULT_REQUEST_TIMEOUT = 60;
/**
* @var HttpClientInterface HTTP client handler.
*/
protected $httpClientHandler;
/**
* @var integer The number of calls that have been made to Graph.
*/
public static $requestCount = 0;
/**
* Instantiates a new DmmClient object.
*
* @param HttpClientInterface|null $httpClientHandler
*/
public function __construct(HttpClientInterface $httpClientHandler = null)
{
$this->httpClientHandler = $httpClientHandler ?: $this->detectHttpClientHandler();
}
/**
* Sets the HTTP client handler.
*
* @param HttpClientInterface $httpClientHandler
*/
public function setHttpClientHandler(HttpClientInterface $httpClientHandler)
{
$this->httpClientHandler = $httpClientHandler;
}
/**
* Returns the HTTP client handler.
*
* @return HttpClientInterface
*/
public function getHttpClientHandler()
{
return $this->httpClientHandler;
}
/**
* Detects which HTTP client handler to use.
*
* @return HttpClientInterface
*/
public function detectHttpClientHandler()
{
$handler = null;
if (class_exists('GuzzleHttp\Client')) {
$handler = new GuzzleHttpClient();
} elseif (function_exists('curl_init')) {
$handler = new CurlHttpClient();
} else {
$handler = new StreamHttpClient();
}
return $handler;
}
/**
* Returns the base API URL.
*
* @return string
*/
public function getBaseApiUrl()
{
return static::BASE_API_URL;
}
/**
* Prepares the request for sending to the client handler.
*
* @param DmmRequest $request
*
* @return array
*/
public function prepareRequestMessage(DmmRequest $request)
{
$url = $this->getBaseApiUrl() . $request->getUrl();
$requestBody = $request->getUrlEncodedBody();
if ($request->getMethod() == "POST") {
$request->setHeaders([
'Content-Type' => 'application/x-www-form-urlencoded',
]);
}
return [
$url,
$request->getMethod(),
$request->getHeaders(),
$requestBody->getBody(),
];
}
/**
* Makes the request to API and returns the result.
*
* @param DmmRequest $request
*
* @return DmmResponse
*
* @throws DmmSDKException
*/
public function sendRequest(DmmRequest $request)
{
if (get_class($request) === 'Dmm\DmmRequest') {
$request->validateCredentials();
}
list($url, $method, $headers, $body) = $this->prepareRequestMessage($request);
// Since file uploads can take a while, we need to give more time for uploads
$timeOut = static::DEFAULT_REQUEST_TIMEOUT;
// Should throw `DmmSDKException` exception on HTTP client error.
// Don't catch to allow it to bubble up.
$rawResponse = $this->httpClientHandler->send($url, $method, $body, $headers, $timeOut);
static::$requestCount++;
$returnResponse = new DmmResponse(
$request,
$rawResponse->getBody(),
$rawResponse->getHttpResponseCode(),
$rawResponse->getHeaders()
);
if ($returnResponse->isError()) {
throw $returnResponse->getThrownException();
}
return $returnResponse;
}
}