|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | +/* |
| 5 | + * This file is part of eelly package. |
| 6 | + * |
| 7 | + * (c) eelly.com |
| 8 | + * |
| 9 | + * For the full copyright and license information, please view the LICENSE |
| 10 | + * file that was distributed with this source code. |
| 11 | + */ |
| 12 | + |
| 13 | +namespace sixdec\SDK; |
| 14 | + |
| 15 | +use Eelly\OAuth2\Client\Provider\EellyProvider; |
| 16 | +use GuzzleHttp\Psr7\MultipartStream; |
| 17 | +use Psr\Http\Message\UploadedFileInterface; |
| 18 | + |
| 19 | +class EellyClient |
| 20 | +{ |
| 21 | + private const URI = [ |
| 22 | + 'logger' => 'http://api.eelly.dev', |
| 23 | + 'member' => 'http://api.eelly.dev', |
| 24 | + 'oauth' => 'http://api.eelly.dev', |
| 25 | + ]; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var string |
| 29 | + */ |
| 30 | + public static $traceId; |
| 31 | + |
| 32 | + private static $services = []; |
| 33 | + |
| 34 | + /** |
| 35 | + * @var EellyProvider |
| 36 | + */ |
| 37 | + private $provider; |
| 38 | + |
| 39 | + /** |
| 40 | + * @var EellyClient |
| 41 | + */ |
| 42 | + private static $self; |
| 43 | + |
| 44 | + /** |
| 45 | + * @param array $options |
| 46 | + */ |
| 47 | + final private function __construct(array $options) |
| 48 | + { |
| 49 | + $this->provider = new EellyProvider($options); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * @param array $options |
| 54 | + * |
| 55 | + * @return self |
| 56 | + */ |
| 57 | + public static function init(array $options): self |
| 58 | + { |
| 59 | + if (null === self::$self) { |
| 60 | + self::$self = new self($options); |
| 61 | + } |
| 62 | + |
| 63 | + return self::$self; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * @return \Eelly\OAuth2\Client\Provider\EellyProvider |
| 68 | + */ |
| 69 | + public function getProvider() |
| 70 | + { |
| 71 | + return $this->provider; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * @param string $traceId |
| 76 | + */ |
| 77 | + public function setTraceId(string $traceId): void |
| 78 | + { |
| 79 | + self::$traceId = $traceId; |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * @param mixed $grant |
| 84 | + * |
| 85 | + * @return \League\OAuth2\Client\Token\AccessToken |
| 86 | + */ |
| 87 | + public function getAccessToken($grant, array $options = []) |
| 88 | + { |
| 89 | + // TODO cache |
| 90 | + return $this->provider->getAccessToken($grant, $options); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * @param string $uri |
| 95 | + * @param string $method |
| 96 | + * @param mixed ...$args |
| 97 | + * |
| 98 | + * @return mixed |
| 99 | + */ |
| 100 | + public static function request(string $uri, string $method, ...$args) |
| 101 | + { |
| 102 | + if (null === self::$self) { |
| 103 | + throw new \ErrorException('uninitial eelly client'); |
| 104 | + } |
| 105 | + $client = self::$self; |
| 106 | + $accessToken = $client->getAccessToken('client_credentials'); |
| 107 | + |
| 108 | + list($serviceName) = explode('/', $uri); |
| 109 | + $uri = self::URI[$serviceName].'/'.$uri.'/'.$method; |
| 110 | + $stream = new MultipartStream($client->paramsToMultipart($args)); |
| 111 | + $provider = $client->getProvider(); |
| 112 | + $options = [ |
| 113 | + 'body' => $stream, |
| 114 | + ]; |
| 115 | + if (!empty(self::$traceId)) { |
| 116 | + $options['headers'] = [ |
| 117 | + 'traceId' => self::$traceId, |
| 118 | + ]; |
| 119 | + } |
| 120 | + $request = $provider->getAuthenticatedRequest(EellyProvider::METHOD_POST, $uri, $accessToken->getToken(), $options); |
| 121 | + |
| 122 | + $response = $provider->getResponse($request); |
| 123 | + $class = $response->getHeader('ReturnType'); |
| 124 | + if (!empty($class)) { |
| 125 | + $returnType = $class[0]; |
| 126 | + if (class_exists($returnType)) { |
| 127 | + $array = json_decode((string) $response->getBody(), true); |
| 128 | + $object = $returnType::hydractor($array); |
| 129 | + } elseif ('array' == $returnType) { |
| 130 | + $object = json_decode((string) $response->getBody(), true); |
| 131 | + } else { |
| 132 | + $object = (string) $response->getBody(); |
| 133 | + settype($object, $returnType); |
| 134 | + } |
| 135 | + } else { |
| 136 | + $object = (string) $response->getBody(); |
| 137 | + } |
| 138 | + |
| 139 | + return $object; |
| 140 | + } |
| 141 | + |
| 142 | + protected function paramsToMultipart($params, $prefix = null) |
| 143 | + { |
| 144 | + $multipart = []; |
| 145 | + foreach ($params as $key => $value) { |
| 146 | + $p = null == $prefix ? $key : $prefix.'['.$key.']'; |
| 147 | + if ($value instanceof UploadedFileInterface) { |
| 148 | + $multipart[] = [ |
| 149 | + 'name' => $p, |
| 150 | + 'contents' => $value->getStream(), |
| 151 | + ]; |
| 152 | + } elseif (is_array($value)) { |
| 153 | + $parentMultipart = $this->paramsToMultipart($value, $p); |
| 154 | + foreach ($parentMultipart as $part) { |
| 155 | + $multipart[] = $part; |
| 156 | + } |
| 157 | + } else { |
| 158 | + $multipart[] = [ |
| 159 | + 'name' => $p, |
| 160 | + 'contents' => $value, |
| 161 | + ]; |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + return $multipart; |
| 166 | + } |
| 167 | +} |
0 commit comments