More tests and a small refactoring

This commit is contained in:
Bruno F. Fontes 2019-06-17 21:23:12 -03:00
parent 9c32f3b8d8
commit 546814b26c
Signed by: brunofontes
GPG Key ID: EE3447CE80048495
2 changed files with 47 additions and 52 deletions

View File

@ -5,21 +5,17 @@ if (!isset($_SESSION['token'])) {
header('Location: /oauth.php'); header('Location: /oauth.php');
} }
define('BASE_URL', 'https://cloud.memsource.com/web'); define('BASE_URL', parse_ini_file('config.ini')['base_url']);
print_r(listProjects()); die(); // print_r(listProjects()); die();
// print_r(getProject('dOVoecVbGYq85VwGYkJgY0')); die(); print_r(getProject('dOVoecVbGYq85VwGYkJgY0')); die();
// print_r(listJobs('dOVoecVbGYq85VwGYkJgY0')); die();
downloadBilingualFiles('dOVoecVbGYq85VwGYkJgY0', ['tRJYN7LHPmaS8BVL3Vx8p2', 'DJbSRDTC7Jo004AvIb1V1M'], 'download.mxliff');
die();
$resource = getResource($_SESSION['token']); $resource = getResource($_SESSION['token']);
print_r($resource); die(); print_r($resource); die();
echo "<br>\nuserName: {$resource['user']['userName']}";
echo "<br>\nName: {$resource['user']['firstName']}";
echo "<br>\nLast name: {$resource['user']['lastName']}";
echo "<br>\nEmail: {$resource['user']['email']}";
echo "<br>\nEdition: {$resource['edition']['name']}";
echo "<br>\nOrganization: {$resource['organization']['name']}";
function listProjects() function listProjects()
{ {
$url = '/api2/v1/projects'; $url = '/api2/v1/projects';
@ -41,16 +37,25 @@ function listProjects()
function getProject(string $projectUid) function getProject(string $projectUid)
{ {
$url = '/api2/v1/projects/'.$projectUid; $url = '/api2/v1/projects/' . $projectUid;
return apiGet($_SESSION['token'], $url); return apiGet($_SESSION['token'], $url);
} }
function listJobs(string $projectUid) function listJobs(string $projectUid)
{ {
$url = '/api2/v1/projects/'.$projectUid.'/jobs/'; $url = '/api2/v1/projects/' . $projectUid . '/jobs/';
return apiGet($_SESSION['token'], $url); return apiGet($_SESSION['token'], $url);
} }
function downloadBilingualFiles(string $projectUid, array $jobsUid, string $filename)
{
$url = '/api2/v1/projects/'.$projectUid.'/jobs/bilingualFile?format=MXLF';
foreach ($jobsUid as $key => $jobUid) {
$postFields[] = ['uid' => $jobUid];
}
return apiDownloadFile($_SESSION['token'], $url, ['jobs' => $postFields], $filename);
}
// we can now use the access_token as much as we want to access protected resources // we can now use the access_token as much as we want to access protected resources
function apiGet(string $access_token, string $url) function apiGet(string $access_token, string $url)
{ {
@ -69,46 +74,44 @@ function apiGet(string $access_token, string $url)
return json_decode($response, true); return json_decode($response, true);
} }
function apiJsonPost(string $access_token, string $url, array $postFields) function apiJsonPost(string $access_token, string $url, array $postFields, array $extraHeader = [])
{ {
$header = array_merge(
["Authorization: Bearer {$access_token}", 'Content-type: application/json'],
$extraHeader
);
$curl = curl_init(); $curl = curl_init();
curl_setopt_array($curl, [ curl_setopt_array(
$curl,
[
CURLOPT_URL => BASE_URL . $url, CURLOPT_URL => BASE_URL . $url,
CURLOPT_HTTPHEADER => ["Authorization: Bearer {$access_token}", "Content-type: application/json"], CURLOPT_HTTPHEADER => $header,
CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_POST => true, CURLOPT_POST => true,
CURLOPT_POSTFIELDS => json_encode($postFields), CURLOPT_POSTFIELDS => json_encode($postFields),
CURLOPT_RETURNTRANSFER => true CURLOPT_RETURNTRANSFER => true
]); ]
);
$response = curl_exec($curl); $response = curl_exec($curl);
curl_close($curl); curl_close($curl);
return json_decode($response, true); return json_decode($response, true);
} }
function getResource($access_token) function apiDownloadFile($access_token, string $url, array $postFields, string $filename)
{ {
//$test_api_url = 'https://cloud.memsource.com/web/api2/v1/auth/whoAmI'; $file = fopen($filename, 'w+');
//$test_api_url = 'https://cloud.memsource.com/web/api2/v1/projects';
// $test_api_url = 'https://cloud.memsource.com/web/api2/v1/projects/dOVoecVbGYq85VwGYkJgY0';
// $test_api_url = 'https://cloud.memsource.com/web/api2/v2/projects/dOVoecVbGYq85VwGYkJgY0/jobs';
$test_api_url = 'https://cloud.memsource.com/web/api2/v1/projects/dOVoecVbGYq85VwGYkJgY0/jobs/bilingualFile';
$array_content = ['jobs' => [['uid' => 'tRJYN7LHPmaS8BVL3Vx8p2']]];
$content = json_encode($array_content);
$header = ["Authorization: Bearer {$access_token}", "Content-type: application/json"];
$curl = curl_init(); $curl = curl_init();
curl_setopt_array($curl, [ curl_setopt_array($curl, [
CURLOPT_URL => $test_api_url, CURLOPT_URL => BASE_URL . $url,
CURLOPT_HTTPHEADER => $header, CURLOPT_HTTPHEADER => ["Authorization: Bearer {$access_token}", 'Content-type: application/json'],
CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYPEER => false,
// CURLOPT_CUSTOMREQUEST => 'DELETE', // <-- This is how to send a DELETE request CURLOPT_TIMEOUT => 500,
CURLOPT_FILE => $file,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_POST => true, CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $content, CURLOPT_POSTFIELDS => json_encode($postFields)
CURLOPT_RETURNTRANSFER => true
]); ]);
$response = curl_exec($curl); $response = curl_exec($curl);
curl_close($curl); curl_close($curl);
return $response;
return json_decode($response, true);
} }

View File

@ -1,9 +1,9 @@
<?php <?php
session_start(); session_start();
$authorize_url = 'https://cloud.memsource.com/web/oauth/authorize';
$token_url = 'https://cloud.memsource.com/web/oauth/token';
$config = parse_ini_file('config.ini'); $config = parse_ini_file('config.ini');
$authorize_url = $config['base_url'] . '/oauth/authorize';
$token_url = $config['base_url'] . '/oauth/token';
// callback URL specified when the application was defined--has to match what the application says // callback URL specified when the application was defined--has to match what the application says
$callback_uri = $config['callbackUri']; $callback_uri = $config['callbackUri'];
@ -29,11 +29,8 @@ if (isset($_GET['code'])) {
function getAuthorizationCode() function getAuthorizationCode()
{ {
global $authorize_url, $client_id, $callback_uri; global $authorize_url, $client_id, $callback_uri;
$authorization_redirect_url = $authorize_url . '?response_type=code&client_id=' . $client_id . '&redirect_uri=' . $callback_uri . '&scope=openid'; $authorization_redirect_url = $authorize_url . '?response_type=code&client_id=' . $client_id . '&redirect_uri=' . $callback_uri . '&scope=openid';
header('Location: ' . $authorization_redirect_url); header('Location: ' . $authorization_redirect_url);
// if you don't want to redirect // if you don't want to redirect
// echo "Go <a href='$authorization_redirect_url'>here</a>, copy the code, and paste it into the box below.<br /><form action=" . $_SERVER["PHP_SELF"] . " method = 'post'><input type='text' name='authorization_code' /><br /><input type='submit'></form>"; // echo "Go <a href='$authorization_redirect_url'>here</a>, copy the code, and paste it into the box below.<br /><form action=" . $_SERVER["PHP_SELF"] . " method = 'post'><input type='text' name='authorization_code' /><br /><input type='submit'></form>";
} }
@ -61,14 +58,9 @@ function getAccessToken($authorization_code)
curl_close($curl); curl_close($curl);
if ($response === false) { if ($response === false) {
echo 'Failed'; echo 'Failed', curl_error($curl);
echo curl_error($curl);
echo 'Failed';
} elseif (isset($objectResponse->error)) { } elseif (isset($objectResponse->error)) {
echo 'Error:<br />'; echo "Error:<br /> $authorization_code $response";
echo $authorization_code;
echo $response;
} }
return $objectResponse->access_token; return $objectResponse->access_token;
} }