Skip to content

Commit

Permalink
Removed ApiException; Added response helper functions
Browse files Browse the repository at this point in the history
  • Loading branch information
karser committed Apr 12, 2021
1 parent 0d4ee76 commit 5cb589f
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 78 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,31 +50,31 @@ $response = $client->request('POST', '/shipments', ['json' => [
'workspace' => '/workspaces/'.$workspaceId,
'trackingNumber' => 'TN-1',
]]);
$shipment = TrackMageClient::item($response);
$response = $client->request('GET', '/workspaces/'.self::$workspaceId.'/shipments');
$data = json_decode($response->getBody()->getContents(), true);
$shipments = TrackMageClient::collection($response);
```

### Get workspaces list

```
$response = $client->request('GET', '/workspaces/'.$workspaceId.'/shipments');
$data = json_decode($response->getBody()->getContents(), true);
$workspaces = TrackMageClient::collection($response);
```

### Get carriers list

```
$response = $client->request('GET', '/public/carriers');
$data = json_decode($response->getBody()->getContents(), true);
$carriers = TrackMageClient::collection($response);
```

### Create webhook

Here is [the webhook handler example](examples/webhook-handler.php) that you need to make accessible on your side.

```php
<?php
```
$workflow = [
'type' => 'webhook',
'period' => 'immediately',
Expand All @@ -91,8 +91,8 @@ $workflow = [
],
];
$response = $client->request('POST', '/workflows', ['json' => $workflow]);
$data = json_decode($response->getBody()->getContents(), true);
$workflowId = $data['id'];
$workflow = TrackMageClient::item($response);
$workflowId = $workflow['id'];
```

## Tests
Expand Down
29 changes: 8 additions & 21 deletions src/AddAuthHeadersMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
namespace TrackMage\Client;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use TrackMage\Client\Swagger\ApiException;
use Psr\Http\Message\RequestInterface;
use GuzzleHttp\Psr7;

