Skip to content

Commit

Permalink
Merge pull request 1EdTech#53 from packbackbooks/better-logs
Browse files Browse the repository at this point in the history
PODB-520 Improve logging on the LtiServiceConnector
  • Loading branch information
dbhynds authored May 2, 2022
2 parents 84f8896 + 8e772ec commit 806625d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 21 deletions.
49 changes: 29 additions & 20 deletions src/LtiServiceConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class LtiServiceConnector implements ILtiServiceConnector
public const SYNC_GRADE_REQUEST = 1;
public const CREATE_LINEITEM_REQUEST = 2;
public const GET_LINEITEMS_REQUEST = 3;
public const AUTH_REQUEST = 3;

private $cache;
private $client;
Expand All @@ -41,6 +42,7 @@ public function __construct(
static::SYNC_GRADE_REQUEST => 'Syncing grade for this lti_user_id: ',
static::CREATE_LINEITEM_REQUEST => 'Creating lineitem: ',
static::GET_LINEITEMS_REQUEST => 'Getting lineitems: ',
static::AUTH_REQUEST => 'Authenticating: ',
];
}

Expand Down Expand Up @@ -84,9 +86,9 @@ public function getAccessToken(ILtiRegistration $registration, array $scopes)
$url = $registration->getAuthTokenUrl();

// Get Access
$response = $this->client->post($url, [
'form_params' => $authRequest,
]);
$request = new ServiceRequest(static::METHOD_POST, $url);
$request->setBody(json_encode($authRequest));
$response = $this->makeRequest($request);

$tokenData = $this->getResponseBody($response);

Expand All @@ -98,11 +100,32 @@ public function getAccessToken(ILtiRegistration $registration, array $scopes)

public function makeRequest(IServiceRequest $request)
{
return $this->client->request(
$response = $this->client->request(
$request->getMethod(),
$request->getUrl(),
$request->getPayload()
);

if ($this->debuggingMode) {
$this->logRequest(
static::AUTH_REQUEST,
$request,
$this->getResponseHeaders($response),
$this->getResponseBody($response)
);
}

return $response;
}

public function getResponseHeaders(Response $response): ?array
{
$responseHeaders = $response->getHeaders();
array_walk($responseHeaders, function (&$value) {
$value = $value[0];
});

return $responseHeaders;
}

public function getResponseBody(Response $response): ?array
Expand Down Expand Up @@ -143,24 +166,10 @@ public function makeServiceRequest(

throw $e;
}
$responseHeaders = $response->getHeaders();
array_walk($responseHeaders, function (&$value) {
$value = $value[0];
});
$responseBody = $this->getResponseBody($response);

if ($this->debuggingMode) {
$this->logRequest(
$requestType,
$request,
$responseHeaders,
$responseBody
);
}

return [
'headers' => $responseHeaders,
'body' => $responseBody,
'headers' => $this->getResponseHeaders($response),
'body' => $this->getResponseBody($response),
'status' => $response->getStatusCode(),
];
}
Expand Down
2 changes: 1 addition & 1 deletion tests/LtiServiceConnectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function testItGetsNewAccessToken()

$this->cache->shouldReceive('getAccessToken')
->once()->andReturn(false);
$this->client->shouldReceive('post')
$this->client->shouldReceive('request')
->once()->andReturn($this->response);
$this->response->shouldReceive('getBody')
->once()->andReturn($this->streamInterface);
Expand Down

0 comments on commit 806625d

Please sign in to comment.