Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move GeminiClient to Crayfish Commons #16

Merged
merged 5 commits into from
Jan 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
232 changes: 232 additions & 0 deletions src/Client/GeminiClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@
<?php
/**
* Created by PhpStorm.
* User: daniel
* Date: 11/07/17
* Time: 11:43 AM
*/

namespace Islandora\Crayfish\Commons\Client;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\RequestException;
use Psr\Log\LoggerInterface;

/**
* Class GeminiClient
* @package Islandora\Milliner\Gemini
*/
class GeminiClient
{
/**
* @var \GuzzleHttp\Client
*/
protected $client;

/**
* @var \Psr\Log\LoggerInterface
*/
protected $log;

/**
* GeminiClient constructor.
* @param \GuzzleHttp\Client $client
* @param \Psr\Log\LoggerInterface $log
*/
public function __construct(Client $client, LoggerInterface $log)
{
$this->client = $client;
$this->log = $log;
}

public static function create($base_url, LoggerInterface $log)
{
$trimmed = trim($base_url);
$with_trailing_slash = rtrim($trimmed, '/') . '/';
return new GeminiClient(
new Client(['base_uri' => $with_trailing_slash]),
$log
);
}

/**
* Gets a pair of drupal/fedora urls for a UUID.
* @param $uuid
* @param $token
*
* @throws \GuzzleHttp\Exception\RequestException
*
* @return array|null
*/
public function getUrls(
$uuid,
$token = null
) {
try {
if (empty($token)) {
$response = $this->client->get($uuid);
} else {
$response = $this->client->get($uuid, [
'headers' => [
'Authorization' => $token,
],
]);
}

$this->log->debug("Gemini GET response", [
'uuid' => $uuid,
'response' => $response,
]);

return json_decode($response->getBody(), true);
} catch (RequestException $e) {
if ($e->getResponse()->getStatusCode() == 404) {
return null;
}

throw $e;
}
}

/**
* Mints a new Fedora URL for a UUID.
* @param $uuid
* @param $token
*
* @throws \GuzzleHttp\Exception\RequestException
*
* @return string
*/
public function mintFedoraUrl(
$uuid,
$token = null
) {
$headers = ['Content-Type' => 'text/plain'];

if (!empty($token)) {
$headers['Authorization'] = $token;
}

$response = $this->client->post('', [
'body' => $uuid,
'headers' => $headers,
]);

$this->log->debug("Gemini POST response", [
'uuid' => $uuid,
'response' => $response,
]);

return (string)$response->getBody();
}

/**
* Saves a pair of drupal/fedora urls for a UUID.
* @param $uuid
* @param $drupal
* @param $fedora
* @param $token
*
* @throws \GuzzleHttp\Exception\RequestException
*
* @return bool
*/
public function saveUrls(
$uuid,
$drupal,
$fedora,
$token = null
) {
$body = json_encode(['drupal' => $drupal, 'fedora' => $fedora]);

$headers = ['Content-Type' => 'application/json'];

if (!empty($token)) {
$headers['Authorization'] = $token;
}

$response = $this->client->put($uuid, [
'body' => $body,
'headers' => $headers,
]);

$this->log->debug("Gemini PUT response", [
'uuid' => $uuid,
'response' => $response,
]);

return true;
}

/**
* Deletes a pair of drupal/fedora urls for a UUID.
* @param $uuid
* @param $token
*
* @throws \GuzzleHttp\Exception\RequestException
*
* @return bool
*/
public function deleteUrls(
$uuid,
$token = null
) {
$headers = [];

if (!empty($token)) {
$headers['Authorization'] = $token;
}

$response = $this->client->delete($uuid, [
'headers' => $headers,
]);

$this->log->debug("Gemini DELETE response", [
'uuid' => $uuid,
'response' => $response,
]);

return true;
}

/**
* Find the URLs given one of them.
* @param $uri
* The Drupal or Fedora URI.
* @param null $token
* Authorization token.
*
* @return null|string[]
*
*/
public function findByUri(
$uri,
$token = null
) {
$headers['X-Islandora-URI'] = $uri;

if (!empty($token)) {
$headers["Authorization"] = $token;
}

try {
$response = $this->client->get('by_uri', [
'headers' => $headers,
]);
} catch (ClientException $e) {
// Got a 404
$this->log->debug('Gemini findByURI 404 response', [
'uri' => $uri,
]);
return null;
}

$this->log->debug("Gemini findByURI response", [
'uri' => $uri,
'response' => $response,
]);

return $response->getHeader('Location');
}
}
Loading