Expand Down Expand Up @@ -53,7 +51,6 @@ public static function get($clientId, $clientSecret, $accessToken, $host)
* @param RequestInterface $request
* @param array $options
* @return mixed
* @throws ApiException
*/
public function __invoke(RequestInterface $request, array $options)
{
Expand All @@ -76,7 +73,7 @@ public function __invoke(RequestInterface $request, array $options)
private function addGeneralHeaders(RequestInterface $request)
{
$headers = [
'Accept' => 'application/json',
'Accept' => 'application/ld+json',
];
$modify['set_headers'] = $headers;

Expand All @@ -102,27 +99,17 @@ private function addAuthHeaders(RequestInterface $request, $token)
* @param string $clientId
* @param string $clientSecret
* @return string
* @throws ApiException
*/
private function getAccessToken($clientId, $clientSecret)
{
$client = new Client();
try {
$response = $client->get($this->host.'/oauth/v2/token', [
'query' => [
'client_id' => $clientId,
'client_secret' => $clientSecret,
'grant_type' => 'client_credentials',
],
]);
} catch (ClientException $e) {
throw new ApiException(
'Authorization error',
$e->getRequest(),
$e->getResponse(),
$e
);
}
$response = $client->get($this->host.'/oauth/v2/token', [
'query' => [
'client_id' => $clientId,
'client_secret' => $clientSecret,
'grant_type' => 'client_credentials',
],
]);
$data = json_decode($response->getBody()->getContents(), true);

return $data['access_token'];
Expand Down
10 changes: 0 additions & 10 deletions src/Swagger/ApiException.php

This file was deleted.

18 changes: 18 additions & 0 deletions src/TrackMageClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use GuzzleHttp\MessageFormatter;
use GuzzleHttp\Middleware;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
use Psr\Log\NullLogger;
Expand Down Expand Up @@ -99,6 +100,23 @@ public function getConfig($option = null)
return $this->guzzleClient->getConfig($option);
}

/**
* @return array
*/
public static function collection(ResponseInterface $response)
{
$data = json_decode($response->getBody()->getContents(), true);
return $data['hydra:member'];
}

/**
* @return array
*/
public static function item(ResponseInterface $response)
{
return json_decode($response->getBody()->getContents(), true);
}

private function initGuzzleClient()
{
$stack = HandlerStack::create(new CurlHandler());
Expand Down
58 changes: 18 additions & 40 deletions test/TrackMageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace TrackMage\Client;

use GuzzleHttp\Exception\ClientException;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Logger\ConsoleLogger;
use Symfony\Component\Console\Output\ConsoleOutput;
Expand All @@ -15,9 +16,6 @@ final class TrackMageTest extends TestCase
/** @var int */
private static $workspaceId;

/**
* @throws Swagger\ApiException
*/
public static function setUpBeforeClass()
{
$host = getenv('API_HOST');
Expand All @@ -31,13 +29,13 @@ public static function setUpBeforeClass()

// confirm email address
$response = $client->request('GET', '/public/user-confirmation-link/'.$email);
$data = json_decode($response->getBody()->getContents(), true);
$data = TrackMageClient::item($response);
$parts = explode('/', $data['link']);
$code = end($parts);

$response = $client->request('POST', '/security/verify-email', ['json' => ['code' => $code]]);
self::assertSame(200, $response->getStatusCode());
$data = json_decode($response->getBody()->getContents(), true);
$data = TrackMageClient::item($response);
$token = $data['token']['access_token'];
self::assertNotNull($token);
$userId = $data['user']['id'];
Expand All @@ -64,12 +62,12 @@ public static function setUpBeforeClass()
]
);
self::assertEquals(201, $response->getStatusCode());
$data = json_decode($response->getBody()->getContents(), true);
$data = TrackMageClient::item($response);
$workspaceId = $data['id'];

$response = $client->request('GET', '/users/'.$userId);
self::assertSame(200, $response->getStatusCode());
$data = json_decode($response->getBody()->getContents(), true);
$data = TrackMageClient::item($response);

self::assertSame($email, $data['email']);
self::assertTrue($data['emailVerified']);
Expand All @@ -79,41 +77,32 @@ public static function setUpBeforeClass()
self::$workspaceId = $workspaceId;
}

/**
* @expectedException \TrackMage\Client\Swagger\ApiException
* @expectedExceptionMessage Authorization error
*/
public function testInvalidCredentials()
{
$this->expectException(ClientException::class);
$this->expectExceptionMessageRegExp('/The client credentials are invalid/');
$client = new TrackMageClient('fake', 'fake');
$client->request('GET', '/workspaces');
}

/**
* @throws Swagger\ApiException
*/
public function testWorkspaces()
{
$response = self::$client->request('GET', '/workspaces');
self::assertEquals(200, $response->getStatusCode());
$workspaces = json_decode($response->getBody()->getContents(), true);

$workspaces = TrackMageClient::collection($response);
self::assertCount(1, $workspaces);
$ws = current($workspaces);
self::assertSame('my company', $ws['title']);
}

/**
* @throws Swagger\ApiException
*/
public function testClientAuth()
{
$response = self::$client->request('POST', '/oauth_clients', ['json' => [
'name' => 'test',
'redirectUris' => ['https://localhost'],
]]);
self::assertSame(201, $response->getStatusCode());
$data = json_decode($response->getBody()->getContents(), true);
$data = TrackMageClient::item($response);
$clientId = $data['publicId'];
$clientSecret = $data['secret'];

Expand All @@ -124,33 +113,27 @@ public function testClientAuth()
self::assertSame(200, $response->getStatusCode());
}

/**
* @throws Swagger\ApiException
*/
public function testShipments()
{
$response = self::$client->request('GET', '/workspaces/'.self::$workspaceId.'/shipments');
self::assertSame(200, $response->getStatusCode());
$data = json_decode($response->getBody()->getContents(), true);
self::assertEmpty($data);
$shipments = TrackMageClient::collection($response);
self::assertEmpty($shipments);

$response = self::$client->request('POST', '/shipments', ['json' => [
'workspace' => '/workspaces/'.self::$workspaceId,
'trackingNumber' => 'TN-1',
]]);
self::assertSame(201, $response->getStatusCode());
$data = json_decode($response->getBody()->getContents(), true);
$data = TrackMageClient::item($response);
self::assertEquals('TN-1', $data['trackingNumber']);

$response = self::$client->request('GET', '/workspaces/'.self::$workspaceId.'/shipments');
self::assertSame(200, $response->getStatusCode());
$data = json_decode($response->getBody()->getContents(), true);
self::assertCount(1, $data);
$shipments = TrackMageClient::collection($response);
self::assertCount(1, $shipments);
}

/**
* @throws Swagger\ApiException
*/
public function testCreateWebhook()
{
// create workspace
Expand All @@ -171,21 +154,16 @@ public function testCreateWebhook()
];
$response = self::$client->request('POST', '/workflows', ['json' => $workflow]);
//THEN
$contents = $response->getBody()->getContents();
self::assertEquals(201, $response->getStatusCode(), $contents);
$data = json_decode($contents, true);
self::assertEquals(201, $response->getStatusCode());
$data = TrackMageClient::item($response);
self::assertArraySubset($workflow, $data);
}

/**
* @throws Swagger\ApiException
*/
public function testCarriers()
{
$response = self::$client->request('GET', '/public/carriers');
$contents = $response->getBody()->getContents();
self::assertEquals(200, $response->getStatusCode(), $contents);
$data = json_decode($contents, true);
self::assertEquals(200, $response->getStatusCode());
$data = TrackMageClient::collection($response);
self::assertTrue(is_array($data));
self::assertNotEmpty($data);
}
Expand Down

0 comments on commit 5cb589f

Please sign in to comment.