Beispiel #1
2
 private function getResponse($stack, $base_uri, $uri, $data, $httpMethod)
 {
     $middleware = new Oauth1(['consumer_key' => self::CONSUMER_KEY, 'consumer_secret' => self::CONSUMER_SECRET, 'token_secret' => self::TOKEN_SECRET]);
     //'auth' => ['user_name', 'pass_name'], // when using username & password
     $options = ['auth' => 'oauth', 'debug' => false, 'headers' => ['Accept' => 'application/hal+json', 'Content-Type' => 'application/hal+json'], 'body' => json_encode($data)];
     $stack->push($middleware);
     $client = new Client(['base_uri' => $base_uri, 'handler' => $stack]);
     $response = false;
     switch ($httpMethod) {
         case self::HTTP_GET:
             $response = $client->get($uri, $options);
             break;
         case self::HTTP_POST:
             $response = $client->post($uri, $options);
             break;
         case self::HTTP_PUT:
             $response = $client->put($uri, $options);
             break;
         case self::HTTP_PATCH:
             $response = $client->patch($uri, $options);
             break;
         case self::HTTP_DELETE:
             $response = $client->delete($uri, $options);
             break;
     }
     return $response;
 }
Beispiel #2
0
 /**
  * @param string $url
  * @param array $options
  * @throws GuzzleException
  * @return Response
  */
 public function patch($url, array $options = [])
 {
     if (strpos($url, 'http') !== 0) {
         $url = $this->baseUrl . '/' . $this->collection . '/' . ltrim($url, '/');
     }
     return new Response($this, $this->client->patch($url, $options));
 }
 public function patch($resource, $data, $type, $options = [])
 {
     $options['json'] = $data;
     $options['headers'] = ['Content-Type' => 'application/json'];
     $content = $this->client->patch($resource, $options)->getBody()->getContents();
     return $this->deserialize($content, $type);
 }
 public function patch($resource, $data, $type, $options = [])
 {
     $options['future'] = true;
     $options['json'] = $data;
     $options['headers'] = ['Content-Type' => 'application/json'];
     return $this->client->patch($resource, $options)->then(function (Response $reponse) {
         return $reponse->getBody()->getContents();
     })->then(function ($content) use($type) {
         return $this->deserialize($content, $type);
     });
 }
 public function testNoCacheOtherMethod()
 {
     $this->client->post("anything");
     $response = $this->client->post("anything");
     $this->assertEquals("", $response->getHeaderLine("X-Cache"));
     $this->client->put("anything");
     $response = $this->client->put("anything");
     $this->assertEquals("", $response->getHeaderLine("X-Cache"));
     $this->client->delete("anything");
     $response = $this->client->delete("anything");
     $this->assertEquals("", $response->getHeaderLine("X-Cache"));
     $this->client->patch("anything");
     $response = $this->client->patch("anything");
     $this->assertEquals("", $response->getHeaderLine("X-Cache"));
 }
 public function testNoCacheOtherMethod()
 {
     $this->client->post('anything');
     $response = $this->client->post('anything');
     $this->assertEquals(CacheMiddleware::HEADER_CACHE_MISS, $response->getHeaderLine(CacheMiddleware::HEADER_CACHE_INFO));
     $this->client->put('anything');
     $response = $this->client->put('anything');
     $this->assertEquals(CacheMiddleware::HEADER_CACHE_MISS, $response->getHeaderLine(CacheMiddleware::HEADER_CACHE_INFO));
     $this->client->delete('anything');
     $response = $this->client->delete('anything');
     $this->assertEquals(CacheMiddleware::HEADER_CACHE_MISS, $response->getHeaderLine(CacheMiddleware::HEADER_CACHE_INFO));
     $this->client->patch('anything');
     $response = $this->client->patch('anything');
     $this->assertEquals(CacheMiddleware::HEADER_CACHE_MISS, $response->getHeaderLine(CacheMiddleware::HEADER_CACHE_INFO));
 }
Beispiel #7
0
 /**
  * Send Rename request to slave
  *
  */
 public function handle()
 {
     $client = new Client();
     try {
         $response = $client->patch($this->slave . ApiUrl::$rename . '/' . $this->from . '/' . $this->to . '?token=' . getMasterPassword());
     } catch (\Exception $e) {
     }
 }
 /**
  * @param string $uri
  * @param array $data
  * @param array $options
  * @param bool $api
  * @return $this;
  */
 public function patch($uri, array $data = [], array $options = [], $api = true)
 {
     $options = $this->configureOptions($options);
     $uri = $api ? $this->getServiceConfig('api_url') . $uri : $uri;
     $response = $this->client->patch($uri, array_merge($options, ['form_params' => $data]));
     $this->setGuzzleResponse($response);
     return $this;
 }
 private function disableEmailObfuscation()
 {
     try {
         $response = $this->client->patch('zones/' . $this->zoneId . '/settings/email_obfuscation', ['json' => ['value' => 'off']]);
         return $response->json(['object' => true])->result;
     } catch (Exception $e) {
         throw $this->createCloudflareException($e);
     }
 }
