public function testPostBodyMD5()
 {
     $postBody = new PostBody();
     $postBody->replaceFields(['signature' => "HPMOHRgPSMKdXrU6AqQs/i9S7alOakkHsJiqLGmInt05Cxj6b/WhS7kJxbIQxKmDW08YKzoFnbVZIoTI2qofEzk="]);
     $this->assertEquals("signature=HPMOHRgPSMKdXrU6AqQs%2Fi9S7alOakkHsJiqLGmInt05Cxj6b%2FWhS7kJxbIQxKmDW08YKzoFnbVZIoTI2qofEzk%3D", (string) $postBody);
     $this->assertEquals("fdfc1a717d2c97649f3b8b2142507129", md5((string) $postBody));
 }
Example #2
0
 public function testHasFields()
 {
     $b = new PostBody();
     $b->setField('foo', 'bar');
     $b->setField('baz', '123');
     $this->assertEquals('bar', $b->getField('foo'));
     $this->assertEquals('123', $b->getField('baz'));
     $this->assertNull($b->getField('ahh'));
     $this->assertTrue($b->hasField('foo'));
     $this->assertFalse($b->hasField('test'));
     $b->replaceFields(['abc' => '123']);
     $this->assertFalse($b->hasField('foo'));
     $this->assertTrue($b->hasField('abc'));
 }
 /**
  * @param \ConsultBundle\Entity\UserInfo $userInfo
  *
  * @return \ConsultBundle\Entity\User|\ConsultBundle\Entity\UserInfo|null
  */
 public function retrieveUserProfileNew(UserInfo $userInfo)
 {
     if (empty($userInfo) || empty($userInfo->getPractoAccountId())) {
         return null;
     }
     $accountId = $userInfo->getPractoAccountId();
     $postData = array('service' => 'software', 'practo_account_id' => $accountId, 'signed' => 'service,practo_account_id,signed');
     $postData = $this->signEndpointPostData($postData, $this->accountsSigningKey);
     $body = new PostBody();
     $body->replaceFields($postData);
     $request = new Request('POST', $this->accountHost . "/_enquire_profile", [], $body);
     $client = new Client();
     $res = $client->send($request);
     $userInfo = $this->populateUserFromAccounts($res->json(), $userInfo);
     return $userInfo;
 }
Example #4
0
 /**
  * @param string $profileToken
  * @param array  $data
  *
  * @return null
  */
 public function updateAccountDetails($profileToken, $data)
 {
     if (empty($profileToken)) {
         return null;
     }
     $postData = $this->populatePostData($data);
     if (empty($postData)) {
         return null;
     }
     $body = new PostBody();
     $body->replaceFields($postData);
     $request = new Request('POST', $this->accountHost . "/update_profile_with_token", array('X-Profile-Token' => $profileToken), $body);
     $client = new Client();
     try {
         $userJson = $client->send($request);
         $_SESSION['authenticated_user'] = $userJson;
     } catch (\Exception $e) {
         //do nothing.
     }
 }
Example #5
0
 /**
  * @param string $method
  * @param array  $payload
  *
  * @return RequestInterface
  */
 private function createRequest($method, array $payload)
 {
     $request = $this->client->createRequest('POST');
     $request->setUrl(self::API_BASE_URL . $method);
     $body = new PostBody();
     $body->replaceFields($payload);
     $request->setBody($body);
     return $request;
 }
Example #6
0
 /**
  * @param string      $method
  * @param array       $data
  * @param string|null $token
  *
  * @throws SlackException
  *
  * @return array
  */
 private function doSend($method, array $data, $token = null)
 {
     try {
         $data['token'] = $token ?: $this->token;
         $this->eventDispatcher->dispatch(self::EVENT_REQUEST, new RequestEvent($data));
         $request = $this->client->createRequest('POST', self::API_BASE_URL . $method);
         $body = new PostBody();
         $body->replaceFields($data);
         $request->setBody($body);
         $response = $this->client->send($request);
     } catch (\Exception $e) {
         throw new SlackException('Failed to send data to the Slack API', null, $e);
     }
     try {
         $responseData = json_decode($response->getBody(), true);
         if (!is_array($responseData)) {
             throw new \Exception(sprintf('Expected JSON-decoded response data to be of type "array", got "%s"', gettype($responseData)));
         }
         $this->eventDispatcher->dispatch(self::EVENT_RESPONSE, new ResponseEvent($responseData));
         return $responseData;
     } catch (\Exception $e) {
         throw new SlackException('Failed to process response from the Slack API', null, $e);
     }
 }
 /**
  * Sends HTTP request to specific path with body wrapped by field.
  *
  * @param string $method
  * @param string $path
  * @param string $field
  * @param TableNode $table
  *
  * @When /^(?:I )?send a (PUT|PATCH|POST) request to path "([^"]+)" with field (\w+)$/
  */
 public function iSendARequestWithField($method, $path, $field, TableNode $table = null)
 {
     $url = $this->getUrlFromPath($path, $method);
     $request = $this->client->createRequest($method, $url, ['headers' => $this->getHeadersBag()->all()]);
     if (null !== $table) {
         $body = new PostBody();
         $body->replaceFields([$field => $table->getRowsHash()]);
         $request->setBody($body);
     }
     $this->send($request);
 }
Example #8
0
 /**
  * @param string $uri
  * @param array $query
  * @param array $body
  * @param string $method
  * @param array $options
  * @return mixed
  * @throws CPanelNotFoundException
  * @throws Exception
  * @throws WHMEmptyResponseException
  */
 public function send($uri, array $query = array(), array $body = array(), $method = 'GET', $options = array())
 {
     $url = $this->getRequestUrl($uri);
     $options = $this->getDefaultRequestOptions($options);
     if ($query) {
         $options["query"] = $query;
     }
     if ($body) {
         $postBody = new PostBody();
         $postBody->forceMultipartUpload(true);
         $postBody->replaceFields($body);
     }
     try {
         /** @var RequestInterface $request */
         $request = $this->client->createRequest($method, $url, $options);
         $request->setHeaders($this->auth->toArray());
         if (!empty($postBody)) {
             $request->setBody($postBody);
         }
         /** @var ResponseInterface $response */
         $response = $this->client->send($request);
         return $response->json(["object" => true]);
     } catch (RequestException $e) {
         $response = $e->getResponse();
         if ($response) {
             switch ($response->getStatusCode()) {
                 case 0:
                     throw new CPanelNotFoundException("cPanel Not Found");
                 case 403:
                     throw new Exception("Invalid Creds");
                 default:
                     throw new Exception("cPanel Status: " . $response->getStatusCode());
             }
         } else {
             throw new WHMEmptyResponseException($e->getMessage());
         }
     } catch (Exception $e) {
         // Try again in case of SSH connection issues
         // @TODO add retries limitation
         if (substr_count($e->getMessage(), "SSH")) {
             return $this->send($uri, $query, $body, $method);
         }
         throw new Exception($e->getMessage(), $e->getCode(), $e);
     }
 }
 public function withBodyParams(array $params)
 {
     $body = new PostBody();
     $body->replaceFields($params);
     return $this->withBody($body);
 }