diff --git a/src/FetchApi.php b/src/FetchApi.php index 466281f..fc9a3ec 100644 --- a/src/FetchApi.php +++ b/src/FetchApi.php @@ -11,6 +11,11 @@ class FetchApi { protected $base_url; protected $token; + + public function getBase_url() + { + return $this->base_url; + } /** * BaseAPI needs at least the Memsource Token to use it's API @@ -18,7 +23,7 @@ class FetchApi * @param string $token * @param string $memsourceBaseUrl [Optional] A non-standard Memsource URL base for the API to work */ - public function __construct(string $token = null, string $memsourceBaseUrl = 'https://cloud.memsource.com/web') + public function __construct(string $token = null, string $memsourceBaseUrl) { $this->base_url = $memsourceBaseUrl; $this->token = $token; @@ -27,7 +32,7 @@ class FetchApi /** * Fetch API data using curl * - * @param string $method Should be 'get', 'post', 'jsonPost' or 'download' + * @param string $method Should be 'get', 'put', 'post', 'jsonPost', 'download' or 'raw' * @param string $url The api url * @param array $parameters Array ['key' => 'value'] of get or post fields or structured array for json requests * @param string $filename [optional] Specified file in which the download request will be saved @@ -64,6 +69,9 @@ class FetchApi $setopt = $this->getDownloadFileParam($filename) + $this->getJsonPostParam($parameters); break; + case 'raw': + $setopt = $parameters; + break; default: throw new \Exception("Method {$method} is invalid on Fetch", 1); } diff --git a/src/Memsource.php b/src/Memsource.php index b994aca..aa75897 100644 --- a/src/Memsource.php +++ b/src/Memsource.php @@ -8,9 +8,15 @@ namespace BrunoFontes; +use \BrunoFontes\Memsource\BilingualFile; +use \BrunoFontes\Memsource\FetchApi; +use \BrunoFontes\Memsource\Jobs; +use \BrunoFontes\Memsource\Oauth; +use \BrunoFontes\Memsource\Project; + /** * Memsource API class - * + * * Instructions: https://github.com/brunofontes/Memsource-API/ * Memsource API details: https://cloud.memsource.com/web/docs/api */ @@ -24,47 +30,46 @@ class Memsource public function __construct(string $token = null, string $memsourceBaseUrl = 'https://cloud.memsource.com/web') { - $this->_fetchApi = new \BrunoFontes\Memsource\FetchApi($token, $memsourceBaseUrl); + $this->_fetchApi = new FetchApi($token, $memsourceBaseUrl); } /** * Memsource Oauth functions * - * @return \BrunoFontes\Memsource\Oauth + * @return Oauth */ - public function oauth(): \BrunoFontes\Memsource\Oauth + public function oauth(): Oauth { - return $this->_oauth ?? $this->_oauth = new \BrunoFontes\Memsource\oauth(); + return $this->_oauth ?? $this->_oauth = new oauth($this->_fetchApi); } /** * Memsource API BilingualFile related functions * - * @return \BrunoFontes\Memsource\BilingualFile + * @return BilingualFile */ - public function bilingualFile(): \BrunoFontes\Memsource\BilingualFile + public function bilingualFile(): BilingualFile { - return $this->_bilingualFile ?? $this->_bilingualFile = new \BrunoFontes\Memsource\BilingualFile($this->_fetchApi); + return $this->_bilingualFile ?? $this->_bilingualFile = new BilingualFile($this->_fetchApi); } /** * Memsource API Jobs related functions * - * @return \BrunoFontes\Memsource\Jobs + * @return Jobs */ - public function jobs(): \BrunoFontes\Memsource\Jobs + public function jobs(): Jobs { - return $this->_jobs ?? $this->_jobs = new \BrunoFontes\Memsource\Jobs($this->_fetchApi); + return $this->_jobs ?? $this->_jobs = new Jobs($this->_fetchApi); } - /** * Memsource API Project related functions * - * @return \BrunoFontes\Memsource\Project + * @return Project */ - public function project(): \BrunoFontes\Memsource\Project + public function project(): Project { - return $this->_project ?? $this->_project = new \BrunoFontes\Memsource\Project($this->_fetchApi); + return $this->_project ?? $this->_project = new Project($this->_fetchApi); } } diff --git a/src/Oauth.php b/src/Oauth.php index 8ac58ee..b948f2c 100644 --- a/src/Oauth.php +++ b/src/Oauth.php @@ -8,7 +8,7 @@ namespace BrunoFontes\Memsource; -class Oauth extends \BrunoFontes\Memsource\FetchApi +class Oauth extends \BrunoFontes\Memsource\BaseApi { private $_url = '/oauth'; @@ -23,8 +23,16 @@ class Oauth extends \BrunoFontes\Memsource\FetchApi */ public function getAuthorizationCodeUrl(string $client_id, string $callback_uri) { - $authorize_url = $this->base_url . $this->_url . '/authorize'; - return $authorize_url . '?response_type=code&client_id=' . $client_id . '&redirect_uri=' . $callback_uri . '&scope=openid'; + $authorize_url = $this->fetchApi->getBase_url() . $this->_url . '/authorize'; + $parambeters = http_build_query( + [ + "response_type" => 'code', + "client_id" => $client_id, + "redirect_uri" => $callback_uri, + "scope" => 'openid' + ] + ); + return "{$authorize_url}?{$parambeters}"; } public function getAccessToken(string $authorization_code, string $client_id, string $client_secret, string $callback_uri) @@ -38,7 +46,12 @@ class Oauth extends \BrunoFontes\Memsource\FetchApi CURLOPT_POST => true, CURLOPT_POSTFIELDS => $content ]; - $response = json_decode($this->curl($token_url, $params), true); + $response = json_decode($this->fetchApi->fetch('raw', $token_url, $params), true); + + if (isset($response['error'])) { + throw new \Exception("Error getting TOKEN: " . $response['error_description'], 1); + } + return $response['access_token']; } }