/** * Create a service with a mocked client * * @param int $code * @param array $headers * @param array $bodyData * @return Client */ protected function createMockClient($code, array $headers = array(), array $bodyData = array()) { // Create a mock and queue a response. $mock = new MockHandler([new Response($code, $headers, \GuzzleHttp\json_encode($bodyData))]); $handler = HandlerStack::create($mock); $client = new Client(['handler' => $handler]); return $client; }
/** * @param $method * @param null $uri * @param array $options * @return \Psr\Http\Message\ResponseInterface */ protected function request($method, $uri = null, array $options = []) { if (null !== $this->token) { $options = array_merge($options, ['headers' => ['Authorization' => sprintf('Bearer %s', $this->token)]]); } $this->log(sprintf('Uri: %s, Method: %s, Options: %s', $uri, $method, \GuzzleHttp\json_encode($options))); $response = $this->client->request($method, $uri, $options); return $response; }
/** * @param UriInterface $httpUri * @param string $method * @param array $data [OPTIONAL] * * @return array */ protected function proceed(UriInterface $httpUri, $method, array $data = []) { if (!empty($data)) { $response = $this->httpClient->request($method, $httpUri, ['headers' => ['Content-Type' => 'application/x-www-form-urlencoded'], 'body' => 'JSONString=' . \GuzzleHttp\json_encode($data)]); } else { $response = $this->httpClient->request($method, $httpUri); } return \GuzzleHttp\json_decode($response->getBody()->getContents()); }
protected function authenticate($username, $password, $integrator_key, $env, $version) { $auth_header = ['Username' => $username, 'Password' => $password, 'IntegratorKey' => $integrator_key]; $this->auth_header = \GuzzleHttp\json_encode($auth_header); $client = new Client(); $response = $client->request('GET', 'https://' . $env . '.docusign.net/restapi/' . $version . '/login_information', ['headers' => ['X-DocuSign-Authentication' => $this->auth_header]]); $content = \GuzzleHttp\json_decode((string) $response->getBody()); $this->accounts = $content->loginAccounts; }
/** * * @param int $code * @param array $headers * @param array $bodyData * @return AuthenticationService */ protected function createMockService($code, array $headers = [], array $bodyData = [], $token = null) { // Create a mock and queue a response. $mock = new MockHandler([new Response($code, $headers, \GuzzleHttp\json_encode($bodyData)), new Response(200, [], \GuzzleHttp\json_encode(['token' => $token]))]); $handler = HandlerStack::create($mock); $client = new Client(['base_uri' => 'http://example.com', 'handler' => $handler]); $service = new AuthenticationService(); $service->setClient($client); return $service; }
/** * Saves the cookies to a file. * * @param string $filename File to save * @throws \RuntimeException if the file cannot be found or created */ public function save($filename) { $json = []; foreach ($this as $cookie) { /** @var SetCookie $cookie */ if (CookieJar::shouldPersist($cookie, $this->storeSessionCookies)) { $json[] = $cookie->toArray(); } } $jsonStr = \GuzzleHttp\json_encode($json); if (false === file_put_contents($filename, $jsonStr)) { throw new \RuntimeException("Unable to save file {$filename}"); } }
public function set($chatId, array $params) { file_put_contents($this->getFileName($chatId), \GuzzleHttp\json_encode($params)); }
/** * Applies the array of request options to a request. * * @param RequestInterface $request * @param array $options * * @return RequestInterface */ private function applyOptions(RequestInterface $request, array &$options) { $modify = []; if (isset($options['form_params'])) { if (isset($options['multipart'])) { throw new \InvalidArgumentException('You cannot use ' . 'form_params and multipart at the same time. Use the ' . 'form_params option if you want to send application/' . 'x-www-form-urlencoded requests, and the multipart ' . 'option to send multipart/form-data requests.'); } $options['body'] = http_build_query($options['form_params'], '', '&'); unset($options['form_params']); $options['_conditional']['Content-Type'] = 'application/x-www-form-urlencoded'; } if (isset($options['multipart'])) { $options['body'] = new Psr7\MultipartStream($options['multipart']); unset($options['multipart']); } if (isset($options['json'])) { $options['body'] = \GuzzleHttp\json_encode($options['json']); unset($options['json']); $options['_conditional']['Content-Type'] = 'application/json'; } if (!empty($options['decode_content']) && $options['decode_content'] !== true) { $modify['set_headers']['Accept-Encoding'] = $options['decode_content']; } if (isset($options['headers'])) { if (isset($modify['set_headers'])) { $modify['set_headers'] = $options['headers'] + $modify['set_headers']; } else { $modify['set_headers'] = $options['headers']; } unset($options['headers']); } if (isset($options['body'])) { if (is_array($options['body'])) { $this->invalidBody(); } $modify['body'] = Psr7\stream_for($options['body']); unset($options['body']); } if (!empty($options['auth']) && is_array($options['auth'])) { $value = $options['auth']; $type = isset($value[2]) ? strtolower($value[2]) : 'basic'; switch ($type) { case 'basic': $modify['set_headers']['Authorization'] = 'Basic ' . base64_encode("{$value['0']}:{$value['1']}"); break; case 'digest': // @todo: Do not rely on curl $options['curl'][CURLOPT_HTTPAUTH] = CURLAUTH_DIGEST; $options['curl'][CURLOPT_USERPWD] = "{$value['0']}:{$value['1']}"; break; } } if (isset($options['query'])) { $value = $options['query']; if (is_array($value)) { $value = http_build_query($value, null, '&', PHP_QUERY_RFC3986); } if (!is_string($value)) { throw new \InvalidArgumentException('query must be a string or array'); } $modify['query'] = $value; unset($options['query']); } // Ensure that sink is not an invalid value. if (isset($options['sink'])) { // TODO: Add more sink validation? if (is_bool($options['sink'])) { throw new \InvalidArgumentException('sink must not be a boolean'); } } $request = Psr7\modify_request($request, $modify); if ($request->getBody() instanceof Psr7\MultipartStream) { // Use a multipart/form-data POST if a Content-Type is not set. $options['_conditional']['Content-Type'] = 'multipart/form-data; boundary=' . $request->getBody()->getBoundary(); } // Merge in conditional headers if they are not present. if (isset($options['_conditional'])) { // Build up the changes so it's in a single clone of the message. $modify = []; foreach ($options['_conditional'] as $k => $v) { if (!$request->hasHeader($k)) { $modify['set_headers'][$k] = $v; } } $request = Psr7\modify_request($request, $modify); // Don't pass this internal value along to middleware/handlers. unset($options['_conditional']); } return $request; }
public function getAllFailedJobs() { $returnArray = array(); $client = APIClient::factory(array('host' => env('RABBITMQ_QUEUE_QUERY_HOST'))); $queues = $client->listQueues(); foreach ($queues as $k => $v) { if (preg_match('/_failed_/', $v->name)) { $url = 'http://' . env('RABBITMQ_QUEUE_QUERY_HOST') . ':' . env('RABBITMQ_QUEUE_QUERY_HOST_PORT') . '/api/queues/%2f/' . $v->name . '/get'; $client = new Client(); $body = ['count' => $v->messages, 'requeue' => true, 'encoding' => 'auto']; $request = $client->post($url, ['content-type' => 'application/json', 'auth' => ['guest', 'guest'], 'body' => \GuzzleHttp\json_encode($body)]); $messagesArray = \GuzzleHttp\json_decode($request->getBody()->getContents()); for ($i = 0; $i < sizeof($messagesArray); $i++) { $a = new \stdClass(); $a->id = $messagesArray[$i]->message_count; $a->connection = env('QUEUE_DRIVER'); $a->queue = $v->name; $a->payload = $messagesArray[$i]->payload; //$a->failed_at = date('Y-m-d H:i:s'); $returnArray[] = $a; } } } return $returnArray; }
/** * @dataProvider jsonSessionProvider */ public function testGetSession($jsonData) { $json = \GuzzleHttp\json_encode($jsonData); $response = new Response(201, ['Content-Type' => 'application/json'], \GuzzleHttp\Psr7\stream_for($json)); $entity = $this->handler->getSession($response); foreach ($jsonData as $key => $value) { $method = AbstractEntity::camel('get_' . $key); $this->assertTrue(method_exists($entity, $method), "Entity does not have method {$method}"); if ($method == 'getExpiresAt') { $this->assertSame(strtotime($value), $entity->{$method}()->getTimestamp()); } else { $this->assertSame($value, $entity->{$method}()); } } }