Пример #1
0
 public function testCRUD()
 {
     // GET ALL (none)
     $response = GuzzleHttp\get("http://miniapi.local/test/endpoint.php");
     $this->assertTrue(is_array($response->json()));
     $this->assertEquals(0, count($response->json()));
     // CREATE ONE
     GuzzleHttp\post("http://miniapi.local/test/endpoint.php", ["json" => ["name" => "fabs"]]);
     // GET ALL (now 1)
     $response = GuzzleHttp\get("http://miniapi.local/test/endpoint.php");
     $json = $response->json();
     $this->assertEquals(1, count($json));
     $this->assertEquals(1, $json[0]["id"]);
     $this->assertEquals('fabs', $json[0]["name"]);
     // EDIT THE NEWLY CREATED
     GuzzleHttp\put("http://miniapi.local/test/endpoint.php", ["json" => ["id" => 1, "name" => "laurent"]]);
     // GET ALL (still 1)
     $response = GuzzleHttp\get("http://miniapi.local/test/endpoint.php");
     $json = $response->json();
     $this->assertEquals(1, count($json));
     $this->assertEquals(1, $json[0]["id"]);
     $this->assertEquals('laurent', $json[0]["name"]);
     // DELETE IT
     $response = GuzzleHttp\delete("http://miniapi.local/test/endpoint.php/1");
     // GET ALL (now 0)
     $response = GuzzleHttp\get("http://miniapi.local/test/endpoint.php");
     $json = $response->json();
     $this->assertEquals(0, count($json));
 }
Пример #2
0
 /**
  * Make the request to the API
  * @return mixed
  */
 public function makeRequest()
 {
     $url = $this->url . $this->endpoint;
     $client = new Client();
     $rdata = array('headers' => array('X-Simply-Auth' => $this->apiKey));
     if (!empty($this->queryParams)) {
         $rdata['query'] = $this->queryParams;
     }
     if (!empty($this->includes)) {
         $rdata['query']['include'] = implode(',', $this->includes);
     }
     if (!empty($this->body)) {
         $rdata['body'] = $this->body;
     }
     switch ($this->method) {
         case self::METHOD_PATCH:
             $response = \GuzzleHttp\get($url, $rdata);
             break;
         case self::METHOD_POST:
             $response = \GuzzleHttp\post($url, $rdata);
             break;
         default:
             $response = \GuzzleHttp\get($url, $rdata);
     }
     return $response->json();
 }
Пример #3
0
 protected function query($endpoint, $body)
 {
     return \GuzzleHttp\post($endpoint, compact('body'));
 }
Пример #4
0
 /**
  * The executor. It will run API function and get the data
  *
  * @param string $action function name that will be called.
  * @param string $arguments list of parameters that will be attached.
  * @return array results of API call
  * @since v1.0.0
  */
 protected function runQuery($action, $arguments)
 {
     $host = $this->getHost();
     $response = \GuzzleHttp\post($host . '/json-api/' . $action, ['headers' => $this->createHeader(), 'verify' => false, 'query' => $arguments]);
     return $response->json();
 }