Overview

Namespaces

  • Crowdsdom
    • Client
      • Exceptions
    • Labor
      • Models
        • Base
  • PHP

Classes

  • API
  • Client
  • Overview
  • Namespace
  • Class
  • Tree
 1: <?php
 2: 
 3: namespace Crowdsdom\Client;
 4: 
 5: use Crowdsdom\Auth;
 6: use Crowdsdom\Crowdsdom;
 7: use GuzzleHttp\HandlerStack;
 8: use Psr\Http\Message\RequestInterface;
 9: 
10: /**
11:  * Class Client
12:  * @package Crowdsdom\Client
13:  */
14: class Client
15: {
16: 
17:     /**
18:      * @var string
19:      */
20:     protected $host;
21: 
22:     /**
23:      * @var string
24:      */
25:     protected $version;
26: 
27:     /**
28:      * @var Auth
29:      */
30:     protected $auth;
31: 
32:     /**
33:      * Client constructor.
34:      * @param string $host
35:      * @param string $version
36:      * @param array $guzzleOptions
37:      */
38:     public function __construct(
39:         Auth &$auth,
40:         $host,
41:         $version = Crowdsdom::DEFAULT_API_VERSION,
42:         array $guzzleOptions = []
43:     ) {
44:         $this->auth = $auth;
45:         $this->host = $host;
46:         $this->version = $version;
47: 
48:         $stack = HandlerStack::create();
49:         $stack->push($auth->authMiddleware());
50:         $stack->push(static::versionMiddleware($version));
51: 
52:         $this->guzzle = new \GuzzleHttp\Client(array_merge([
53:             'base_uri' => $host,
54:             'handler' => $stack
55:         ], $guzzleOptions));
56:     }
57: 
58:     /**
59:      * @return \GuzzleHttp\Client
60:      */
61:     public function getGuzzle()
62:     {
63:         return $this->guzzle;
64:     }
65: 
66:     public static function versionMiddleware($version)
67:     {
68:         return function (callable $handler) use ($version) {
69:             return function (RequestInterface $request, array $options) use ($handler, $version) {
70:                 $prefix = $version ? "/$version" : '';
71:                 $uri = $request->getUri();
72:                 $uri = $uri->withPath($prefix . $uri->getPath());
73:                 $request = $request->withUri($uri);
74:                 return $handler($request, $options);
75:             };
76:         };
77:     }
78: 
79: }
80: 
API documentation generated by ApiGen 2.8.0