forked from tomb1n0/generic-api-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.php
131 lines (103 loc) · 3.61 KB
/
example.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
<?php
use GuzzleHttp\Psr7\HttpFactory;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Tomb1n0\GenericApiClient\Http\Client;
use GuzzleHttp\Client as GuzzleHttpClient;
use Tomb1n0\GenericApiClient\Http\Response;
use Tomb1n0\GenericApiClient\Contracts\MiddlewareContract;
use Tomb1n0\GenericApiClient\Contracts\PaginationHandlerContract;
require_once 'vendor/autoload.php';
class PaginationHandler implements PaginationHandlerContract
{
public function hasNextPage(Response $response): bool
{
$total = $response->json('total');
$skip = $response->json('skip');
$limit = $response->json('limit');
if (is_null($total) || is_null($skip) || is_null($limit)) {
return false;
}
return $total > $skip + $limit;
}
public function getNextPage(Response $response): RequestInterface
{
$originalRequest = $response->getRequest();
$uri = $originalRequest->getUri();
$originalQuery = $uri->getQuery();
$originalQueryArray = [];
// Parse the query string into an array
parse_str($originalQuery, $originalQueryArray);
// Figure out what the skip should be
$skip = isset($originalQueryArray['skip']) ? $originalQueryArray['skip'] : 0;
$newSkip = $skip + $originalQueryArray['limit'];
// Build the new URI
$newUri = $uri->withQuery(
http_build_query(
array_merge($originalQueryArray, [
'skip' => $newSkip,
]),
),
);
// Return the original request but with the new URI, ergo new parameters
return $originalRequest->withUri($newUri);
}
}
class ProfilingMiddleware implements MiddlewareContract
{
public function handle(RequestInterface $request, callable $next): ResponseInterface
{
$start = microtime(true);
$response = $next($request);
var_dump('Done! Request took ' . microtime(true) - $start . ' seconds');
return $response;
}
}
class LoggerMiddleware implements MiddlewareContract
{
public function handle(RequestInterface $request, callable $next): ResponseInterface
{
var_dump("Performing {$request->getMethod()} request to {$request->getUri()}");
return $next($request);
}
}
class AuthenticationMiddleware implements MiddlewareContract
{
public function __construct(protected string $accessToken)
{
}
public function handle(RequestInterface $request, callable $next): ResponseInterface
{
$request = $request->withHeader('Authorization', 'Bearer ' . $this->accessToken);
return $next($request);
}
}
class BeforeMiddleware implements MiddlewareContract
{
public function handle(RequestInterface $request, callable $next): ResponseInterface
{
$request = $request->withHeader('X-Custom-Before-Header', 'Foo');
return $next($request);
}
}
class AfterMiddleware implements MiddlewareContract
{
public function handle(RequestInterface $request, callable $next): ResponseInterface
{
$response = $next($request);
$response = $response->withHeader('X-Custom-After-Header', 'Foo');
return $response;
}
}
$dummyJsonApi = new Client(
new GuzzleHttpClient(),
new HttpFactory(),
new HttpFactory(),
new HttpFactory(),
new HttpFactory(),
);
$dummyJsonApi = $dummyJsonApi->withBaseUrl('https://dummyjson.com')->stubResponse('https://dummyjson.com/products', []);
$response = $dummyJsonApi->json('GET', '/products');
if ($response->successful()) {
var_dump($response->json('products'));
}