summaryrefslogtreecommitdiff
path: root/server/vendor/php-opencloud/common/src/Common/Transport
diff options
context:
space:
mode:
authorEoleDev <root@serverpc.home>2016-03-09 15:37:42 +0100
committerEoleDev <root@serverpc.home>2016-03-09 15:37:42 +0100
commit1d3ed3af6d57316640c143002ddf80d61e6c098a (patch)
tree97ac49bc7ff0f16150aefee821da3557dc0d0644 /server/vendor/php-opencloud/common/src/Common/Transport
parentd69adc4f9d1f6019927de235ef84885c68e6e508 (diff)
parent08ea5ef31abcc4e23a39a780cacb64fa27f19194 (diff)
Merge branch 'Evan' into develop
Diffstat (limited to 'server/vendor/php-opencloud/common/src/Common/Transport')
-rw-r--r--server/vendor/php-opencloud/common/src/Common/Transport/HandlerStack.php19
-rw-r--r--server/vendor/php-opencloud/common/src/Common/Transport/JsonSerializer.php95
-rw-r--r--server/vendor/php-opencloud/common/src/Common/Transport/Middleware.php94
-rw-r--r--server/vendor/php-opencloud/common/src/Common/Transport/RequestSerializer.php85
-rw-r--r--server/vendor/php-opencloud/common/src/Common/Transport/Utils.php88
5 files changed, 381 insertions, 0 deletions
diff --git a/server/vendor/php-opencloud/common/src/Common/Transport/HandlerStack.php b/server/vendor/php-opencloud/common/src/Common/Transport/HandlerStack.php
new file mode 100644
index 0000000..7d83875
--- /dev/null
+++ b/server/vendor/php-opencloud/common/src/Common/Transport/HandlerStack.php
@@ -0,0 +1,19 @@
+<?php
+
+namespace OpenCloud\Common\Transport;
+
+use function GuzzleHttp\choose_handler;
+use GuzzleHttp\HandlerStack as GuzzleStack;
+
+class HandlerStack extends GuzzleStack
+{
+ public static function create(callable $handler = null)
+ {
+ $stack = new self($handler ?: choose_handler());
+
+ $stack->push(Middleware::httpErrors());
+ $stack->push(Middleware::prepareBody());
+
+ return $stack;
+ }
+}
diff --git a/server/vendor/php-opencloud/common/src/Common/Transport/JsonSerializer.php b/server/vendor/php-opencloud/common/src/Common/Transport/JsonSerializer.php
new file mode 100644
index 0000000..11b31d3
--- /dev/null
+++ b/server/vendor/php-opencloud/common/src/Common/Transport/JsonSerializer.php
@@ -0,0 +1,95 @@
+<?php
+
+namespace OpenCloud\Common\Transport;
+
+use OpenCloud\Common\Api\Parameter;
+use OpenCloud\Common\JsonPath;
+
+/**
+ * Class responsible for populating the JSON body of a {@see GuzzleHttp\Message\Request} object.
+ *
+ * @package OpenCloud\Common\Transport
+ */
+class JsonSerializer
+{
+ /**
+ * Populates the actual value into a JSON field, i.e. it has reached the end of the line and no
+ * further nesting is required.
+ *
+ * @param Parameter $param The schema that defines how the JSON field is being populated
+ * @param mixed $userValue The user value that is populating a JSON field
+ * @param array $json The existing JSON structure that will be populated
+ *
+ * @return array|mixed
+ */
+ private function stockValue(Parameter $param, $userValue, $json)
+ {
+ $name = $param->getName();
+ if ($path = $param->getPath()) {
+ $jsonPath = new JsonPath($json);
+ $jsonPath->set(sprintf("%s.%s", $path, $name), $userValue);
+ $json = $jsonPath->getStructure();
+ } elseif ($name) {
+ $json[$name] = $userValue;
+ } else {
+ $json[] = $userValue;
+ }
+
+ return $json;
+ }
+
+ /**
+ * Populates a value into an array-like structure.
+ *
+ * @param Parameter $param The schema that defines how the JSON field is being populated
+ * @param mixed $userValue The user value that is populating a JSON field
+ *
+ * @return array|mixed
+ */
+ private function stockArrayJson(Parameter $param, $userValue)
+ {
+ $elems = [];
+ foreach ($userValue as $item) {
+ $elems = $this->stockJson($param->getItemSchema(), $item, $elems);
+ }
+ return $elems;
+ }
+
+ /**
+ * Populates a value into an object-like structure.
+ *
+ * @param Parameter $param The schema that defines how the JSON field is being populated
+ * @param mixed $userValue The user value that is populating a JSON field
+ *
+ * @return array
+ */
+ private function stockObjectJson(Parameter $param, $userValue)
+ {
+ $object = [];
+ foreach ($userValue as $key => $val) {
+ $object = $this->stockJson($param->getProperty($key), $val, $object);
+ }
+ return $object;
+ }
+
+ /**
+ * A generic method that will populate a JSON structure with a value according to a schema. It
+ * supports multiple types and will delegate accordingly.
+ *
+ * @param Parameter $param The schema that defines how the JSON field is being populated
+ * @param mixed $userValue The user value that is populating a JSON field
+ * @param array $json The existing JSON structure that will be populated
+ *
+ * @return array
+ */
+ public function stockJson(Parameter $param, $userValue, $json)
+ {
+ if ($param->isArray()) {
+ $userValue = $this->stockArrayJson($param, $userValue);
+ } elseif ($param->isObject()) {
+ $userValue = $this->stockObjectJson($param, $userValue);
+ }
+ // Populate the final value
+ return $this->stockValue($param, $userValue, $json);
+ }
+}
diff --git a/server/vendor/php-opencloud/common/src/Common/Transport/Middleware.php b/server/vendor/php-opencloud/common/src/Common/Transport/Middleware.php
new file mode 100644
index 0000000..916ff22
--- /dev/null
+++ b/server/vendor/php-opencloud/common/src/Common/Transport/Middleware.php
@@ -0,0 +1,94 @@
+<?php
+
+namespace OpenCloud\Common\Transport;
+
+use function GuzzleHttp\Psr7\modify_request;
+use GuzzleHttp\MessageFormatter;
+use GuzzleHttp\Middleware as GuzzleMiddleware;
+use OpenCloud\Common\Auth\AuthHandler;
+use OpenCloud\Common\Auth\Token;
+use OpenCloud\Common\Error\Builder;
+use Psr\Http\Message\ResponseInterface;
+
+final class Middleware
+{
+ /**
+ * @return callable
+ */
+ public static function httpErrors()
+ {
+ return function (callable $handler) {
+ return function ($request, array $options) use ($handler) {
+ return $handler($request, $options)->then(
+ function (ResponseInterface $response) use ($request, $handler) {
+ if ($response->getStatusCode() < 400) {
+ return $response;
+ }
+ throw (new Builder())->httpError($request, $response);
+ }
+ );
+ };
+ };
+ }
+
+ /**
+ * @param callable $tokenGenerator
+ * @param Token $token
+ *
+ * @return callable
+ */
+ public static function authHandler(callable $tokenGenerator, Token $token = null)
+ {
+ return function (callable $handler) use ($tokenGenerator, $token) {
+ return new AuthHandler($handler, $tokenGenerator, $token);
+ };
+ }
+
+ /**
+ * @codeCoverageIgnore
+ */
+ public static function history(array &$container)
+ {
+ return GuzzleMiddleware::history($container);
+ }
+
+ /**
+ * @codeCoverageIgnore
+ */
+ public static function retry(callable $decider, callable $delay = null)
+ {
+ return GuzzleMiddleware::retry($decider, $delay);
+ }
+
+ /**
+ * @codeCoverageIgnore
+ */
+ public static function log(LoggerInterface $logger, MessageFormatter $formatter, $logLevel = LogLevel::INFO)
+ {
+ return GuzzleMiddleware::log($logger, $formatter, $logLevel);
+ }
+
+ /**
+ * @codeCoverageIgnore
+ */
+ public static function prepareBody()
+ {
+ return GuzzleMiddleware::prepareBody();
+ }
+
+ /**
+ * @codeCoverageIgnore
+ */
+ public static function mapRequest(callable $fn)
+ {
+ return GuzzleMiddleware::mapRequest($fn);
+ }
+
+ /**
+ * @codeCoverageIgnore
+ */
+ public static function mapResponse(callable $fn)
+ {
+ return GuzzleMiddleware::mapResponse($fn);
+ }
+}
diff --git a/server/vendor/php-opencloud/common/src/Common/Transport/RequestSerializer.php b/server/vendor/php-opencloud/common/src/Common/Transport/RequestSerializer.php
new file mode 100644
index 0000000..30f04af
--- /dev/null
+++ b/server/vendor/php-opencloud/common/src/Common/Transport/RequestSerializer.php
@@ -0,0 +1,85 @@
+<?php
+
+namespace OpenCloud\Common\Transport;
+
+use function GuzzleHttp\uri_template;
+use function GuzzleHttp\Psr7\build_query;
+use function GuzzleHttp\Psr7\modify_request;
+use OpenCloud\Common\Api\Operation;
+use OpenCloud\Common\Api\Parameter;
+
+class RequestSerializer
+{
+ private $jsonSerializer;
+
+ public function __construct(JsonSerializer $jsonSerializer = null)
+ {
+ $this->jsonSerializer = $jsonSerializer ?: new JsonSerializer();
+ }
+
+ public function serializeOptions(Operation $operation, array $userValues = [])
+ {
+ $options = ['headers' => []];
+
+ foreach ($userValues as $paramName => $paramValue) {
+ if (null === ($schema = $operation->getParam($paramName))) {
+ continue;
+ }
+
+ $method = sprintf('stock%s', ucfirst($schema->getLocation()));
+ $this->$method($schema, $paramValue, $options);
+ }
+
+ if (!empty($options['json'])) {
+ if ($key = $operation->getJsonKey()) {
+ $options['json'] = [$key => $options['json']];
+ }
+ if (strpos(json_encode($options['json']), '\/') !== false) {
+ $options['body'] = json_encode($options['json'], JSON_UNESCAPED_SLASHES);
+ $options['headers']['Content-Type'] = 'application/json';
+ unset($options['json']);
+ }
+ }
+
+ return $options;
+ }
+
+ private function stockUrl()
+ {
+ }
+
+ private function stockQuery(Parameter $schema, $paramValue, array &$options)
+ {
+ $options['query'][$schema->getName()] = $paramValue;
+ }
+
+ private function stockHeader(Parameter $schema, $paramValue, array &$options)
+ {
+ $paramName = $schema->getName();
+
+ if (stripos($paramName, 'metadata') !== false) {
+ return $this->stockMetadataHeader($schema, $paramValue, $options);
+ }
+
+ $options['headers'] += is_scalar($paramValue) ? [$schema->getPrefixedName() => $paramValue] : [];
+ }
+
+ private function stockMetadataHeader(Parameter $schema, $paramValue, array &$options)
+ {
+ foreach ($paramValue as $key => $keyVal) {
+ $schema = $schema->getItemSchema() ?: new Parameter(['prefix' => $schema->getPrefix(), 'name' => $key]);
+ $this->stockHeader($schema, $keyVal, $options);
+ }
+ }
+
+ private function stockJson(Parameter $schema, $paramValue, array &$options)
+ {
+ $json = isset($options['json']) ? $options['json'] : [];
+ $options['json'] = $this->jsonSerializer->stockJson($schema, $paramValue, $json);
+ }
+
+ private function stockRaw(Parameter $schema, $paramValue, array &$options)
+ {
+ $options['body'] = $paramValue;
+ }
+}
diff --git a/server/vendor/php-opencloud/common/src/Common/Transport/Utils.php b/server/vendor/php-opencloud/common/src/Common/Transport/Utils.php
new file mode 100644
index 0000000..c2a2dc1
--- /dev/null
+++ b/server/vendor/php-opencloud/common/src/Common/Transport/Utils.php
@@ -0,0 +1,88 @@
+<?php
+
+namespace OpenCloud\Common\Transport;
+
+use function GuzzleHttp\Psr7\uri_for;
+use Psr\Http\Message\ResponseInterface;
+use Psr\Http\Message\UriInterface;
+
+class Utils
+{
+ public static function jsonDecode(ResponseInterface $response, $assoc = true)
+ {
+ $jsonErrors = [
+ JSON_ERROR_DEPTH => 'JSON_ERROR_DEPTH - Maximum stack depth exceeded',
+ JSON_ERROR_STATE_MISMATCH => 'JSON_ERROR_STATE_MISMATCH - Underflow or the modes mismatch',
+ JSON_ERROR_CTRL_CHAR => 'JSON_ERROR_CTRL_CHAR - Unexpected control character found',
+ JSON_ERROR_SYNTAX => 'JSON_ERROR_SYNTAX - Syntax error, malformed JSON',
+ JSON_ERROR_UTF8 => 'JSON_ERROR_UTF8 - Malformed UTF-8 characters, possibly incorrectly encoded'
+ ];
+
+ $responseBody = (string) $response->getBody();
+
+ if (strlen($responseBody) === 0) {
+ return $responseBody;
+ }
+
+ $data = json_decode($responseBody, $assoc);
+
+ if (JSON_ERROR_NONE !== json_last_error()) {
+ $last = json_last_error();
+ throw new \InvalidArgumentException(
+ 'Unable to parse JSON data: ' . (isset($jsonErrors[$last]) ? $jsonErrors[$last] : 'Unknown error')
+ );
+ }
+
+ return $data;
+ }
+
+ /**
+ * Method for flattening a nested array.
+ *
+ * @param array $data The nested array
+ * @param null $key The key to extract
+ *
+ * @return array
+ */
+ public static function flattenJson($data, $key = null)
+ {
+ return (!empty($data) && $key && isset($data[$key])) ? $data[$key] : $data;
+ }
+
+ /**
+ * Method for normalize an URL string.
+ *
+ * Append the http:// prefix if not present, and add a
+ * closing url separator when missing.
+ *
+ * @param string $url The url representation.
+ *
+ * @return string
+ */
+ public static function normalizeUrl($url)
+ {
+ if (strpos($url, 'http') === false) {
+ $url = 'http://' . $url;
+ }
+
+ return rtrim($url, '/') . '/';
+ }
+
+ /**
+ * Add an unlimited list of paths to a given URI.
+ *
+ * @param UriInterface $uri
+ * @param ...$paths
+ *
+ * @return UriInterface
+ */
+ public static function addPaths(UriInterface $uri, ...$paths)
+ {
+ return uri_for(rtrim((string) $uri, '/') . '/' . implode('/', $paths));
+ }
+
+ public static function appendPath(UriInterface $uri, $path)
+ {
+ return uri_for(rtrim((string) $uri, '/') . '/' . $path);
+ }
+}