/** * This checks the response with the given index * * @param $options * @param int $index */ public function assertRequestIs($options, $index = 0) { $this->assertArrayHasKey($index, $this->mockedTransactionsContainer, 'Should have made an API call.'); $transaction = $this->mockedTransactionsContainer[$index]; $request = $transaction['request']; $response = $transaction['response']; $options = array_merge(['statusCode' => 200, 'headers' => ['Accept' => 'application/json', 'Content-Type' => 'application/json']], $options); $this->assertEquals($options['statusCode'], $response->getStatusCode()); if (isset($options['multipart'])) { $body = $request->getBody(); $this->assertInstanceOf(MultipartStream::class, $body); $this->assertGreaterThan(0, $body->getSize()); $this->assertNotEmpty($header = $request->getHeaderLine('Content-Type')); $this->assertContains('multipart/form-data', $header); unset($options['headers']['Content-Type']); } if (isset($options['file'])) { $body = $request->getBody(); $this->assertInstanceOf(LazyOpenStream::class, $body); $this->assertGreaterThan(0, $body->getSize()); $this->assertEquals($options['file'], $body->getMetadata('uri')); $this->assertNotEmpty($header = $request->getHeaderLine('Content-Type')); $this->assertEquals('application/binary', $header); unset($options['headers']['Content-Type']); } if (isset($options['headers']) && is_array($options['headers'])) { foreach ($options['headers'] as $headerKey => $value) { if ($value) { $this->assertNotEmpty($header = $request->getHeaderLine($headerKey)); $this->assertEquals($value, $header); } } } if (isset($options['method'])) { $this->assertEquals($options['method'], $request->getMethod()); } if (isset($options['endpoint'])) { // Truncate the base path from the target $apiBasePath = "/{$this->client->getApiBasePath()}"; $endpoint = preg_replace('/^' . preg_quote($apiBasePath, '/') . '/', '', $request->getUri()->getPath()); $this->assertEquals($options['endpoint'], $endpoint); } if (isset($options['queryParams'])) { $expectedQueryParams = urldecode(http_build_query($options['queryParams'])); $this->assertEquals($expectedQueryParams, $request->getUri()->getQuery()); } if (isset($options['postFields'])) { $this->assertEquals(json_encode($options['postFields']), $request->getBody()->getContents()); } if (isset($options['requestUri'])) { $this->assertEquals($options['requestUri'], $request->getUri()->__toString()); } }
/** * Use the send method to call every endpoint except for oauth/tokens * * @param HttpClient $client * @param string $endPoint E.g. "/tickets.json" * @param array $options * Available options are listed below: * array $queryParams Array of unencoded key-value pairs, e.g. ["ids" => "1,2,3,4"] * array $postFields Array of unencoded key-value pairs, e.g. ["filename" => "blah.png"] * string $method "GET", "POST", etc. Default is GET. * string $contentType Default is "application/json" * * @return \stdClass | null The response body, parsed from JSON into an object. Also returns null if something went wrong * @throws ApiResponseException * @throws AuthException */ public static function send(HttpClient $client, $endPoint, $options = []) { $options = array_merge(['method' => 'GET', 'contentType' => 'application/json', 'postFields' => null, 'queryParams' => null], $options); $headers = array_merge(['Accept' => 'application/json', 'Content-Type' => $options['contentType'], 'User-Agent' => $client->getUserAgent()], $client->getHeaders()); $request = new Request($options['method'], $client->getApiUrl() . $client->getApiBasePath() . $endPoint, $headers); $requestOptions = []; if (!empty($options['multipart'])) { $request = $request->withoutHeader('Content-Type'); $requestOptions['multipart'] = $options['multipart']; } elseif (!empty($options['postFields'])) { $request = $request->withBody(\GuzzleHttp\Psr7\stream_for(json_encode($options['postFields']))); } elseif (!empty($options['file'])) { if (is_file($options['file'])) { $fileStream = new LazyOpenStream($options['file'], 'r'); $request = $request->withBody($fileStream); } } if (!empty($options['queryParams'])) { foreach ($options['queryParams'] as $queryKey => $queryValue) { $uri = $request->getUri(); $uri = $uri->withQueryValue($uri, $queryKey, $queryValue); $request = $request->withUri($uri, true); } } // \RockstarGames\Cake\Log\Log::debug($request); // \RockstarGames\Cake\Log\Log::debug($options); try { list($request, $requestOptions) = $client->getAuth()->prepareRequest($request, $requestOptions); $response = $client->guzzle->send($request, $requestOptions); } catch (RequestException $e) { $requestException = RequestException::create($e->getRequest(), $e->getResponse()); throw new ApiResponseException($requestException); } finally { $client->setDebug($request->getHeaders(), $request->getBody()->getContents(), isset($response) ? $response->getStatusCode() : null, isset($response) ? $response->getHeaders() : null, isset($e) ? $e : null); $request->getBody()->rewind(); } if (isset($file)) { fclose($file); } $client->setSideload(null); return json_decode($response->getBody()->getContents()); }