From 0de70b2e5169fec97235011a4e40819c15247b9d Mon Sep 17 00:00:00 2001 From: Daniel Lamb Date: Mon, 10 Apr 2017 00:38:32 -0300 Subject: [PATCH 1/2] Passing through headers --- src/Chullo.php | 37 +++++++++++++++++++++++++------------ src/FedoraApi.php | 23 +++++++++++++++-------- src/IFedoraApi.php | 18 ++++++++++++++---- src/IFedoraClient.php | 4 +++- 4 files changed, 57 insertions(+), 25 deletions(-) diff --git a/src/Chullo.php b/src/Chullo.php index ab6c179..c1878a0 100644 --- a/src/Chullo.php +++ b/src/Chullo.php @@ -80,14 +80,17 @@ public function getResource( * Gets a Fedora resource's headers. * * @param string $uri Resource URI + * @param array $headers HTTP Headers * * @return array Headers of a resource, null on failure */ public function getResourceHeaders( - $uri = "" + $uri = "", + $headers = [] ) { $response = $this->api->getResourceHeaders( - $uri + $uri, + $headers ); if ($response->getStatusCode() != 200) { @@ -101,13 +104,17 @@ public function getResourceHeaders( * Gets information about the supported HTTP methods, etc., for a Fedora resource. * * @param string $uri Resource URI + * @param array $headers HTTP Headers * * @return string Options of a resource. */ - public function getResourceOptions($uri = "") - { + public function getResourceOptions( + $uri = "", + $headers = [] + ) { $response = $this->api->getResourceOptions( - $uri + $uri, + $headers ); return $response->getHeaders(); @@ -193,12 +200,14 @@ public function saveResource( * * @param string $uri Resource URI * @param EasyRdf_Resource $graph Graph to save + * @param array $headers HTTP Headers * * @return boolean True if successful */ public function saveGraph( $uri, - \EasyRdf_Graph $graph + \EasyRdf_Graph $graph, + $headers = [] ) { // Serialze the rdf. $turtle = $graph->serialise('turtle'); @@ -206,14 +215,15 @@ public function saveGraph( // Checksum it. $checksum_value = sha1($turtle); + // Set headers. + $headers['Content-Type' => 'text/turtle']; + $headers['digest' => 'sha1='.$checksum_value]; + // Save it. return $this->saveResource( $uri, $turtle, - [ - 'Content-Type' => 'text/turtle', - 'digest' => 'sha1='.$checksum_value - ] + $headers ); } @@ -244,14 +254,17 @@ public function modifyResource( * Issues a DELETE request to Fedora. * * @param string $uri Resource URI + * @param array $headers HTTP Headers * * @return boolean True if successful */ public function deleteResource( - $uri + $uri, + $headers = [] ) { $response = $this->api->deleteResource( - $uri + $uri, + $headers ); return $response->getStatusCode() == 204; diff --git a/src/FedoraApi.php b/src/FedoraApi.php index 9e5b01a..f55a9d9 100644 --- a/src/FedoraApi.php +++ b/src/FedoraApi.php @@ -86,18 +86,20 @@ public function getResource( * Gets a Fedora resoure's headers. * * @param string $uri Resource URI + * @param array $headers HTTP Headers * * @return ResponseInterface */ public function getResourceHeaders( - $uri = "" + $uri = "", + $headers = [] ) { // Send the request. return $this->client->request( 'HEAD', $uri, - ['http_errors' => false] + ['http_errors' => false, 'headers' => $headers] ); } @@ -105,15 +107,18 @@ public function getResourceHeaders( * Gets information about the supported HTTP methods, etc., for a Fedora resource. * * @param string $uri Resource URI + * @param array $headers HTTP Headers * * @return ResponseInterface */ - public function getResourceOptions($uri = "") - { + public function getResourceOptions( + $uri = "", + $headers = [] + ) { return $this->client->request( 'OPTIONS', $uri, - ['http_errors' => false] + ['http_errors' => false, 'headers' => $headers] ); } @@ -138,7 +143,7 @@ public function createResource( // Set headers. $options['headers'] = $headers; - + return $this->client->request( 'POST', $uri, @@ -209,13 +214,15 @@ public function modifyResource( * Issues a DELETE request to Fedora. * * @param string $uri Resource URI + * @param array $headers HTTP Headers * * @return ResponseInterface */ public function deleteResource( - $uri + $uri, + $headers = [] ) { - $options = ['http_errors' => false]; + $options = ['http_errors' => false, 'headers' => $headers]; return $this->client->request( 'DELETE', diff --git a/src/IFedoraApi.php b/src/IFedoraApi.php index ebb05c9..50b7c43 100644 --- a/src/IFedoraApi.php +++ b/src/IFedoraApi.php @@ -23,7 +23,7 @@ /** * Interface for Fedora interaction. All functions return a PSR-7 response. - */ + */ interface IFedoraApi { /** @@ -43,20 +43,28 @@ public function getResource( $uri = "", $headers = [] ); + /** * Gets a Fedora resoure's headers. * * @param string $uri Resource URI + * @param array $headers HTTP Headers */ public function getResourceHeaders( - $uri = "" + $uri = "", + $headers = [] ); + /** * Gets information about the supported HTTP methods, etc., for a Fedora resource. * * @param string $uri Resource URI + * @param array $headers HTTP Headers */ - public function getResourceOptions($uri = ""); + public function getResourceOptions( + $uri = "", + $headers = [] + ); /** * Creates a new resource in Fedora. @@ -101,8 +109,10 @@ public function modifyResource( * Issues a DELETE request to Fedora. * * @param string $uri Resource URI + * @param array $headers HTTP Headers */ public function deleteResource( - $uri + $uri = "", + $headers = [] ); } diff --git a/src/IFedoraClient.php b/src/IFedoraClient.php index a1d7d1f..ecc0b66 100644 --- a/src/IFedoraClient.php +++ b/src/IFedoraClient.php @@ -42,11 +42,13 @@ public function getGraph( * * @param string $uri Resource URI * @param EasyRdf_Resource $rdf RDF to save + * @param array $headers HTTP Headers * * @return null */ public function saveGraph( $uri, - \EasyRdf_Graph $graph + \EasyRdf_Graph $graph, + $headers = [] ); } From d301388564d361e16b29612d671026227f0ae12f Mon Sep 17 00:00:00 2001 From: Daniel Lamb Date: Wed, 12 Apr 2017 13:17:35 -0300 Subject: [PATCH 2/2] Tests are passing --- src/Chullo.php | 6 +++--- src/FedoraApi.php | 6 ++++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/Chullo.php b/src/Chullo.php index c1878a0..e34801c 100644 --- a/src/Chullo.php +++ b/src/Chullo.php @@ -216,8 +216,8 @@ public function saveGraph( $checksum_value = sha1($turtle); // Set headers. - $headers['Content-Type' => 'text/turtle']; - $headers['digest' => 'sha1='.$checksum_value]; + $headers['Content-Type'] = 'text/turtle'; + $headers['digest'] = 'sha1=' . $checksum_value; // Save it. return $this->saveResource( @@ -259,7 +259,7 @@ public function modifyResource( * @return boolean True if successful */ public function deleteResource( - $uri, + $uri = '', $headers = [] ) { $response = $this->api->deleteResource( diff --git a/src/FedoraApi.php b/src/FedoraApi.php index f55a9d9..2c1bda9 100644 --- a/src/FedoraApi.php +++ b/src/FedoraApi.php @@ -45,7 +45,9 @@ public function __construct(Client $client) */ public static function create($fedora_rest_url) { - $guzzle = new Client(['base_uri' => $fedora_rest_url]); + $normalized = rtrim($fedora_rest_url); + $normalized = rtrim($normalized, '/') . '/'; + $guzzle = new Client(['base_uri' => $normalized]); return new static($guzzle); } @@ -219,7 +221,7 @@ public function modifyResource( * @return ResponseInterface */ public function deleteResource( - $uri, + $uri = '', $headers = [] ) { $options = ['http_errors' => false, 'headers' => $headers];