1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Lakion package. |
5
|
|
|
* |
6
|
|
|
* (c) Lakion |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sylius\Api; |
13
|
|
|
|
14
|
|
|
use CommerceGuys\Guzzle\Oauth2\Oauth2Subscriber; |
15
|
|
|
use GuzzleHttp\Client as HttpClient; |
16
|
|
|
use GuzzleHttp\ClientInterface as HttpClientInterface; |
17
|
|
|
use GuzzleHttp\Event\SubscriberInterface; |
18
|
|
|
use GuzzleHttp\Post\PostBodyInterface; |
19
|
|
|
use GuzzleHttp\Url; |
20
|
|
|
use Sylius\Api\Factory\PostFileFactory; |
21
|
|
|
use Sylius\Api\Factory\PostFileFactoryInterface; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Sylius API client |
25
|
|
|
* |
26
|
|
|
* @author Michał Marcinkowski <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
class Client implements ClientInterface |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var Url $baseUrl |
32
|
|
|
*/ |
33
|
|
|
private $baseUrl; |
34
|
|
|
/** |
35
|
|
|
* @var HttpClientInterface $httpClient |
36
|
|
|
*/ |
37
|
|
|
private $httpClient; |
38
|
|
|
/** |
39
|
|
|
* @var PostFileFactoryInterface $postFileFactory |
40
|
|
|
*/ |
41
|
|
|
private $postFileFactory; |
42
|
|
|
|
43
|
|
|
public function __construct(HttpClientInterface $httpClient, PostFileFactoryInterface $postFileFactory = null) |
44
|
|
|
{ |
45
|
|
|
$this->httpClient = $httpClient; |
46
|
|
|
$this->postFileFactory = $postFileFactory ?: new PostFileFactory(); |
47
|
|
|
$this->baseUrl = Url::fromString($httpClient->getBaseUrl()); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc } |
52
|
|
|
*/ |
53
|
|
|
public function get($url, array $queryParameters = []) |
54
|
|
|
{ |
55
|
|
|
return $this->httpClient->get($url, ['query' => $queryParameters]); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritdoc } |
60
|
|
|
*/ |
61
|
|
|
public function patch($url, array $body) |
62
|
|
|
{ |
63
|
|
|
return $this->httpClient->patch($url, ['body' => $body]); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritdoc } |
68
|
|
|
*/ |
69
|
|
|
public function put($url, array $body) |
70
|
|
|
{ |
71
|
|
|
return $this->httpClient->put($url, ['body' => $body]); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritdoc } |
76
|
|
|
*/ |
77
|
|
|
public function delete($url) |
78
|
|
|
{ |
79
|
|
|
return $this->httpClient->delete($url); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* {@inheritdoc } |
84
|
|
|
*/ |
85
|
|
|
public function post($url, $body, array $files = array()) |
86
|
|
|
{ |
87
|
|
|
$request = $this->httpClient->createRequest('POST', $url, ['body' => $body]); |
88
|
|
|
/** @var PostBodyInterface $postBody */ |
89
|
|
|
$postBody = $request->getBody(); |
90
|
|
|
foreach ($files as $key => $filePath) { |
91
|
|
|
$file = $this->postFileFactory->create($key, $filePath); |
92
|
|
|
$postBody->addFile($file); |
93
|
|
|
} |
94
|
|
|
$response = $this->httpClient->send($request); |
95
|
|
|
|
96
|
|
|
return $response; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* {@inheritdoc } |
101
|
|
|
*/ |
102
|
|
|
public function getSchemeAndHost() |
103
|
|
|
{ |
104
|
|
|
return sprintf('%s://%s', $this->baseUrl->getScheme(), $this->baseUrl->getHost()); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param string $url |
109
|
|
|
* @param null|SubscriberInterface $subscriber |
110
|
|
|
* @param array $options |
111
|
|
|
* @return Client |
112
|
|
|
*/ |
113
|
|
|
public static function createFromUrl($url, SubscriberInterface $subscriber = null, array $options = []) |
114
|
|
|
{ |
115
|
|
|
$options['base_url'] = $url; |
116
|
|
|
self::resolveDefaults($options, $subscriber); |
117
|
|
|
|
118
|
|
|
return new self(new HttpClient($options)); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
private static function resolveDefaults(array &$options, SubscriberInterface $subscriber = null) |
122
|
|
|
{ |
123
|
|
|
$options['defaults']['headers']['User-Agent'] = isset($options['defaults']['headers']['User-Agent']) ? $options['defaults']['headers']['User-Agent'] : 'SyliusApi/0.1'; |
124
|
|
|
$options['defaults']['headers']['Accept'] = isset($options['defaults']['headers']['Accept']) ? $options['defaults']['headers']['Accept'] : 'application/json'; |
125
|
|
|
$options['defaults']['exceptions'] = isset($options['defaults']['exceptions']) ? $options['defaults']['exceptions'] : false; |
126
|
|
|
|
127
|
|
|
if ($subscriber) { |
128
|
|
|
$options['defaults']['subscribers'][] = $subscriber; |
129
|
|
|
if ($subscriber instanceof Oauth2Subscriber) { |
130
|
|
|
$options['defaults']['auth'] = 'oauth2'; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|