Transforming into a composer package class

As I should use this into more than one project, it would be better to
turn it into a composer package. So I learn how to do that and use it at
work at the same time.
This commit is contained in:
Bruno F. Fontes 2019-06-19 10:39:34 -03:00
parent 546814b26c
commit d772ee5d47
Signed by: brunofontes
GPG Key ID: EE3447CE80048495
5 changed files with 169 additions and 2 deletions

2
.gitignore vendored
View File

@ -1,3 +1,3 @@
config.ini config.ini
tags tags
vendor/

View File

@ -1,4 +1,17 @@
{ {
"name": "brunofontes/memsource-api", "name": "brunofontes/memsource-api",
"version": "1.0.0" "version": "1.0.0",
"description": "A personal memsource api to better understand how it works",
"autoload": {
"psr-4": {
"BrunoFontes\\": "src/",
"BrunoFontes\\Memsource\\": "src/"
}
},
"repositories": [
{
"type": "vcs",
"url": "https://github.com/brunofontes/Memsource-API"
}
]
} }

79
src/Memsource.php Normal file
View File

@ -0,0 +1,79 @@
<?php
/**
* A very compact and simple Memsource API library
*
* @author Bruno Fontes <developer@brunofontes.net>
* @link https://github.com/brunofontes
*/
namespace BrunoFontes;
class Memsource
{
private $_oauth;
protected $base_url;
protected $token;
public function __construct(string $memsourceBaseUrl = 'https://cloud.memsource.com/web')
{
$this->base_url = $memsourceBaseUrl;
}
public function oauth()
{
return $this->_oauth ?? $this->_oauth = new \BrunoFontes\Memsource\oauth();
}
public function get(string $url)
{
$response = $this->curl($url);
return json_decode($response, true);
}
protected function apiDownloadFile(string $url, array $postFields, string $filename)
{
$file = fopen($filename, 'w+');
$extraSetopt = [
CURLOPT_FILE => $file,
CURLOPT_FOLLOWLOCATION => true,
];
return $this->jsonPost($url, $postFields, $extraSetopt);
}
protected function jsonPost(string $url, array $postFields, array $extraSetopt = [], array $extraHeader = [])
{
$extraHeader = (['Content-type: application/json'] + $extraHeader);
$postFields = json_encode($postFields);
$response = $this->post($url, $postFields, [], $extraHeader);
return json_decode($response, true);
}
protected function post(string $url, string $postFields, array $extraSetopt = [], array $extraHeader = [])
{
$extraSetopt = (
$extraSetopt +
[
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $postFields
]
);
$response = $this->curl($url, $extraSetopt, $extraHeader);
return json_decode($response, true);
}
protected function curl(string $url, array $curl_extra_setopt=[], array $extraHeader=[])
{
$header = $this->token ? ["Authorization: Bearer {$this->token}"] : [];
$curl_setopt = [
CURLOPT_URL => $this->base_url . $url,
CURLOPT_HTTPHEADER => ($header + $extraHeader),
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_TIMEOUT => 500,
CURLOPT_FOLLOWLOCATION => true,
];
$curl = curl_init();
curl_setopt_array($curl, ($curl_setopt + $curl_extra_setopt));
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
}

34
src/auth.php Normal file
View File

@ -0,0 +1,34 @@
<?php
/**
* A very compact and simple Memsource API library
*
* @author Bruno Fontes <developer@brunofontes.net>
* @link https://github.com/brunofontes
*/
namespace BrunoFontes\Memsource;
class Auth
{
protected $base_url;
private $_client_id;
private $_client_secret;
function __construct(string $base_url)
{
$this->base_url = $base_url;
}
/**
* Directly login and get a token valid for 24h
*
* @param string $username Memsource username
* @param string $password Memsource password
*
* @return string Authorization code valid for 24h only
*/
function login(string $username, string $password)
{
$authorize_url = $this->base_url . '/oauth/authorize';
return $authorize_url . '?response_type=code&client_id=' . $client_id . '&redirect_uri=' . $callback_uri . '&scope=openid';
}
}

41
src/oauth.php Normal file
View File

@ -0,0 +1,41 @@
<?php
/**
* A very compact and simple Memsource API library
*
* @author Bruno Fontes <developer@brunofontes.net>
* @link https://github.com/brunofontes
*/
namespace BrunoFontes\Memsource;
class Oauth extends \BrunoFontes\Memsource
{
private $_url = '/oauth';
/**
* Get the URL to generate the Authorization Code from Memsource
*
* @param string $client_id Memsource client ID
* @param string $client_secret Memsource client secret
* @param string $callback_uri URL that Memsource will redirect to
*
* @return string the authorization code
*/
public function getAuthorizationCodeUrl(string $client_id, string $callback_uri)
{
$authorize_url = $this->_url . '/authorize';
return $authorize_url . '?response_type=code&client_id=' . $client_id . '&redirect_uri=' . $callback_uri . '&scope=openid';
}
public function getAccessToken(string $authorization_code, string $client_id, string $client_secret, string $callback_uri)
{
$token_url = $this->_url . '/token';
$authorization = base64_encode("$client_id:$client_secret");
$header = ["Authorization: Basic {$authorization}", 'Content-Type: application/x-www-form-urlencoded'];
$content = "grant_type=authorization_code&code=$authorization_code&redirect_uri=$callback_uri";
$response = $this->post($token_url, $content, [], $header);
if ($respose['error']) {
throw new Exception("Error getting access token", 1);
}
return $response['access_token'];
}
}