Example #1
0
 /**
  * Make all the requests and push responses (as objects if success) to self::responses["public" or "oauth"]
  */
 public static function run()
 {
     // Create Public and Oauth Clients
     self::$clientPublic = new Client(Endpoint::headers());
     self::$clientOauth = new Client(Endpoint::headers(TRUE));
     // Public
     $poolPublic = new Pool(self::$clientPublic, self::$requests["public"], ["concurrency" => 20, "fulfilled" => function (Response $response, $index) {
         // On success we json_decode response
         $data = json_decode($response->getBody()->getContents(), TRUE);
         // Create object and push it to responses success
         $factory = OpenCrest::getFactory();
         $object = $factory->create(self::$objects["public"][$index], $data, $response);
         array_push(self::$responses["public"]["success"], $object);
     }, "rejected" => function ($reason, $index) {
         // On failure we push it to responses rejected
         array_push(self::$responses["public"]["rejected"], ["reason" => $reason, "index" => $index]);
     }]);
     self::$promise["public"] = $poolPublic->promise();
     self::$promise["public"]->wait();
     // Oauth
     $poolOauth = new Pool(self::$clientOauth, self::$requests["oauth"], ["concurrency" => 20, "fulfilled" => function (Response $response, $index) {
         // On success we json_decode response
         $data = json_decode($response->getBody()->getContents(), TRUE);
         // Create object and push it to responses success
         $factory = OpenCrest::getFactory();
         $object = $factory->create(self::$objects["oauth"][$index], $data, $response);
         array_push(self::$responses["oauth"]["success"], $object);
     }, "rejected" => function ($reason, $index) {
         // On failure we push it to responses rejected
         array_push(self::$responses["oauth"]["rejected"], ["reason" => $reason, "index" => $index]);
     }]);
     self::$promise["oauth"] = $poolOauth->promise();
     self::$promise["oauth"]->wait();
 }
Example #2
0
 /**
  * Endpoint constructor
  *
  * @param ObjectInterface $object
  * @throws OAuthException
  */
 public function __construct(ObjectInterface $object)
 {
     $this->object = $object;
     $oauth = $this->object->getAttribute("oauth");
     $this->client = new GuzzleHttp\Client($this->headers($oauth));
     // When making AuthRequest but token isn't provided, throw OAuthException
     if ($oauth and !OpenCrest::getToken()) {
         throw new OAuthException();
     }
     $this->factory = OpenCrest::getFactory();
 }