Beispiel #10
0
 /**
  * @param $url
  * @param $version
  * @param array  $data
  * @param string $bodyEncoding
  *
  * @return \GuzzleHttp\Message\FutureResponse|\GuzzleHttp\Message\ResponseInterface|\GuzzleHttp\Ring\Future\FutureInterface|null
  */
 private function patch($url, $version, array $data = [], $bodyEncoding = 'json')
 {
     $requestOptions = $this->getPostRequestOptions($version, $data, $bodyEncoding);
     $response = $this->client->patch($url, $requestOptions);
     if (200 !== $response->getStatusCode()) {
         throw new \LogicException('An error occurred when trying to POST data to MR API');
     }
     return $response;
 }
 /**
  * @param string $path
  * @param array  $options
  *
  * @throws ClientException
  *
  * @return string
  */
 public function patch($path, array $options = [])
 {
     $options = array_merge($this->options, $options);
     try {
         /** @var Response $result */
         $result = $this->guzzle->patch($path, $options);
     } catch (RequestException $requestException) {
         throw new ClientException('Could not update.', 0, $requestException);
     }
     return $result->getBody()->getContents();
 }
 public function patch($uri, array $body)
 {
     $this->lastRequestBody = $body;
     try {
         $this->lastResponse = $this->client->patch($uri, ['json' => $body, 'headers' => $this->headers]);
     } catch (RequestException $e) {
         $this->lastResponse = $e->getResponse();
     } finally {
         $this->lastResponseBody = $this->lastResponse->getBody()->getContents();
     }
 }
 /**
  * Completes a task
  *
  * @param int $taskId
  * @param int $revision
  * 
  * @return array
  */
 public function completeTask($taskId, $revision)
 {
     if (!is_numeric($taskId)) {
         throw new \InvalidArgumentException('The list id must be numeric');
     } elseif (!is_numeric($revision)) {
         throw new \InvalidArgumentException('The revision must be numeric');
     }
     $response = $this->guzzle->patch('tasks/' . $taskId, ['body' => json_encode(['revision' => (int) $revision, 'completed' => true])]);
     $this->checkResponseStatusCode($response, 200);
     return json_decode($response->getBody(), true);
 }
Beispiel #14
0
 /**
  * Modify the name of a remote `Node`.
  *
  * @param string $name
  *
  * @return array
  */
 public function rename($name)
 {
     $retval = ['success' => false, 'data' => []];
     $response = self::$httpClient->patch(self::$account->getMetadataUrl() . "nodes/{$this['id']}", ['headers' => ['Authorization' => 'Bearer ' . self::$account->getToken()['access_token']], 'json' => ['name' => $name], 'exceptions' => false]);
     $retval['data'] = json_decode((string) $response->getBody(), true);
     if ($response->getStatusCode() === 200) {
         $retval['success'] = true;
         $this->replace($retval['data']);
         $this->save();
     }
     return $retval;
 }
