From 351f3042d565ba6261b77ea9210af7077b62c3fb Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Wed, 8 Nov 2017 20:09:43 +0100 Subject: [PATCH] tests --- Makefile | 2 +- index.php | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 index.php diff --git a/Makefile b/Makefile index 8cf6ed9..19378ad 100644 --- a/Makefile +++ b/Makefile @@ -5,4 +5,4 @@ all: $(COMPOSER) install server: - $(PHP) -S 127.0.0.1:8080 + $(PHP) -S 127.0.0.1:8080 -t . diff --git a/index.php b/index.php new file mode 100644 index 0000000..8803a58 --- /dev/null +++ b/index.php @@ -0,0 +1,71 @@ +getStatusCode()); + + foreach ($response->getHeaders() as $header => $values) { + header(sprintf('%s: %s'."\n", $header, implode(', ', $values))); + } + + echo $response->getBody(); +} + +/** + * Does a request to the API of Bittrex. + * + * @param ServerRequest $request + * @param Client $client + * + * @return \GuzzleHttp\Psr7\Response + */ +function doRequest(ServerRequest $request, Client $client) +{ + $headers = []; + + foreach ($request->getHeaders() as $name => $value) { + $headers[$name] = $value[0]; + } + + $serverParams = $request->getServerParams(); + $url = sprintf('https://gitnet.fr%s', $serverParams['PATH_INFO'] ?? $serverParams['REQUEST_URI']); + + unset($headers['host']); + + return $client->request( + $request->getMethod(), + $url, + [ + 'headers' => $headers, + 'body' => $request->getBody(), + ] + ); +} + +try { + $apiResponse = doRequest( + ServerRequestFactory::fromGlobals(), + new Client() + ); +} catch (ClientException $e) { + $apiResponse = $e->getResponse(); +} + +return sendResponse(new Response( + $apiResponse->getBody(), + $apiResponse->getStatusCode(), + $apiResponse->getHeaders() +));