Skip to content
Merged
Show file tree
Hide file tree
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
54 changes: 54 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,34 @@ foreach ($school->attendance->all() as $attendance) {
}
```

### POST Attendance

```php
$client = new \Wonde\Client('TOKEN_GOES_HERE');

// Initiate a new register
$register = new \Wonde\Writeback\SessionRegister();

// Initiate a new attendance record
$attendance = new \Wonde\Writeback\SessionAttendanceRecord();

// Set fields
$attendance->setStudentId('STUDENT_ID_GOES_HERE');
$attendance->setDate('2017-01-01');
$attendance->setSession('AM'); // AM or PM
$attendance->setAttendanceCodeId('ATTENDANCE_CODE_ID_GOES_HERE');
$attendance->setComment('Comment here.');

// Add attendance mark to register
$register->add($attendance);

// Save the session register
$result = $school->attendance()->sessionRegister($register);

// Writeback id is part of the response
echo $result->writeback_id;
```

### Attendance Codes

```php
Expand Down Expand Up @@ -333,6 +361,32 @@ foreach ($school->lessonAttendance->all() as $lessonAttendance) {
}
```

### POST Lesson Attendance

```php
$client = new \Wonde\Client('TOKEN_GOES_HERE');

// Initiate a new register
$register = new \Wonde\Writeback\LessonRegister();

// Initiate a new attendance record
$attendance = new \Wonde\Writeback\LessonAttendanceRecord();

// Set fields
$attendance->setStudentId('STUDENT_ID_GOES_HERE');
$attendance->setLessonId('LESSON_ID_GOES_HERE');
$attendance->setAttendanceCodeId('ATTENDANCE_CODE_ID_GOES_HERE');

// Add attendance mark to register
$register->add($attendance);

// Save the lesson register
$result = $school->lessonAttendance()->lessonRegister($register);

// Writeback id is part of the response
echo $result->writeback_id;
```

### Medical Conditions

```php
Expand Down
22 changes: 8 additions & 14 deletions src/Client.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php namespace Wonde;

use Wonde\Endpoints\AttendanceCodes;
use Wonde\Endpoints\BootstrapEndpoint;
use Wonde\Endpoints\Schools;
use Wonde\Exceptions\InvalidTokenException;

Expand All @@ -9,7 +10,6 @@
*/
class Client
{

/**
* @var AttendanceCodes
*/
Expand All @@ -23,7 +23,7 @@ class Client
/**
* @var string
*/
const version = '1.2.0';
const version = '1.3.0';

/**
* Client constructor.
Expand Down Expand Up @@ -53,31 +53,25 @@ public function school($id)
/**
* Request access to the current school
*
* @return array
* @return \stdClass
*/
public function requestAccess($schoolId)
{
$this->uri = $this->uri . $schoolId . '/request-access';

/** @var array $response */
$response = $this->post();
$uri = 'schools/' . $schoolId . '/request-access';

return $response;
return (new BootstrapEndpoint($this->token, $uri))->post();
}

/**
* Revoke access to the current school
*
* @return array
* @return \stdClass
*/
public function revokeAccess($schoolId)
{
$this->uri = $this->uri . $schoolId . '/revoke-access';

/** @var array $response */
$response = $this->post();
$uri = 'schools/' . $schoolId . '/revoke-access';

return $response;
return (new BootstrapEndpoint($this->token, $uri))->delete();
}
}

13 changes: 13 additions & 0 deletions src/Endpoints/Attendance.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
<?php namespace Wonde\Endpoints;

use Wonde\Writeback\SessionRegister;

class Attendance extends BootstrapEndpoint
{
/**
* @var string
*/
public $uri = 'attendance/session';

/**
* Session Register
*
* @param SessionRegister $register
* @return \stdClass
*/
public function sessionRegister(SessionRegister $register)
{
return $this->post($register);
}
}
164 changes: 128 additions & 36 deletions src/Endpoints/BootstrapEndpoint.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php namespace Wonde\Endpoints;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use Wonde\Exceptions\ValidationError;
use Wonde\ResultIterator;

class BootstrapEndpoint
Expand Down Expand Up @@ -32,6 +34,52 @@ public function __construct($token, $uri = false)
}
}