Beispiel #15
0
 private function method($object, $url, $name = 'post', $args = null)
 {
     try {
         if (is_array($args)) {
             $url = array($url, $args);
         }
         switch ($name) {
             case 'post':
                 $response = $this->client->post($url, array('body' => $this->object[$object]));
                 break;
             case 'patch':
                 $response = $this->client->patch($url, array('body' => $this->object[$object]));
                 break;
             default:
                 throw new \Exception("Method isn't defined");
         }
         //Fetch recent created record
         $this->object_created[$object] = $this->client->get($response->getHeader('location'))->json();
     } catch (ClientException $e) {
         var_dump($e->getResponse()->json());
         \PHPUnit_Framework_TestCase::fail('CLIENT: ' . $e->getMessage());
     }
 }
 /**
  * Sends a request to the API.
  *
  * @param string $request_type
  *   The type of HTTP request being made.
  *   Expected to be one of: REQUEST_TYPE_GET, REQUEST_TYPE_POST
  *   REQUEST_TYPE_PATCH, REQUEST_TYPE_PUT or REQUEST_TYPE_DELETE.
  * @param string $api_end_point
  *   The relative url for the endpoint.  All endpoints are assumed to be
  *   relative to 'https://api.cloudflare.com/client/v4/'.
  * @param array $request_params
  *   (Optional) Associative array of parameters to be passed with the HTTP
  *   request.
  *
  * @return \CloudFlarePhpSdk\ApiTypes\CloudFlareApiResponse
  *   The response from the Api
  *
  * @throws \CloudFlarePhpSdk\Exceptions\CloudFlareApiException
  *    Exception at the application level.
  * @throws \CloudFlarePhpSdk\Exceptions\CloudFlareHttpException
  *     Exception at the Http level.
  */
 protected function makeRequest($request_type, $api_end_point, $request_params = [])
 {
     // Default the number of pages returned by the API to MAX.
     if (!isset($request_params['per_page'])) {
         $request_params['per_page'] = self::MAX_ITEMS_PER_PAGE;
     }
     // This check seems superfluous.  However, the Api only returns a http 400
     // code. This proactive check gives us more information.
     $is_api_key_valid = strlen($this->apikey) == CloudFlareAPI::API_KEY_LENGTH;
     $is_api_key_alpha_numeric = ctype_alnum($this->apikey);
     $is_api_key_lower_case = !preg_match('/[A-Z]/', $this->apikey);
     if (!$is_api_key_valid) {
         throw new CloudFlareInvalidCredentialException("Invalid Api Key: Key should be 37 chars long.", 403);
     }
     if (!$is_api_key_alpha_numeric) {
         throw new CloudFlareInvalidCredentialException('Invalid Api Key: Key can only contain alphanumeric characters.', 403);
     }
     if (!$is_api_key_lower_case) {
         throw new CloudFlareInvalidCredentialException('Invalid Api Key: Key can only contain lowercase or numerical characters.', 403);
     }
     try {
         switch ($request_type) {
             case self::REQUEST_TYPE_GET:
                 $this->lastHttpResponse = $this->client->get($api_end_point, ['query' => $request_params]);
                 break;
             case self::REQUEST_TYPE_POST:
                 $this->lastHttpResponse = $this->client->post($api_end_point, ['data' => $request_params]);
                 break;
             case self::REQUEST_TYPE_PATCH:
                 $this->lastHttpResponse = $this->client->patch($api_end_point, ['json' => $request_params]);
                 break;
             case self::REQUEST_TYPE_PUT:
                 $this->lastHttpResponse = $this->client->put($api_end_point, ['json' => $request_params]);
                 break;
             case self::REQUEST_TYPE_DELETE:
                 $this->lastHttpResponse = $this->client->delete($api_end_point, ['json' => $request_params]);
                 break;
         }
     } catch (ServerException $se) {
         $http_response_code = $se->getCode();
         $http_response_message = $se->getMessage();
         throw new CloudFlareHttpException($http_response_message, $http_response_code, $se->getPrevious());
     } catch (RequestException $re) {
         $http_response_code = $re->getCode();
         $http_response_message = $re->getMessage();
         if ($http_response_code == 403) {
             throw new CloudFlareInvalidCredentialException("Unfortunately your credentials failed to authenticate against the CloudFlare API.  Please enter valid credentials.", 403);
         } else {
             throw new CloudFlareTimeoutException($http_response_message, $http_response_code, $re->getPrevious());
         }
     }
     $http_response_code = $this->lastHttpResponse->getStatusCode();
     $is_status_code_good = $http_response_code == '200' || $http_response_code == '301';
     // HTTP level error.
     if (!$is_status_code_good) {
         $http_response_message = $this->lastHttpResponse->getReasonPhrase();
         throw new CloudFlareHttpException($http_response_message, $http_response_code, NULL);
     }
     // Note this behavior was introduced in Guzzle 6.
     $response_body = (string) $this->lastHttpResponse->getBody();
     $this->lastApiResponse = new CloudFlareApiResponse($response_body);
     $json_decode_failure = is_null($this->lastApiResponse);
     if ($json_decode_failure) {
         throw new CloudFlareApiException($http_response_code, NULL, "Unable to decode response payload.", NULL);
     }
     $is_request_successful = $this->lastApiResponse->isSuccess();
     // See https://api.cloudflare.com/#responses
     $has_errors_from_api = count($this->lastApiResponse->getErrors()) > 0;
     // Application level error.
     if (!$is_request_successful || $has_errors_from_api) {
         $http_response_message = $this->lastHttpResponse->getReasonPhrase();
         throw new CloudFlareApiException($http_response_code, NULL, $http_response_message, NULL);
     }
     return $this->lastApiResponse;
 }
 public function tearDown()
 {
     $this->mailtrap->patch('inboxes/' . getenv('MAILTRAP_INBOX') . '/clean', ['future' => true]);
 }
 /**
  * @param string $url
  * @param array $options
  * @return \GuzzleHttp\Message\FutureResponse|\GuzzleHttp\Message\ResponseInterface|\GuzzleHttp\Ring\Future\FutureInterface|HttpResponse|\iDimensionz\SendGridWebApiV3\HttpResponseInterface|null
  */
 public function patch($url = null, array $options = [])
 {
     $guzzleResponse = parent::patch($url, $options);
     $response = $this->getHttpResponse($guzzleResponse);
     return $response;
 }
