Exemplo n.º 1
0
 /**
  * Perform a request.
  *
  * @param  string $uri
  * @param  array  $uriParams
  * @param  string $body
  * @param  array  $headers
  * @return GraphCommons\Http\Response
  * @throws \InvalidArgumentException,
  *         GraphCommons\Http\Exception\Request, GraphCommons\Util\JsonException
  */
 public final function request($uri, array $uriParams = null, $body = '', array $headers = null)
 {
     // match for a valid request i.e: GET /foo
     preg_match('~^([a-z]+)\\s+(/.*)~i', $uri, $match);
     if (!isset($match[1], $match[2])) {
         throw new \InvalidArgumentException('Usage: <REQUEST METHOD> <REQUEST URI>');
     }
     $uri = sprintf('%s/%s/%s', $this->graphCommons->apiUrl, $this->graphCommons->apiVersion, trim($match[2]));
     $uri = preg_replace('~(^|[^:])//+~', '\\1/', trim($uri, '/'));
     $this->request->setMethod($match[1])->setUri($uri, (array) $uriParams);
     if (!empty($headers)) {
         foreach ($headers as $key => $value) {
             $this->request->setHeader(trim($key), $value);
         }
     }
     $requestMethod = $this->request->getMethod();
     if ($requestMethod == Request::METHOD_POST || $requestMethod == Request::METHOD_PUT) {
         // set body stuff
         $body = trim($body);
         $bodyLength = strlen($body);
         $this->request->setBody($body);
         $this->request->setBodyLength($bodyLength);
         // set content headers stuff
         $this->request->setHeader('Content-Type', 'application/json');
         $this->request->setHeader('Content-Length', (string) $bodyLength);
     }
     $result = $this->request->send();
     if ($result === null) {
         $fail = $this->request->getFail();
         throw new RequestException(sprintf('HTTP error: code(%d) message(%s)', $fail['code'], $fail['message']), $fail['code']);
     }
     unset($headers, $body);
     // split headers/body pairs
     @(list($headers, $body) = explode("\r\n\r\n", $result, 2));
     if (!isset($headers)) {
         throw new ResponseException('No headers received from server!');
     }
     if (!isset($body)) {
         throw new ResponseException('No body received from server!');
     }
     // parse response headers
     $headers = Util::parseResponseHeaders($headers);
     if (isset($headers['status'])) {
         $this->response->setStatus($headers['status']);
         $this->response->setStatusCode($headers['status_code']);
         $this->response->setStatusText($headers['status_text']);
     }
     $this->response->setHeaders($headers);
     $this->response->setBody($body);
     $json = new Json($body);
     // render response body
     $bodyData = $json->decode(true);
     if ($json->hasError()) {
         $jsonError = $json->getError();
         throw new JsonException(sprintf('JSON error: code(%d) message(%s)', $jsonError['code'], $jsonError['message']), $jsonError['code']);
     }
     $this->response->setBodyData($bodyData);
     return $this->response;
 }
Exemplo n.º 2
0
 /**
  * Get failure.
  *
  * @return array
  */
 public final function getFail()
 {
     // set default
     $fail = array('code' => Exception::UNKNOWN_ERROR_CODE, 'message' => Exception::UNKNOWN_ERROR_MESSAGE);
     if ($this->type == self::TYPE_REQUEST) {
         if ($this->isFail()) {
             $fail['code'] = $this->getFailCode();
             $fail['message'] = $this->getFailText();
         }
     } elseif ($this->type == self::TYPE_RESPONSE) {
         $bodyData = Util::toObject((array) $this->bodyData, false);
         // check first body for errors (differs by requests)
         if (isset($bodyData->msg)) {
             $fail['code'] = $this->getStatusCode();
             $fail['message'] = $bodyData->msg;
         } elseif (isset($bodyData->status, $bodyData->error)) {
             $fail['code'] = $bodyData->status;
             $fail['message'] = $bodyData->error;
         } elseif ($this->getStatusCode() >= 400) {
             $fail['code'] = $this->getStatusCode();
             $fail['message'] = $this->getStatusText();
         }
     }
     return $fail;
 }
 /**
  * Create a signal collection from JSON string.
  *
  * @param  string $json
  * @return GraphCommons\Graph\SignalCollection
  * @throws GraphCommons\Util\JsonException, \InvalidArgumentException
  */
 public static final function fromJson($json)
 {
     $json = new Json($json);
     if ($json->hasError()) {
         $jsonError = $json->getError();
         throw new JsonException(sprintf('JSON error: code(%d) message(%s)', $jsonError['code'], $jsonError['message']), $jsonError['code']);
     }
     $data = $json->decode(true);
     if (!isset($data['signals'])) {
         throw new \InvalidArgumentException("'signals' field is required!");
     }
     $array = array();
     foreach ($data['signals'] as $i => $signal) {
         if (!isset($signal['action'])) {
             throw new \InvalidArgumentException("Signal 'action' and 'parameters' fields are required!");
         }
         $array[$i]['action'] = Signal::detectAction(Util::arrayPick($signal, 'action'));
         foreach ($signal as $key => $value) {
             $array[$i]['parameters'][$key] = $value;
         }
     }
     return self::fromArray($array);
 }
 /**
  * Constructor.
  *
  * @param string $apiKey
  * @param array  $config
  */
 public final function __construct($apiKey, array $config = [])
 {
     $this->api = new GraphCommonsApi($this);
     // check user config options
     if (isset($config['api_url'])) {
         $this->apiUrl = Util::arrayPick($config, 'api_url');
     }
     if (isset($config['api_version'])) {
         $this->apiVersion = Util::arrayPick($config, 'api_version');
     }
     // prevent typos
     $this->apiKey = trim($apiKey);
     $this->client = new Client($this, $config);
 }
 /**
  * Fill a lib's node object.
  *
  * @param  GraphCommons\Graph\Entity\GraphNode $node
  * @param  array|object $g
  * @return GraphCommons\Graph\Entity\GraphNode
  */
 public final function fillNode(GraphNode $node, $n)
 {
     // force input being an object
     $n = Util::toObject($n);
     if (isset($n->id)) {
         $node->setId($n->id)->setName($n->name)->setDescription($n->description);
         // add suspected (nullable) properties
         if (isset($n->image)) {
             $node->setImage($n->image);
         }
         if (isset($n->created_at)) {
             $node->setCreatedAt($n->created_at);
         }
         if (isset($n->updated_at)) {
             $node->setUpdatedAt($n->updated_at);
         }
         if (isset($n->properties)) {
             $node->setProperties($n->properties);
         }
         if (isset($n->hubs, $n->users, $n->graphs, $n->graphs_count)) {
             $node->setHubs((array) $n->hubs)->setUsers((array) $n->users)->setGraphs((array) $n->graphs)->setGraphsCount($n->graphs_count);
         }
         // add node type as GraphCommons\Graph\Entity\NodeType
         $nodeType = new GraphNodeType();
         if (isset($n->nodetype)) {
             $nodeType->setId($n->nodetype->id)->setName($n->nodetype->name)->setColor($n->nodetype->color);
             $node->setTypeId($n->nodetype->id);
         } elseif (isset($n->type, $n->type_id)) {
             $nodeType->setId($n->type_id)->setName($n->type);
             $node->setTypeId($n->type_id);
         }
         $node->setType($nodeType);
     }
     return $node;
 }