Skip to content

Commit

Permalink
Updates to follow the new API
Browse files Browse the repository at this point in the history
  • Loading branch information
tbleckert committed Mar 25, 2018
1 parent dec428b commit 7c6851d
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 83 deletions.
20 changes: 8 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,16 @@ Make sure the file is loaded, either by `require 'vendor/autoload.php'` if you a

```php
<?php
require 'vendor/autoload.php';

$clientId = 'ThisIsMyId';
$clientSecret = '123456789asdf';
$client = new Minutemailer\Minutemailer($clientId, $clientSecret);
require 'vendor/autoload.php';

// Data required
$contactListToken = 'asdf123';
$postData = [
'name' => 'Firstname Lastname',
'email' => '[email protected]'
];
$personalAccessToken = 'myAccessToken';

$response = $client->subscribe($contactListToken)->post($postData);
$client = new Minutemailer\Minutemailer($personalAccessToken);
$response = $client->contact_lists('123abc')->subscribe([
'name' => 'Firstname Lastname',
'email' => '[email protected]'
]);
```

Please note that both name and email are required. `$response` will be filled with the contact data if the request is valid. Otherwise, an error will be returned.
Email is required and name is optional. `$response` will be filled with the contact data if the request is valid. Otherwise, an error will be returned.
16 changes: 16 additions & 0 deletions src/Minutemailer/ContactLists.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Minutemailer;

class ContactLists extends RestAPI
{
public $endpoint = 'contactlists';

public function subscribe($data)
{
$this->segment = 'subscribe';
$response = $this->post($data);

return $response;
}
}
96 changes: 25 additions & 71 deletions src/Minutemailer/Minutemailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,76 +5,30 @@
class Minutemailer
{

private $baseUrl;
private $urlAccessToken;
private $grantType;
private $currentEndpoint;
private $version;
private $clientSecret;
private $clientId;
private $client;

public function __construct($clientId, $clientSecret)
{
$this->baseUrl = 'https://api.minutemailer.com';
$this->urlAccessToken = '/oauth/access_token';
$this->version = 'v1';
$this->grantType = 'password';
$this->clientId = $clientId;
$this->clientSecret = $clientSecret;
$this->client = new Client(['base_uri' => $this->baseUrl]);

$this->getAccessToken();
}

public function getAccessToken()
{
$response = $this->client->post($this->urlAccessToken, ['form_params' => [
'grant_type' => $this->grantType,
'client_id' => $this->clientId,
'client_secret' => $this->clientSecret
]]);

if ($response->getBody()) {
$json = json_decode($response->getBody());

$this->accessToken = $json->access_token;
}
}

public function contactLists($token = null)
{
$this->currentEndpoint = '/contactlists';

if ($token) {
$this->currentEndpoint += '/' . $token;
}

return $this;
}

public function subscribe($token)
{
$this->currentEndpoint = '/subscribe/' . $token;
return $this;
}

public function get($query = [])
{
$response = $this->client->get('/' . $this->version . $this->currentEndpoint, ['query' => $query]);
$response = json_decode($response->getBody());

return $response;
}

public function post($data)
{
$data['access_token'] = $this->accessToken;

$response = $this->client->post('/' . $this->version . $this->currentEndpoint, ['form_params' => $data]);
$response = json_decode($response->getBody());

return $response;
}
private $baseUrl;
private $currentEndpoint;
private $version;
private $clientSecret;
private $clientId;
private $client;

public function __construct($personalAccessToken)
{
$this->baseUrl = 'http://api.minutemailer.test';
$this->version = 'v1';
$this->personalAccessToken = $personalAccessToken;
$this->client = new Client([
'base_uri' => $this->baseUrl . '/' . $this->version . '/',
'headers' => [
'Accept' => 'application/json',
'Authorization' => 'Bearer ' . $this->personalAccessToken
]
]);
}

public function contact_lists($id)
{
return new ContactLists($this->client, $id);
}

}
62 changes: 62 additions & 0 deletions src/Minutemailer/RestAPI.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Minutemailer;

class RestAPI
{
public $endpoint;
public $client;
public $id;
public $segment;

public function __construct($client, $id = null)
{
$this->client = $client;
$this->id = $id;
}

protected function getEndpoint()
{
$endpoint = $this->endpoint;

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

if ($this->segment) {
$endpoint .= '/' . $this->segment;
}

return $endpoint;
}

/**
* Get resource
* @param array $query
* @return GuzzleHttp\Psr7\Response
*/
public function get($query = [])
{
$endpoint = $this->getEndpoint();

$response = $this->client->get($endpoint, ['query' => $query]);
$response = json_decode($response->getBody());

return $response;
}

/**
* Post data
* @param array $data
* @return GuzzleHttp\Psr7\Response
*/
public function post($data = [])
{
$endpoint = $this->getEndpoint();

$response = $this->client->post($endpoint, ['form_params' => $data]);
$response = json_decode($response->getBody());

return $response;
}
}

0 comments on commit 7c6851d

Please sign in to comment.