/** * @param string $id * @param string $secret * @param string $url */ public function __construct($id, $secret, $url) { $this->logger = new NullLogger(); if (!strlen($id) || !strlen($secret)) { throw new InvalidArgumentException('api_id and api_secret must both be provided'); } $validatedUrl = filter_var($url, FILTER_VALIDATE_URL); if (!$validatedUrl) { throw new InvalidArgumentException($url . ' is not a valid URL'); } $this->id = $id; $this->secret = $secret; $this->baseUrl = $validatedUrl . '/page/api/'; $handlerStack = HandlerStack::create(GuzzleHttp\choose_handler()); $handlerStack->push(Middleware::mapRequest(function (RequestInterface $request) { $uri = $request->getUri(); $query = new Query($uri->getQuery()); /* * Add id and version to the query */ $query = $query->merge(Query::createFromArray(['api_id' => $this->id, 'api_ver' => '2'])); /* * Add timestamp to the query */ if (!$query->hasKey('api_ts')) { $query = $query->merge(Query::createFromArray(['api_ts', time()])); } $query = $query->merge(Query::createFromArray(['api_mac' => $this->generateMac($uri->getPath(), $query)])); return $request->withUri($uri->withQuery((string) $query)); })); $this->guzzleClient = new GuzzleClient(['handler' => $handlerStack]); }
public static function create(callable $handler = null) : self { $stack = new self($handler ?: choose_handler()); $stack->push(Middleware::httpErrors()); $stack->push(Middleware::prepareBody()); return $stack; }
protected function _getGuzzleClient($baseUrl, $persistent = true) { if (!$persistent || !self::$_guzzleClientConnection) { $stack = HandlerStack::create(GuzzleHttp\choose_handler()); $stack->push($this->getSigningMiddleware(), 'sign'); self::$_guzzleClientConnection = new Client(['base_uri' => $baseUrl, 'handler' => $stack]); } return self::$_guzzleClientConnection; }