Improve api operations

Change directory structure
Add bitinflow-accounts socialite provider
Improve docs generation
This commit is contained in:
René Preuß
2019-09-22 21:53:49 +02:00
parent 8a3d7e0411
commit 228c845bb5
32 changed files with 569 additions and 37 deletions

View File

@@ -9,9 +9,10 @@ PHP bitinflow Accounts API Client for Laravel 5+
## Table of contents
1. [Installation](#installation)
2. [Configuration](#configuration)
3. [Examples](#examples)
4. [Documentation](#documentation)
2. [Event Listener](#event-listener)
3. [Configuration](#configuration)
4. [Examples](#examples)
5. [Documentation](#documentation)
6. [Development](#Development)
## Installation
@@ -28,6 +29,28 @@ Add Service Provider to your `app.php` configuration file:
GhostZero\BitinflowAccounts\Providers\BitinflowAccountsServiceProvider::class,
```
## Event Listener
- Add `SocialiteProviders\Manager\SocialiteWasCalled` event to your `listen[]` array in `app/Providers/EventServiceProvider`.
- Add your listeners (i.e. the ones from the providers) to the `SocialiteProviders\Manager\SocialiteWasCalled[]` that you just created.
- The listener that you add for this provider is `'GhostZero\\BitinflowAccounts\\Socialite\\BitinflowExtendSocialite@handle',`.
- Note: You do not need to add anything for the built-in socialite providers unless you override them with your own providers.
```
/**
* The event handler mappings for the application.
*
* @var array
*/
protected $listen = [
\SocialiteProviders\Manager\SocialiteWasCalled::class => [
// add your listeners (aka providers) here
'GhostZero\\BitinflowAccounts\\Socialite\\BitinflowExtendSocialite@handle',
],
];
```
## Configuration
Copy configuration to config folder:
@@ -44,6 +67,18 @@ BITINFLOW_ACCOUNTS_SECRET=
BITINFLOW_ACCOUNTS_REDIRECT_URI=http://localhost
```
You will need to add an entry to the services configuration file so that after config files are cached for usage in production environment (Laravel command `artisan config:cache`) all config is still available.
**Add to `config/services.php`:**
```php
'bitinflow-accounts' => [
'client_id' => env('BITINFLOW_ACCOUNTS_KEY'),
'client_secret' => env('BITINFLOW_ACCOUNTS_SECRET'),
'redirect' => env('BITINFLOW_ACCOUNTS_REDIRECT_URI')
],
```
## Examples
#### Basic
@@ -114,10 +149,20 @@ BitinflowAccounts::withClientId('abc123')->withToken('abcdef123456')->getAuthedU
## Documentation
### Users
### Charges
```php
public function getAuthedUser()
public function createCharge(array $parameters)
public function getCharge(string $id)
public function updateCharge(string $id, array $parameters)
public function captureCharge(string $id, array $parameters = [])
```
### CheckoutSessions
```php
public function getCheckoutSession(string $id)
public function createCheckoutSession(array $parameters)
```
### SshKeys
@@ -128,7 +173,13 @@ public function createSshKey(string $publicKey, string $name = NULL)
public function deleteSshKey(int $id)
```
[**OAuth Scopes Enums**](https://git.preuss.io/ghostzero/bitinflow-accounts/blob/master/src/Enums/Scope.php)
### Users
```php
public function getAuthedUser()
```
[**OAuth Scopes Enums**](https://github.com/ghostzero/bitinflow-accounts/blob/master/src/Enums/Scope.php)
## Development

View File

@@ -28,6 +28,28 @@ Add Service Provider to your `app.php` configuration file:
GhostZero\BitinflowAccounts\Providers\BitinflowAccountsServiceProvider::class,
```
## Event Listener
- Add `SocialiteProviders\Manager\SocialiteWasCalled` event to your `listen[]` array in `app/Providers/EventServiceProvider`.
- Add your listeners (i.e. the ones from the providers) to the `SocialiteProviders\Manager\SocialiteWasCalled[]` that you just created.
- The listener that you add for this provider is `'GhostZero\\BitinflowAccounts\\Socialite\\BitinflowExtendSocialite@handle',`.
- Note: You do not need to add anything for the built-in socialite providers unless you override them with your own providers.
```
/**
* The event handler mappings for the application.
*
* @var array
*/
protected $listen = [
\SocialiteProviders\Manager\SocialiteWasCalled::class => [
// add your listeners (aka providers) here
'GhostZero\\BitinflowAccounts\\Socialite\\BitinflowExtendSocialite@handle',
],
];
```
## Configuration
Copy configuration to config folder:
@@ -44,6 +66,18 @@ BITINFLOW_ACCOUNTS_SECRET=
BITINFLOW_ACCOUNTS_REDIRECT_URI=http://localhost
```
You will need to add an entry to the services configuration file so that after config files are cached for usage in production environment (Laravel command `artisan config:cache`) all config is still available.
**Add to `config/services.php`:**
```php
'bitinflow-accounts' => [
'client_id' => env('BITINFLOW_ACCOUNTS_KEY'),
'client_secret' => env('BITINFLOW_ACCOUNTS_SECRET'),
'redirect' => env('BITINFLOW_ACCOUNTS_REDIRECT_URI')
],
```
## Examples
#### Basic
@@ -116,7 +150,7 @@ BitinflowAccounts::withClientId('abc123')->withToken('abcdef123456')->getAuthedU
<!-- GENERATED-DOCS -->
[**OAuth Scopes Enums**](https://git.preuss.io/ghostzero/bitinflow-accounts/blob/master/src/Enums/Scope.php)
[**OAuth Scopes Enums**](https://github.com/ghostzero/bitinflow-accounts/blob/master/src/Enums/Scope.php)
## Development

View File

@@ -10,9 +10,11 @@
],
"require": {
"php": ">=7.2",
"ext-json": "*",
"illuminate/support": "^5.5",
"illuminate/console": "^5.5",
"guzzlehttp/guzzle": "^6.3"
"guzzlehttp/guzzle": "^6.3",
"socialiteproviders/manager": "^3.4"
},
"require-dev": {
"phpunit/phpunit": "^8.0",
@@ -21,12 +23,12 @@
},
"autoload": {
"psr-4": {
"GhostZero\\BitinflowAccounts\\": "src"
"GhostZero\\BitinflowAccounts\\": "src/GhostZero/BitinflowAccounts"
}
},
"autoload-dev": {
"psr-4": {
"GhostZero\\BitinflowAccounts\\Tests\\": "tests"
"GhostZero\\BitinflowAccounts\\Tests\\": "tests/GhostZero/BitinflowAccounts"
}
},
"scripts": {

189
composer.lock generated
View File

@@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically"
],
"content-hash": "d862392957845962bdb82aeb0ee836fe",
"content-hash": "d05b61cfebf348cc424dbf01ec508a51",
"packages": [
{
"name": "doctrine/inflector",
@@ -627,6 +627,70 @@
],
"time": "2019-08-27T14:35:59+00:00"
},
{
"name": "laravel/socialite",
"version": "v4.2.0",
"source": {
"type": "git",
"url": "https://github.com/laravel/socialite.git",
"reference": "f509d06e1e7323997b804c5152874f8aad4508e9"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/laravel/socialite/zipball/f509d06e1e7323997b804c5152874f8aad4508e9",
"reference": "f509d06e1e7323997b804c5152874f8aad4508e9",
"shasum": ""
},
"require": {
"ext-json": "*",
"guzzlehttp/guzzle": "~6.0",
"illuminate/http": "~5.7.0|~5.8.0|^6.0|^7.0",
"illuminate/support": "~5.7.0|~5.8.0|^6.0|^7.0",
"league/oauth1-client": "~1.0",
"php": "^7.1.3"
},
"require-dev": {
"illuminate/contracts": "~5.7.0|~5.8.0|^6.0|^7.0",
"mockery/mockery": "^1.0",
"phpunit/phpunit": "^7.0|^8.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "4.0-dev"
},
"laravel": {
"providers": [
"Laravel\\Socialite\\SocialiteServiceProvider"
],
"aliases": {
"Socialite": "Laravel\\Socialite\\Facades\\Socialite"
}
}
},
"autoload": {
"psr-4": {
"Laravel\\Socialite\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Taylor Otwell",
"email": "taylor@laravel.com"
}
],
"description": "Laravel wrapper around OAuth 1 & OAuth 2 libraries.",
"homepage": "https://laravel.com",
"keywords": [
"laravel",
"oauth"
],
"time": "2019-09-03T15:27:17+00:00"
},
{
"name": "league/flysystem",
"version": "1.0.55",
@@ -711,6 +775,69 @@
],
"time": "2019-08-24T11:17:19+00:00"
},
{
"name": "league/oauth1-client",
"version": "1.7.0",
"source": {
"type": "git",
"url": "https://github.com/thephpleague/oauth1-client.git",
"reference": "fca5f160650cb74d23fc11aa570dd61f86dcf647"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/thephpleague/oauth1-client/zipball/fca5f160650cb74d23fc11aa570dd61f86dcf647",
"reference": "fca5f160650cb74d23fc11aa570dd61f86dcf647",
"shasum": ""
},
"require": {
"guzzlehttp/guzzle": "^6.0",
"php": ">=5.5.0"
},
"require-dev": {
"mockery/mockery": "^0.9",
"phpunit/phpunit": "^4.0",
"squizlabs/php_codesniffer": "^2.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.0-dev"
}
},
"autoload": {
"psr-4": {
"League\\OAuth1\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Ben Corlett",
"email": "bencorlett@me.com",
"homepage": "http://www.webcomm.com.au",
"role": "Developer"
}
],
"description": "OAuth 1.0 Client Library",
"keywords": [
"Authentication",
"SSO",
"authorization",
"bitbucket",
"identity",
"idp",
"oauth",
"oauth1",
"single sign on",
"trello",
"tumblr",
"twitter"
],
"time": "2016-08-17T00:36:58+00:00"
},
{
"name": "monolog/monolog",
"version": "1.24.0",
@@ -1328,6 +1455,63 @@
],
"time": "2018-07-19T23:38:55+00:00"
},
{
"name": "socialiteproviders/manager",
"version": "v3.4.2",
"source": {
"type": "git",
"url": "https://github.com/SocialiteProviders/Manager.git",
"reference": "e3e8e78b9a3060801cd008941a0894a0a0c479e1"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/SocialiteProviders/Manager/zipball/e3e8e78b9a3060801cd008941a0894a0a0c479e1",
"reference": "e3e8e78b9a3060801cd008941a0894a0a0c479e1",
"shasum": ""
},
"require": {
"illuminate/support": "~5.4|~5.7.0|~5.8.0|^6.0",
"laravel/socialite": "~3.0|~4.0",
"php": "^5.6 || ^7.0"
},
"require-dev": {
"mockery/mockery": "^0.9.4",
"phpunit/phpunit": "^5.0"
},
"type": "library",
"extra": {
"laravel": {
"providers": [
"SocialiteProviders\\Manager\\ServiceProvider"
]
}
},
"autoload": {
"psr-4": {
"SocialiteProviders\\Manager\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Andy Wendt",
"email": "andy@awendt.com"
},
{
"name": "Anton Komarev",
"email": "a.komarev@cybercog.su"
},
{
"name": "Miguel Piedrafita",
"email": "soy@miguelpiedrafita.com"
}
],
"description": "Easily add new or override built-in providers in Laravel Socialite.",
"time": "2019-09-09T03:07:52+00:00"
},
{
"name": "swiftmailer/swiftmailer",
"version": "v6.2.1",
@@ -4810,7 +4994,8 @@
"prefer-stable": false,
"prefer-lowest": false,
"platform": {
"php": ">=7.2"
"php": ">=7.2",
"ext-json": "*"
},
"platform-dev": []
}

View File

@@ -76,6 +76,8 @@ $markdown = collect(class_uses(BitinflowAccounts::class))
return $markdown;
})->join(PHP_EOL . PHP_EOL);
$markdown = str_replace("array (\n)", '[]', $markdown);
$content = file_get_contents(__DIR__ . '/../README.stub');
$content = str_replace('<!-- GENERATED-DOCS -->', $markdown, $content);

View File

@@ -1,22 +0,0 @@
<?php
declare(strict_types=1);
namespace GhostZero\BitinflowAccounts\Enums;
/**
* @author René Preuß <rene@preuss.io>
*/
class Scope
{
/*
* v0 API
*/
// Deprecated scope.
const API = 'api';
// Read nonpublic user information, including email address.
const READ_USER = 'read_user';
}

View File

@@ -6,7 +6,11 @@ namespace GhostZero\BitinflowAccounts\ApiOperations;
use GhostZero\BitinflowAccounts\Helpers\Paginator;
/**
* @author René Preuß <rene@preuss.io>
*/
trait Delete
{
abstract public function delete(string $path = '', array $parameters = [], Paginator $paginator = null);
}

View File

@@ -6,7 +6,11 @@ namespace GhostZero\BitinflowAccounts\ApiOperations;
use GhostZero\BitinflowAccounts\Helpers\Paginator;
/**
* @author René Preuß <rene@preuss.io>
*/
trait Get
{
abstract public function get(string $path = '', array $parameters = [], Paginator $paginator = null);
}

View File

@@ -6,7 +6,11 @@ namespace GhostZero\BitinflowAccounts\ApiOperations;
use GhostZero\BitinflowAccounts\Helpers\Paginator;
/**
* @author René Preuß <rene@preuss.io>
*/
trait Post
{
abstract public function post(string $path = '', array $parameters = [], Paginator $paginator = null);
}

View File

@@ -6,7 +6,11 @@ namespace GhostZero\BitinflowAccounts\ApiOperations;
use GhostZero\BitinflowAccounts\Helpers\Paginator;
/**
* @author René Preuß <rene@preuss.io>
*/
trait Put
{
abstract public function put(string $path = '', array $parameters = [], Paginator $paginator = null);
}

View File

@@ -17,8 +17,10 @@ use GuzzleHttp\Exception\RequestException;
class BitinflowAccounts
{
use Traits\UsersTrait;
use Traits\ChargesTrait;
use Traits\CheckoutSessionsTrait;
use Traits\SshKeysTrait;
use Traits\UsersTrait;
const BASE_URI = 'https://accounts.bitinflow.com/api/';
const OAUTH_BASE_URI = 'https://accounts.bitinflow.com/api/';

View File

@@ -0,0 +1,38 @@
<?php
declare(strict_types=1);
namespace GhostZero\BitinflowAccounts\Enums;
/**
* @author René Preuß <rene@preuss.io>
*/
class Scope
{
/*
* v0 API
*/
// Deprecated scope.
const API = 'api';
// Read nonpublic user information, including email address.
const READ_USER = 'read_user';
/*
* v1 API
*/
// Read authorized user´s email address.
const USERS_READ_EMAIL = 'users:read:email';
// Manage a authorized user object.
const USERS_EDIT = 'users:edit';
// Read authorized user´s transactions.
const TRANSACTIONS_READ = 'transactions:read';
// Create a new charge for the authorized user.
const CHARGES_CREATE = 'charges:create';
}

View File

@@ -0,0 +1,24 @@
<?php
namespace GhostZero\BitinflowAccounts\Socialite;
use SocialiteProviders\Manager\SocialiteWasCalled;
/**
* @author René Preuß <rene@preuss.io>
*/
class BitinflowExtendSocialite
{
/**
* Register the provider.
*
* @param SocialiteWasCalled $socialiteWasCalled
*/
public function handle(SocialiteWasCalled $socialiteWasCalled)
{
$socialiteWasCalled->extendSocialite(
'bitinflow-accounts', __NAMESPACE__ . '\Provider'
);
}
}

View File

@@ -0,0 +1,89 @@
<?php
namespace GhostZero\BitinflowAccounts\Socialite;
use GhostZero\BitinflowAccounts\Enums\Scope;
use Illuminate\Support\Arr;
use Laravel\Socialite\Two\ProviderInterface;
use SocialiteProviders\Manager\OAuth2\AbstractProvider;
use SocialiteProviders\Manager\OAuth2\User;
/**
* @author René Preuß <rene@preuss.io>
*/
class Provider extends AbstractProvider implements ProviderInterface
{
/**
* Unique Provider Identifier.
*/
const IDENTIFIER = 'BITINFLOW_ACCOUNTS';
/**
* {@inheritdoc}
*/
protected $scopes = [Scope::READ_USER];
/**
* {@inherticdoc}.
*/
protected $scopeSeparator = ' ';
/**
* {@inheritdoc}
*/
protected function getAuthUrl($state)
{
return $this->buildAuthUrlFromBase(
'https://accounts.bitinflow.com/oauth/authorize', $state
);
}
/**
* {@inheritdoc}
*/
protected function getTokenUrl()
{
return 'https://accounts.bitinflow.com/oauth/token';
}
/**
* {@inheritdoc}
*/
protected function getUserByToken($token)
{
$response = $this->getHttpClient()->get(
'https://accounts.bitinflow.com/api/user', [
'headers' => [
'Accept' => 'application/json',
'Authorization' => 'Bearer ' . $token,
],
]);
return json_decode($response->getBody()->getContents(), true);
}
/**
* {@inheritdoc}
*/
protected function mapUserToObject(array $user)
{
return (new User())->setRaw($user)->map([
'id' => $user['id'],
'nickname' => $user['name'],
'name' => $user['name'],
'email' => Arr::get($user, 'email'),
'avatar' => $user['avatar'],
]);
}
/**
* {@inheritdoc}
*/
protected function getTokenFields($code)
{
return array_merge(parent::getTokenFields($code), [
'grant_type' => 'authorization_code',
]);
}
}

View File

@@ -0,0 +1,69 @@
<?php
declare(strict_types=1);
namespace GhostZero\BitinflowAccounts\Traits;
use GhostZero\BitinflowAccounts\ApiOperations\Get;
use GhostZero\BitinflowAccounts\ApiOperations\Post;
use GhostZero\BitinflowAccounts\ApiOperations\Put;
use GhostZero\BitinflowAccounts\Result;
/**
* @author René Preuß <rene@preuss.io>
*/
trait ChargesTrait
{
use Get, Post, Put;
/**
* Create a Charge object
*
* @param array $parameters
*
* @return Result Result object
*/
public function createCharge(array $parameters): Result
{
return $this->post('charges', $parameters);
}
/**
* Get a Charge object
*
* @param string $id
*
* @return Result Result object
*/
public function getCharge(string $id): Result
{
return $this->get("charges/$id");
}
/**
* Update a Charge object
*
* @param string $id
* @param array $parameters
*
* @return Result Result object
*/
public function updateCharge(string $id, array $parameters): Result
{
return $this->put("charges/$id", $parameters);
}
/**
* Capture a Charge object
*
* @param string $id
* @param array $parameters
*
* @return Result Result object
*/
public function captureCharge(string $id, array $parameters = []): Result
{
return $this->post("charges/$id/capture", $parameters);
}
}

View File

@@ -0,0 +1,42 @@
<?php
declare(strict_types=1);
namespace GhostZero\BitinflowAccounts\Traits;
use GhostZero\BitinflowAccounts\ApiOperations\Get;
use GhostZero\BitinflowAccounts\ApiOperations\Post;
use GhostZero\BitinflowAccounts\Result;
/**
* @author René Preuß <rene@preuss.io>
*/
trait CheckoutSessionsTrait
{
use Get, Post;
/**
* Get a Session object
*
* @param string $id
*
* @return Result Result object
*/
public function getCheckoutSession(string $id): Result
{
return $this->get("checkout/sessions/$id");
}
/**
* Create a Session object
*
* @param array $parameters
*
* @return Result
*/
public function createCheckoutSession(array $parameters): Result
{
return $this->post('payments/sessions', $parameters);
}
}

View File

@@ -39,7 +39,7 @@ trait SshKeysTrait
return $this->post('ssh-keys', [
'public_key' => $publicKey,
'name' => $name,
], null);
]);
}
/**
@@ -51,6 +51,6 @@ trait SshKeysTrait
*/
public function deleteSshKey(int $id): Result
{
return $this->delete("ssh-keys/$id", [], null);
return $this->delete("ssh-keys/$id", []);
}
}