-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathAbstractEndpoint.php
More file actions
90 lines (79 loc) · 2.57 KB
/
Copy pathAbstractEndpoint.php
File metadata and controls
90 lines (79 loc) · 2.57 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
<?php
namespace Alma\Client\Application\Endpoint;
use Alma\Client\Application\Request;
use Psr\Http\Client\ClientInterface;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Psr\Log\NullLogger;
abstract class AbstractEndpoint implements LoggerAwareInterface
{
use loggerAwareTrait;
/** @var ClientInterface */
protected ClientInterface $client;
/**
* Init the Endpoint
* @param ClientInterface $client The Client to use with the Endpoint
*/
public function __construct(ClientInterface $client)
{
$this->client = $client;
$this->logger = new NullLogger();
}
/**
* Create a Request
* @param string $method The HTTP verb of the Request
* @param string $uri The endpoint URI
* @param array $body The body of the Request
* @return Request The Request object
*/
private function createRequest(string $method, string $uri, array $body = []): Request {
$headers = [
'User-Agent' => $this->client->getConfig()->getUserAgentString(),
'Authorization' => ['Alma-Auth ' . $this->client->getConfig()->getApiKey()]
];
return new Request($method, $uri, $headers, json_encode($body));
}
/**
* Create a GET Request
*
* @param string $uri The endpoint URI
* @param array $queryParams The query parameters
* @return Request The Request object
*/
public function createGetRequest(string $uri, array $queryParams = []): Request {
$queryString = http_build_query($queryParams);
if ($queryString) {
$uri .= '?' . $queryString;
}
return $this->createRequest('GET', $uri);
}
/**
* Create a POST Request
*
* @param string $uri The endpoint URI
* @param array $body The body attributes
* @return Request The Request object
*/
public function createPostRequest(string $uri, array $body = []): Request {
return $this->createRequest('POST', $uri, $body);
}
/**
* Create a PUT Request
*
* @param string $uri The endpoint URI
* @param array $body The body attributes
* @return Request The Request object
*/
public function createPutRequest(string $uri, array $body = []): Request {
return $this->createRequest('PUT', $uri, $body);
}
/**
* Create de DELETE Request
*
* @param string $uri The endpoint URI
* @return Request The Request object
*/
public function createDeleteRequest(string $uri): Request {
return $this->createRequest('DELETE', $uri);
}
}