Skip to content

Commit

Permalink
Merge school endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
philipmclifton committed Sep 13, 2016
2 parents 1dd2d04 + 9a92ede commit 70c0665
Show file tree
Hide file tree
Showing 6 changed files with 267 additions and 119 deletions.
48 changes: 48 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,54 @@ foreach ($client->schools->all() as $school) {
// Display school name
echo $school->name . PHP_EOL;
}

```

### Pending Schools

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

foreach ($client->schools->pending() as $school) {
// Display school name
echo $school->name . PHP_EOL;
}
```

### Search Schools

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

// Search for schools with a postcode starting CB21
foreach ($client->schools->search(['postcode' => 'CB21']) as $school) {
// Display school name
echo $school->name . PHP_EOL;
}

// Search for schools with the establishment number = 6006
foreach ($client->schools->search(['establishment_number' => '6006']) as $school) {
// Display school name
echo $school->name . PHP_EOL;
}
```

### Request Access

Provide the school ID to request access to a school's data.

```php
$client = new \Wonde\Client('TOKEN_GOES_HERE');
$client->requestAccess('A0000000000');
```

### Revoke Access

Provide the school ID to access already approve or pending approval.

```php
$client = new \Wonde\Client('TOKEN_GOES_HERE');
$client->revokeAccess('A0000000000');
```

### Students
Expand Down
32 changes: 31 additions & 1 deletion src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Client
/**
* @var string
*/
const version = '1.1.0';
const version = '1.2.0';

/**
* Client constructor.
Expand All @@ -49,5 +49,35 @@ public function school($id)
{
return new Schools($this->token, $id);
}

/**
* Request access to the current school
*
* @return array
*/
public function requestAccess($schoolId)
{
$this->uri = $this->uri . $schoolId . '/request-access';

/** @var array $response */
$response = $this->post();

return $response;
}

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

/** @var array $response */
$response = $this->post();

return $response;
}
}

93 changes: 60 additions & 33 deletions src/Endpoints/BootstrapEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,29 +33,24 @@ public function __construct($token, $uri = false)
}

/**
* Get the default guzzle client
* Get all of resource
*
* @return Client
* @param array $includes
* @param array $parameters
* @return mixed|\Psr\Http\Message\ResponseInterface
*/
private function client()
public function all($includes = [], $parameters = [])
{
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
]
]
]);
if ( ! empty($includes)) {
$parameters['include'] = implode(',', $includes);
}

$uri = ! empty($parameters) ? $this->uri . '?' . http_build_query($parameters) : $this->uri;

$response = $this->getRequest($uri)->getBody()->getContents();
$decoded = json_decode($response);

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

/**
Expand All @@ -81,24 +76,29 @@ public function getUrl($url)
}

/**
* Get all of resource
* Get the default guzzle client
*
* @param array $includes
* @param array $parameters
* @return mixed|\Psr\Http\Message\ResponseInterface
* @return Client
*/
public function all($includes = [], $parameters = [])
private function client()
{
if ( ! empty($includes)) {
$parameters['include'] = implode(',', $includes);
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
]
]
]);
}

$uri = ! empty($parameters) ? $this->uri . '?' . http_build_query($parameters) : $this->uri;

$response = $this->getRequest($uri)->getBody()->getContents();
$decoded = json_decode($response);

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

/**
Expand All @@ -120,4 +120,31 @@ public function get($id, $includes = [], $parameters = [])

return $decoded->data;
}

/**
* Make a post request
*
* @param array $body
* @return array
*/
public function post($body = [])
{
return $this->postRequest($this->uri);
}

/**
* Make a post request and decode the response
*
* @param $endpoint
* @param array $body
* @return array
*/
private function postRequest($endpoint, $body = [])
{
$response = $this->client()->post(self::endpoint . $endpoint);
/** @var string $content */
$content = $response->getBody()->getContents();
/** @var array $decoded */
return json_decode($content);
}
}
29 changes: 29 additions & 0 deletions src/Endpoints/Schools.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?php namespace Wonde\Endpoints;

use GuzzleHttp\Psr7\Response;
use Wonde\Exceptions\InvalidInputException;

class Schools extends BootstrapEndpoint
{
/**
Expand Down Expand Up @@ -140,4 +143,30 @@ public function __construct($token, $id = false)
$this->students = new Students($token, $this->uri);
$this->subjects = new Subjects($token, $this->uri);
}

/**
* Return all pending schools
*
* @param array $includes
* @param array $parameters
* @return mixed|\Psr\Http\Message\ResponseInterface
*/
public function pending($includes = [], $parameters = [])
{
$this->uri = $this->uri . 'pending/';
return $this->all($includes, $parameters);
}

/**
* Search available schools
*
* @param array $includes
* @param array $parameters
* @return mixed|\Psr\Http\Message\ResponseInterface
*/
public function search($includes = [], $parameters = [])
{
$this->uri = $this->uri . 'all/';
return $this->all($includes, $parameters);
}
}
Loading

0 comments on commit 70c0665

Please sign in to comment.