Skip to content

Commit

Permalink
ENT-1048: add manifesto php client (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikerudd authored Apr 23, 2019
1 parent 7e2af7c commit 7e4609d
Show file tree
Hide file tree
Showing 15 changed files with 1,453 additions and 6 deletions.
3 changes: 0 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ FROM talis/ubuntu:1404-latest

ENV DEBIAN_FRONTEND noninteractive

ARG git_oauth_token
ARG persona_oauth_client
ARG persona_oauth_secret

Expand Down Expand Up @@ -32,8 +31,6 @@ RUN apt-get install --no-install-recommends -y \
# Install composer
RUN curl https://getcomposer.org/installer | php -- && mv composer.phar /usr/local/bin/composer && chmod +x /usr/local/bin/composer

RUN composer config -g github-oauth.github.com $git_oauth_token

# Tidy up
RUN apt-get -y autoremove && apt-get clean && apt-get autoclean && \
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
Expand Down
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,33 @@ up and running to develop and test changes. Follow these steps:

# Build the development image

Clone the repo locally:

```bash
git clone https://github.com/talis/talis-php.git
cd talis-php
ant build
```

Manually run a docker build:

```bash
docker build -t "talis/talis-php" --network=host --build-arg persona_oauth_client=<persona-user-goes-here> --build-arg persona_oauth_secret=<persona-secret-goes-here> .
```

`persona_oauth_client` = the persona user you want to use.

`persona_oauth_secret` = the password to the user specified.

Initialise the environment. Run the following command which will download the required libraries.

```bash
docker-compose run init
```

# When the above has built you can run the tests

Available test commands:

```bash
docker-compose run lint
docker-compose run codecheck
Expand Down
7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
{
"name": "talis/talis-php",
"description": "This is a php client library for talis api's",
"version": "0.2.0",
"version": "0.3.0",
"keywords": [
"persona",
"echo",
"babel",
"critic",
"manifesto",
"php",
"client library"
],
Expand Down Expand Up @@ -35,7 +36,9 @@
"Talis\\Critic\\": "src",
"Talis\\Critic\\Exceptions\\": "src",
"Talis\\EchoClient\\": "src",
"Talis\\Babel\\": "src"
"Talis\\Babel\\": "src",
"Talis\\Manifesto\\": "src",
"Talis\\Manifesto\\Exceptions": "src"
}
}
}
70 changes: 70 additions & 0 deletions src/Talis/Manifesto/Archive.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php


namespace Talis\Manifesto;

require_once 'common.inc.php';

class Archive {
/**
* @var string
*/
protected $id;

/**
* @var string
*/
protected $status;

/**
* @var string
*/
protected $location;

public function loadFromJson($jsonDocument)
{
$this->loadFromArray(json_decode($jsonDocument, true));
}

public function loadFromArray(array $array)
{
if(isset($array['id']))
{
$this->id = $array['id'];
}
if(isset($array['status']))
{
$this->status = $array['status'];
}

if(isset($array['location']))
{
$this->location = $array['location'];
}
}

/**
* @return string
*/
public function getStatus()
{
return $this->status;
}

/**
* @return string
*/
public function getLocation()
{
return $this->location;
}

/**
* @return string
*/
public function getId()
{
return $this->id;
}

}
246 changes: 246 additions & 0 deletions src/Talis/Manifesto/Client.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,246 @@
<?php

namespace Talis\Manifesto;

require_once dirname(__FILE__) . '/common.inc.php';

class Client {

protected $clientId;
protected $clientSecret;

/**
* @var Talis\Persona\Client\Tokens
*/
protected $personaClient;

/**
* @var array
*/
protected $personaConnectValues = array();

/**
* @var string
*/
protected $manifestoBaseUrl;

/**
* @var \Guzzle\Http\Client
*/
protected $httpClient;
/**
* @param string $manifestoBaseUrl
* @param array $personaConnectValues
*/
public function __construct($manifestoBaseUrl, $personaConnectValues = array())
{
$this->manifestoBaseUrl = $manifestoBaseUrl;
$this->personaConnectValues = $personaConnectValues;
}

/**
* @param array $personaConnectValues
*/
public function setPersonaConnectValues($personaConnectValues)
{
$this->personaConnectValues = $personaConnectValues;
}

/**
* @return string
*/
public function getManifestoBaseUrl()
{
return $this->manifestoBaseUrl;
}

/**
* @param string $manifestoBaseUrl
*/
public function setManifestoBaseUrl($manifestoBaseUrl)
{
$this->manifestoBaseUrl = $manifestoBaseUrl;
}

/**
* For mocking
* @return Talis\Persona\Client\Tokens
*/
protected function getPersonaClient()
{
if(!isset($this->personaClient))
{
$this->personaClient = new Talis\Persona\Client\Tokens($this->personaConnectValues);
}
return $this->personaClient;
}

/**
* Allows PersonaClient override, if PersonaClient has been initialized elsewhere
* @param Talis\Persona\Client\Tokens $personaClient
*/
public function setPersonaClient(\Talis\Persona\Client\Tokens $personaClient)
{
$this->personaClient = $personaClient;
}

/**
* For mocking
* @return \Guzzle\Http\Client
*/
protected function getHTTPClient()
{
if(!$this->httpClient)
{
$this->httpClient = new \Guzzle\Http\Client();
}
return $this->httpClient;
}

/**
* Create an archive generation job request
*
* @param Manifest $manifest
* @param string $clientId
* @param string $clientSecret
* @throws \Exception|\Guzzle\Http\Exception\ClientErrorResponseException
* @throws Exceptions\ManifestValidationException
* @throws Exceptions\UnauthorisedAccessException
* @throws Exceptions\ArchiveException
* @return \Talis\Manifesto\Archive
*/
public function requestArchive(Manifest $manifest, $clientId, $clientSecret)
{
$archiveLocation = $this->manifestoBaseUrl . '/1/archives';
$manifestDocument = json_encode($manifest->generateManifest());

try
{
$client = $this->getHTTPClient();
$headers = $this->getHeaders($clientId, $clientSecret);

$request = $client->post($archiveLocation, $headers, $manifestDocument);

$response = $request->send();

if($response->getStatusCode() == 202)
{
$archive = new \Talis\Manifesto\Archive();
$archive->loadFromJson($response->getBody(true));
return $archive;
}
else
{
throw new \Talis\Manifesto\Exceptions\ArchiveException($response->getStatusCode(), $response->getBody(true));
}
}
/** @var \Guzzle\Http\Exception\ClientErrorResponseException $e */
catch(\Guzzle\Http\Exception\ClientErrorResponseException $e)
{
$response = $e->getResponse();
$error = $this->processErrorResponseBody($response->getBody(true));
switch($response->getStatusCode())
{
case 400:
throw new \Talis\Manifesto\Exceptions\ManifestValidationException($error['message'], $error['error_code'], $e);
case 403:
case 401:
throw new \Talis\Manifesto\Exceptions\UnauthorisedAccessException($error['message'], $error['error_code'], $e);
break;
case 404:
throw new \Talis\Manifesto\Exceptions\ArchiveException('Misconfigured Manifesto base url', 404);
break;
default:
throw $e;
}
}

}

/**
* Generate a pre signed URL via Manifesto API
* @param string $jobId
* @param string $clientId
* @param string $clientSecret
* @return string
* @throws \Exception|\Guzzle\Http\Exception\ClientErrorResponseException
* @throws Exceptions\GenerateUrlException
*/
public function generateUrl($jobId, $clientId, $clientSecret)
{
$url = $this->manifestoBaseUrl . '/1/archives/'.$jobId.'/generateUrl';
try
{
$client = $this->getHTTPClient();
$headers = $this->getHeaders($clientId, $clientSecret);

$request = $client->post($url, $headers);

$response = $request->send();

if($response->getStatusCode() == 200)
{
$body = $response->getBody(true);
if(!empty($body))
{
$body = json_decode($response->getBody(true));
}
return $body->url;
} else
{
throw new \Talis\Manifesto\Exceptions\GenerateUrlException($response->getStatusCode, $response->getBody(true));
}

} catch(\Guzzle\Http\Exception\ClientErrorResponseException $e){
$response = $e->getResponse();
$error = $this->processErrorResponseBody($response->getBody(true));
switch($response->getStatusCode())
{
case 401:
throw new \Talis\Manifesto\Exceptions\UnauthorisedAccessException($error['message'], $error['error_code'], $e);
break;
case 404:
throw new \Talis\Manifesto\Exceptions\GenerateUrlException('Missing archive', 404);
break;
default:
throw $e;
}
}
}

/**
* Setup the header array for any request to Manifesto
* @param string $clientId
* @param string $clientSecret
* @return array
*/
protected function getHeaders($clientId, $clientSecret)
{
$arrPersonaToken = $this->getPersonaClient()->obtainNewToken($clientId, $clientSecret);
$personaToken = $arrPersonaToken['access_token'];
$headers = array(
'Content-Type'=>'application/json',
'Authorization'=>'Bearer '.$personaToken
);
return $headers;
}

protected function processErrorResponseBody($responseBody)
{

$error = array('error_code'=>null, 'message'=>null);
$response = json_decode($responseBody, true);

if(isset($response['error_code']))
{
$error['error_code'] = $response['error_code'];
}

if(isset($response['message']))
{
$error['message'] = $response['message'];
}

return $error;
}
}
Loading

0 comments on commit 7e4609d

Please sign in to comment.