Skip to content

Commit

Permalink
Response logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Hodglim committed Feb 22, 2022
1 parent 4a8babb commit 7659ffa
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 36 deletions.
17 changes: 13 additions & 4 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,27 @@ class Client
/**
* @var string
*/
const version = '2.1.2';
const version = '2.2.2';

/**
* @var string
*/
private $logPath = '';

/**
* Client constructor.
*/
public function __construct($token)
public function __construct($token, $logPath = '')
{
if (empty($token) or !is_string($token)) {
if (empty($token) or !is_string($token)) {
throw new InvalidTokenException('Token string is required');
}

$this->token = $token;
$this->schools = new Schools($token);
$this->meta = new Meta($token);
$this->attendanceCodes = new AttendanceCodes($token);
$this->logPath = $logPath;
}

/**
Expand All @@ -56,7 +62,10 @@ public function __construct($token)
*/
public function school($id)
{
return new Schools($this->token, $id);
if(!empty($this->logPath)) {
$this->logPath .= DIRECTORY_SEPARATOR . $id;
}
return new Schools($this->token, $id, $this->logPath);
}

/**
Expand Down
34 changes: 31 additions & 3 deletions src/Endpoints/BootstrapEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,18 @@ class BootstrapEndpoint
*/
public $token;

/**
* @var string
*/
private $logPath;

/**
* BootstrapEndpoint constructor.
*/
public function __construct($token, $uri = false)
public function __construct($token, $uri = false, $logPath = '')
{
$this->token = $token;
$this->logPath = $logPath;

if ($uri) {
$this->uri = $uri . $this->uri;
Expand Down Expand Up @@ -111,7 +117,9 @@ public function all($includes = [], $parameters = [])
$response = $this->getRequest($uri)->getBody()->getContents();
$decoded = json_decode($response);

return new ResultIterator($decoded, $this->token);
$this->logResponse($this->logPath, $uri, $response);

return new ResultIterator($decoded, $this->token, $this->logPath);
}

/**
Expand Down Expand Up @@ -166,6 +174,8 @@ public function getWithMeta($id, $includes = [], $parameters = [])
$response = $this->getRequest($uri)->getBody()->getContents();
$decoded = json_decode($response);

$this->logResponse($this->logPath, $uri, $response);

return $decoded;
}

Expand Down Expand Up @@ -265,4 +275,22 @@ public function getEndpoint()
{
return "https://{$this->domain}/{$this->version}/";
}
}


/**
* Log response to filesystem
*
* @param string $logPath
* @param string $uri
* @param string $response
*/
protected function logResponse(string $logPath, string $uri, string $response) {
if(!empty($logPath)) {
if (!is_dir($logPath)) {
mkdir($logPath, 0777, true);
}
$filename = sha1($uri) . '.json';
file_put_contents($logPath . DIRECTORY_SEPARATOR . $filename, "URI: $uri\nRESPONSE:\n$response");
}
}
}
62 changes: 34 additions & 28 deletions src/Endpoints/Schools.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,46 +142,52 @@ class Schools extends BootstrapEndpoint
*/
public $exclusions;

/**
* @var string
*/
private $logPath;

/**
* Schools constructor.
*
* @param string $uri
*/
public function __construct($token, $id = false)
public function __construct($token, $id = false, $logPath = '')
{
$this->token = $token;
$this->logPath = $logPath;

if ($id) {
$this->uri = $this->uri . $id . '/';
}

$this->achievements = new Achievements($token, $this->uri);
$this->achievementsAttributes = new AchievementsAttributes($token, $this->uri);
$this->assessment = new Assessment($token, $this->uri);
$this->attendance = new Attendance($token, $this->uri);
$this->attendanceSummaries = new AttendanceSummaries($token, $this->uri);
$this->behaviours = new Behaviours($token, $this->uri);
$this->behavioursAttributes = new BehavioursAttributes($token, $this->uri);
$this->classes = new Classes($token, $this->uri);
$this->contacts = new Contacts($token, $this->uri);
$this->counts = new Counts($token, $this->uri);
$this->deletions = new Deletions($token, $this->uri);
$this->doctors = new Doctors($token, $this->uri);
$this->employees = new Employees($token, $this->uri);
$this->employeeAbsences = new EmployeeAbsences($token, $this->uri);
$this->events = new Events($token, $this->uri);
$this->exclusions = new Exclusions($token, $this->uri);
$this->groups = new Groups($token, $this->uri);
$this->lessons = new Lessons($token, $this->uri);
$this->lessonAttendance = new LessonAttendance($token, $this->uri);
$this->medicalConditions = new MedicalConditions($token, $this->uri);
$this->medicalEvents = new MedicalEvents($token, $this->uri);
$this->periods = new Periods($token, $this->uri);
$this->photos = new Photos($token, $this->uri);
$this->rooms = new Rooms($token, $this->uri);
$this->students = new Students($token, $this->uri);
$this->studentsPreAdmission = new StudentsPreAdmission($token, $this->uri);
$this->subjects = new Subjects($token, $this->uri);
$this->achievements = new Achievements($token, $this->uri, $this->logPath);
$this->achievementsAttributes = new AchievementsAttributes($token, $this->uri, $this->logPath);
$this->assessment = new Assessment($token, $this->uri, $this->logPath);
$this->attendance = new Attendance($token, $this->uri, $this->logPath);
$this->attendanceSummaries = new AttendanceSummaries($token, $this->uri, $this->logPath);
$this->behaviours = new Behaviours($token, $this->uri, $this->logPath);
$this->behavioursAttributes = new BehavioursAttributes($token, $this->uri, $this->logPath);
$this->classes = new Classes($token, $this->uri, $this->logPath);
$this->contacts = new Contacts($token, $this->uri, $this->logPath);
$this->counts = new Counts($token, $this->uri, $this->logPath);
$this->deletions = new Deletions($token, $this->uri, $this->logPath);
$this->doctors = new Doctors($token, $this->uri, $this->logPath);
$this->employees = new Employees($token, $this->uri, $this->logPath);
$this->employeeAbsences = new EmployeeAbsences($token, $this->uri, $this->logPath);
$this->events = new Events($token, $this->uri, $this->logPath);
$this->exclusions = new Exclusions($token, $this->uri, $this->logPath);
$this->groups = new Groups($token, $this->uri, $this->logPath);
$this->lessons = new Lessons($token, $this->uri, $this->logPath);
$this->lessonAttendance = new LessonAttendance($token, $this->uri, $this->logPath);
$this->medicalConditions = new MedicalConditions($token, $this->uri, $this->logPath);
$this->medicalEvents = new MedicalEvents($token, $this->uri, $this->logPath);
$this->periods = new Periods($token, $this->uri, $this->logPath);
$this->photos = new Photos($token, $this->uri, $this->logPath);
$this->rooms = new Rooms($token, $this->uri, $this->logPath);
$this->students = new Students($token, $this->uri, $this->logPath);
$this->studentsPreAdmission = new StudentsPreAdmission($token, $this->uri, $this->logPath);
$this->subjects = new Subjects($token, $this->uri, $this->logPath);
}

public function updateDomain($domain)
Expand Down
10 changes: 9 additions & 1 deletion src/ResultIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,17 @@ class ResultIterator extends BootstrapEndpoint implements \Iterator
*/
private $meta;

public function __construct($givenArray, $token)
/**
* @var string
*/
private $logPath;

public function __construct($givenArray, $token, $logPath = '')
{
$this->array = $givenArray->data;
$this->token = $token;
$this->meta = ! empty($givenArray->meta) ? $givenArray->meta : new \stdClass();
$this->logPath = $logPath;
}

function rewind()
Expand Down Expand Up @@ -57,6 +63,8 @@ function valid()
$nextResponse = $this->getUrl($this->meta->pagination->next)->getBody()->getContents();
$decoded = json_decode($nextResponse);

$this->logResponse($this->logPath, $this->meta->pagination->next, $nextResponse);

$this->meta = ! empty($decoded->meta) ? $decoded->meta : new \stdClass();
$this->array = $decoded->data;

Expand Down

0 comments on commit 7659ffa

Please sign in to comment.