/**
* Get the default guzzle client
*
* @return Client
*/
private function client()
{
if ((float) Client::VERSION >= 6) {
return new Client([
'headers' => [
'Authorization' => 'Basic ' . base64_encode($this->token . ':'),
'User-Agent' => 'wonde-php-client-' . \Wonde\Client::version
]
]);
} else {
return new Client([
'defaults' => [
'headers' => [
'Authorization' => 'Basic ' . base64_encode($this->token . ':'),
'User-Agent' => 'wonde-php-client-' . \Wonde\Client::version
]
]
]);
}
}

/**
* Throw an error
*
* @param ClientException $exception
* @return null
* @throws ValidationError
*/
private function throwError(ClientException $exception)
{
if ($exception->getResponse()->getStatusCode() === 422) {

// Status code 422 is a validation error
$validationError = new ValidationError('Validation has failed');
$validationError->setErrors(json_decode($exception->getResponse()->getBody()->getContents()));
throw $validationError;
} else {
throw $exception;
}
}

/**
* Get all of resource
*
Expand Down Expand Up @@ -75,32 +123,6 @@ public function getUrl($url)
return $this->client()->get($url);
}

/**
* Get the default guzzle client
*
* @return Client
*/
private function client()
{
if ((float) Client::VERSION >= 6) {
return new Client([
'headers' => [
'Authorization' => 'Basic ' . base64_encode($this->token . ':'),
'User-Agent' => 'wonde-php-client-' . \Wonde\Client::version
]
]);
} else {
return new Client([
'defaults' => [
'headers' => [
'Authorization' => 'Basic ' . base64_encode($this->token . ':'),
'User-Agent' => 'wonde-php-client-' . \Wonde\Client::version
]
]
]);
}
}

/**
* Get single resource
*
Expand All @@ -124,27 +146,97 @@ public function get($id, $includes = [], $parameters = [])
/**
* Make a post request
*
* @param $endpoint
* @param array $body
* @return array
* @return mixed|\Psr\Http\Message\ResponseInterface
*/
public function post($body = [])
public function postRequest($endpoint, $body = [])
{
return $this->postRequest($this->uri);
return $this->postUrl(self::endpoint . $endpoint, $body);
}

/**
* Make a post request to url
*
* @param $url
* @param array $body
* @return mixed|\Psr\Http\Message\ResponseInterface
*/
private function postUrl($url, $body = [])
{
return $this->client()->post($url, $body);
}

/**
* Make a post request and decode the response
*
* @param array $body
* @return \stdClass
*/
public function post($body = [])
{
$body = ['body' => json_encode($body)];
$body['headers']['Content-Type'] = 'application/json';

try {
$post = $this->postRequest($this->uri, $body);
} catch ( ClientException $exception ) {
return $this->throwError($exception);
}

$response = $post->getBody()->getContents();

$decoded = json_decode($response);

return $decoded;
}


/**
* Make a delete request
*
* @param $endpoint
* @param array $body
* @return array
* @return mixed|\Psr\Http\Message\ResponseInterface
*/
public function deleteRequest($endpoint, $body = [])
{
return $this->deleteUrl(self::endpoint . $endpoint, $body);
}

/**
* Make a delete request to url
*
* @param $url
* @param array $body
* @return mixed|\Psr\Http\Message\ResponseInterface
*/
private function postRequest($endpoint, $body = [])
private function deleteUrl($url, $body = [])
{
$response = $this->client()->post(self::endpoint . $endpoint);
/** @var string $content */
$content = $response->getBody()->getContents();
/** @var array $decoded */
return json_decode($content);
return $this->client()->delete($url, $body);
}

/**
* Make a delete request and decode the response
*
* @param array $body
* @return \stdClass
*/
public function delete($body = [])
{
$body = ['body' => json_encode($body)];
$body['headers']['Content-Type'] = 'application/json';

try {
$delete = $this->deleteRequest($this->uri, $body);
} catch ( ClientException $exception ) {
return $this->throwError($exception);
}

$response = $delete->getBody()->getContents();

$decoded = json_decode($response);

return $decoded;
}
}
14 changes: 14 additions & 0 deletions src/Endpoints/LessonAttendance.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
<?php namespace Wonde\Endpoints;

use Wonde\Writeback\LessonRegister;

class LessonAttendance extends BootstrapEndpoint
{
/**
* @var string
*/
public $uri = 'attendance/lesson';

/**
* Lesson Register
*
* @param LessonRegister $register
* @return \stdClass
*/
public function lessonRegister(LessonRegister $lessonRegister){

return $this->post($lessonRegister);

}
}
Loading