|
19 | 19 |
|
20 | 20 | // options for both clients
|
21 | 21 | $options = new HTTPOptions;
|
22 |
| -$options->ca_info = __DIR__.'/cacert.pem'; |
23 |
| -$options->sleep = 1; // GW2 API limit: 300 requests/minute |
| 22 | +$options->ca_info = __DIR__.'/cacert.pem'; |
| 23 | +$options->sleep = 750000; // GW2 API limit: 300 requests/minute |
| 24 | +$options->window_size = 5; |
| 25 | +$options->retries = 1; |
24 | 26 | #$options->user_agent = 'my fancy http client';
|
25 | 27 |
|
26 | 28 | $factory = new HTTPFactory;
|
|
47 | 49 | // the multi request handler
|
48 | 50 | $handler = new class () implements MultiResponseHandlerInterface{
|
49 | 51 |
|
50 |
| - public function handleResponse(ResponseInterface $response, RequestInterface $request, int $id, array $curl_info):?RequestInterface{ |
| 52 | + public function handleResponse( |
| 53 | + ResponseInterface $response, |
| 54 | + RequestInterface $request, |
| 55 | + int $id, |
| 56 | + array $curl_info, |
| 57 | + ):RequestInterface|null{ |
51 | 58 |
|
52 | 59 | // the API returns either 200 or 206 on OK responses
|
53 | 60 | // https://gitter.im/arenanet/api-cdi?at=5738e2c0ae26c1967f9eb4a0
|
54 |
| - if(in_array($response->getStatusCode(), [200, 206], true)){ |
55 |
| - $lang = $response->getHeaderLine('content-language'); |
56 |
| - |
57 |
| - // the response body is empty for some reason, we pretend that's fine and exit |
58 |
| - if($response->getBody()->getSize() === 0){ |
59 |
| - return null; |
60 |
| - } |
61 |
| - |
62 |
| - try{ |
63 |
| - $json = MessageUtil::decodeJSON($response); |
64 |
| - } |
65 |
| - catch(Throwable){ |
66 |
| - // maybe we didn't properly receive the data? let's try again |
67 |
| - return $request; |
68 |
| - } |
69 |
| - |
70 |
| - // create a file for each item in the response (ofc you'd rather put this in a DB) |
71 |
| - foreach($json as $item){ |
72 |
| - $file = $lang.'/'.$item->id; |
73 |
| - file_put_contents(__DIR__.'/'.$file.'.json', json_encode($item, (JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES))); |
74 |
| - |
75 |
| - echo $file.PHP_EOL; |
76 |
| - } |
77 |
| - |
78 |
| - // response ok, nothing to return |
| 61 | + if(!in_array($response->getStatusCode(), [200, 206], true)){ |
| 62 | + // return the failed request back to the stack |
| 63 | + return $request; |
| 64 | + } |
| 65 | + |
| 66 | + // the response body is empty for some reason, we pretend that's fine and exit |
| 67 | + if($response->getBody()->getSize() === 0){ |
79 | 68 | return null;
|
80 | 69 | }
|
81 | 70 |
|
82 |
| - // return the failed request back to the stack |
83 |
| - return $request; |
| 71 | + try{ |
| 72 | + $json = MessageUtil::decodeJSON($response); |
| 73 | + } |
| 74 | + catch(Throwable){ |
| 75 | + // maybe we didn't properly receive the data? let's try again |
| 76 | + return $request; |
| 77 | + } |
| 78 | + |
| 79 | + $lang = $response->getHeaderLine('content-language'); |
| 80 | + |
| 81 | + // create a file for each item in the response (ofc you'd rather put this in a DB) |
| 82 | + foreach($json as $item){ |
| 83 | + $file = $lang.'/'.$item->id; |
| 84 | + file_put_contents(__DIR__.'/'.$file.'.json', json_encode($item, (JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES))); |
| 85 | + |
| 86 | + echo $file.PHP_EOL; |
| 87 | + } |
| 88 | + |
| 89 | + // response ok, nothing to return |
| 90 | + return null; |
84 | 91 | }
|
85 | 92 |
|
86 | 93 | };
|
|
0 commit comments