Skip to content

Commit

Permalink
making the simple api work with bearer auth
Browse files Browse the repository at this point in the history
  • Loading branch information
Enrique Perez committed Aug 1, 2017
1 parent 53f023a commit 7256959
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 26 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
vendor/
composer.lock
.DS_Store
.DS_Store
.env
test.php
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"type": "library",
"require": {
"php": ">=5.4",
"guzzlehttp/guzzle": "^6.3"
"guzzlehttp/guzzle": "^6.3",
"vlucas/phpdotenv": "^2.4"
},
"require-dev": {
"phpunit/phpunit": "5.2.*"
Expand Down
4 changes: 2 additions & 2 deletions src/Post.php → src/Entities/Post.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

namespace GhostIO;
namespace GhostIO\Entities;

use GhostIO\JsonSerializableObject;
use GhostIO\Utils\JsonSerializableObject;

/**
* Class that contains the values of a Post
Expand Down
4 changes: 2 additions & 2 deletions src/Tag.php → src/Entities/Tag.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

namespace GhostIO;
namespace GhostIO\Entities;

use GhostIO\JsonSerializableObject;
use GhostIO\Utils\JsonSerializableObject;

/**
* A tag
Expand Down
4 changes: 2 additions & 2 deletions src/User.php → src/Entities/User.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

namespace GhostIO;
namespace GhostIO\Entities;

use GhostIO\JsonSerializableObject;
use GhostIO\Utils\JsonSerializableObject;

/**
* Class that contains the values of a User
Expand Down
72 changes: 58 additions & 14 deletions src/GhostIO.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,74 @@

namespace GhostIO;

class GhostIO {
class GhostIO
{

protected $username;
protected $password;
protected $clientId;
protected $clientSecret;
protected $apiUrl;

protected $httpClient;

public function __construct()
protected $token;

public function __construct($url, $username, $password, $clientId, $clientSecret)
{
$this->httpClient = new GuzzleHttp\Client();
$this->httpClient = new \GuzzleHttp\Client([
'base_uri' => $url . '/ghost/api/v0.1/'
]);

// initialize global properties
$this->username = $username;
$this->password = $password;
$this->clientId = $clientId;
$this->clientSecret = $clientSecret;

$this->authenticate();
}

public function getPosts()
protected function authenticate()
{
// This will get you the posts that are on ghost.io
$res = $httpClient->request('GET', $this->apiUrl . 'posts');

echo $res->getStatusCode();
// "200"
echo $res->getHeader('content-type');
// 'application/json; charset=utf8'
echo $res->getBody();
// {"type":"User"...'
$response = $this->httpClient->request('POST', 'authentication/token', [
'form_params' => [
'grant_type' => 'password',
'username' => $this->username,
'password' => $this->password,
'client_id' => $this->clientId,
'client_secret' => $this->clientSecret
]
]);

$response_data = json_decode($response->getBody()->getContents());
if (!$response_data->access_token) {
throw new Exception('Unable to get access token.');

}
$this->token = $response_data->access_token;
}


public function getAllPosts()
{

$response = $this->httpClient->request('GET', 'posts', [
'headers' => [
'Authorization' => 'Bearer ' . $this->token,
'Content-Type' => 'application/json'
],
'query' => [
'fields' => "title,url"
]
]);

$response_data = json_decode($response->getBody()->getContents());
if (!$response_data->posts) {
throw new Exception('Unable to get the posts.');

}
return $response_data->posts;
}


}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace GhostIO;
namespace GhostIO\Utils;

use JsonSerializable;

Expand Down
2 changes: 1 addition & 1 deletion tests/PostTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

use GhostIO\Post;
use GhostIO\Entities\Post;

/**
* Corresponding Class to test Post class
Expand Down
2 changes: 1 addition & 1 deletion tests/TagTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

use GhostIO\Tag;
use GhostIO\Entities\Tag;

/**
* Corresponding Class to test Tag class
Expand Down
2 changes: 1 addition & 1 deletion tests/UserTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

use GhostIO\User;
use GhostIO\Entities\User;

/**
* Corresponding Class to test User class
Expand Down

0 comments on commit 7256959

Please sign in to comment.