Skip to content
This repository has been archived by the owner on Jul 17, 2019. It is now read-only.

Feature/parse payload #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions src/Jeskew/Guzzle/Plugin/Hawk.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,28 @@
use GuzzleHttp\Event\BeforeEvent;
use GuzzleHttp\Event\EmitterInterface;
use GuzzleHttp\Event\SubscriberInterface;
use GuzzleHttp\Message\RequestInterface;

class Hawk implements SubscriberInterface
{
private $key;
private $secret;
private $offset;
private $parsePayload;
private $extHeaders;

public function __construct($key, $secret, $offset = 0)
{
public function __construct(
$key,
$secret,
$offset = 0,
$parsePayload = false,
$extHeaders = []
) {
$this->key = $key;
$this->secret = $secret;
$this->offset = $offset;
$this->parsePayload = $parsePayload;
$this->extHeaders = $extHeaders;
}

public function getEvents()
Expand All @@ -38,7 +48,10 @@ public function signRequest(BeforeEvent $event)
$this->secret,
$request->getUrl(),
$request->getMethod(),
$this->offset
$this->offset,
$this->gatherExtHeaders($request),
$this->parsePayload ? (string) $request->getBody() : '',
$this->parsePayload ? $request->getHeader('content-type') : ''
);

$request->setHeader(
Expand Down Expand Up @@ -73,6 +86,18 @@ public function generateHawkRequest(
return $request;
}

private function gatherExtHeaders(RequestInterface $request)
{
$headers = array_intersect_key(
$request->getHeaders(),
array_flip($this->extHeaders)
);

ksort($headers);

return $headers;
}

private function buildClient($offset)
{
$builder = ClientBuilder::create();
Expand Down