Beispiel #19
0
 /**
  * @param int    $groupId
  * @param string $reference
  */
 public function updateGroupReference($groupId, $reference)
 {
     $data = $this->encode(['group' => ['reference' => $reference]]);
     $this->guzzle->patch('groups/' . $groupId, ['body' => $data]);
 }
Beispiel #20
0
 public function setMachineDetail($options = array())
 {
     $client = new \GuzzleHttp\Client();
     try {
         $response = $client->get('https://api.ciscospark.com/v1/people/me', ['headers' => $this->getBaseHeaders(), 'verify' => false]);
     } catch (ClientException $e) {
         $errorResponse = $e->getResponse();
         $statusCode = $errorResponse->getStatusCode();
         if ($statusCode == 401) {
             $response = $client->get('https://api.ciscospark.com/v1/people/me', ['headers' => $this->getRefreshHeaders(), 'verify' => false]);
         } else {
             if ($statusCode != 200) {
                 return ApiException::errorMessage($statusCode);
             }
         }
     }
     $jsonArray = json_decode($response->getBody());
     $midstring = base64_decode($jsonArray->id);
     $mid = str_replace("ciscospark://us/PEOPLE/", "", $midstring);
     $body = json_encode($options);
     $it = $this->getIdentityToken();
     $pclient = new \GuzzleHttp\Client(array('verify' => false));
     $pr = $pclient->patch('https://identity.webex.com/organization/' . $this->configuration['machine_org'] . '/v1/Machines/' . $mid, ['headers' => ['Authorization' => $it, 'Content-Type' => 'application/json'], 'body' => $body, 'verify' => false]);
     return json_decode($pr->getBody());
 }
Beispiel #21
0
 /**
  * Updates the build with the following summary message.
  */
 public function tag($build, $tags)
 {
     if (empty($build)) {
         throw new Exception('Please provide a build.');
     }
     if (empty($tags)) {
         throw new Exception('Please provide at least one tag.');
     }
     $username = $this->getUsername();
     $password = $this->getPassword();
     $url = $this->getUrl();
     $client = new Client(['base_url' => $url]);
     $client->patch('node/' . $build, ['headers' => ['Content-type' => 'application/hal+json'], 'body' => json_encode(array('_links' => array('type' => array('href' => $url . 'rest/type/node/result')), 'field_tags' => $this->buildTags($tags))), 'auth' => [$username, $password]]);
 }
Beispiel #22
0
 /**
  * Update Chat Romm | WhiteList or Meta
  *
  * @param string $type
  * @param string $diuit_session
  * @param integer $chat_id
  * @param array $data
  * @return mixed
  */
 public function updateChatRoom($type, $diuit_session, $chat_id, $data)
 {
     switch ($type) {
         case 'meta':
             $action = 'updateMeta';
             $json = ['chatId' => $chat_id, 'meta' => $data];
             break;
         case 'whiteList':
             $action = 'updateWhiteList';
             $json = ['chatId' => $chat_id, 'whiteList' => $data];
             break;
     }
     $client = new Client();
     $response = $client->patch($this->url . 'chats/' . $action, ['headers' => ['x-diuit-app-key' => $this->DIUIT_APP_KEY, 'x-diuit-application-id' => $this->DIUIT_APP_ID, 'Authorization' => 'Bearer ' . $diuit_session], 'json' => $json])->json();
     return $response;
 }
Beispiel #23
0
 /**
  * @return void
  */
 public function deleteAllEmails()
 {
     $inboxId = $this->config['inbox'];
     $this->mailtrap->patch("inboxes/{$inboxId}/clean");
 }
 /**
  * Clean all the messages from inbox.
  *
  * @return void
  */
 public function cleanInbox()
 {
     $this->client->patch("inboxes/{$this->config['inbox_id']}/clean")->send();
 }