Skip to content

Commit

Permalink
Merge pull request #23 from weber-gregoire/master
Browse files Browse the repository at this point in the history
Add support for idempotency token header
  • Loading branch information
bakura10 authored Jun 4, 2019
2 parents f6f6c09 + 28cc88e commit f49050e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/TradeGeckoClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -347,13 +347,22 @@ public function __call($method, $args = [])
}

$args = $args[0] ?? [];
$idempotencyToken;
if (isset($args['idempotency_token'])) {
$idempotencyToken = $args['idempotency_token'];
unset($args['idempotency_token']);
}

$args = array_merge($args, [
'@http' => [
'headers' => [
'Authorization' => "Bearer $this->accessToken"
]
]
]);
if (isset($idempotencyToken)) {
$args['@http']['headers']['Idempotency-Key'] = $idempotencyToken;
}

$command = $this->guzzleClient->getCommand(ucfirst($method), $args);
$result = $this->guzzleClient->execute($command);
Expand Down
38 changes: 38 additions & 0 deletions test/TradeGeckoClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,42 @@ public function testMagicMethod()

$this->assertEquals(['id' => 123], $payload);
}

public function testMagicMethodWithIdempotencyToken()
{
$serviceClient = $this->prophesize(GuzzleClient::class);
$client = new TradeGeckoClient('access_token_123', $serviceClient->reveal());

$expectedArgs = [
'@http' => [
'headers' => [
'Authorization' => 'Bearer access_token_123',
'Idempotency-Key' => 'uniqueToken'
]
]
];

$command = $this->prophesize(CommandInterface::class);
$command->getName()->shouldBeCalled()->willReturn('UpdateOrder');

$result = $this->prophesize(ToArrayInterface::class);
$result->toArray()->shouldBeCalled()->willReturn([
'order' => ['id' => 456],
]);

$serviceClient->getCommand('UpdateOrder', $expectedArgs)->shouldBeCalled()->willReturn($command->reveal());
$serviceClient->execute($command)->shouldBeCalled()->willReturn($result->reveal());

$description = $this->prophesize(DescriptionInterface::class);
$serviceClient->getDescription()->shouldBeCalled()->willReturn($description->reveal());

$operation = $this->prophesize(Operation::class);
$description->getOperation('UpdateOrder')->shouldBeCalled()->willReturn($operation->reveal());

$operation->getData('root_key')->shouldBeCalled()->willReturn('order');

$payload = $client->updateOrder(['idempotency_token' => 'uniqueToken']);

$this->assertEquals(['id' => 456], $payload);
}
}

0 comments on commit f49050e

Please sign in to comment.