/** * @param array $config */ public function __construct($config = array()) { $this->config = array_merge(array('base_uri' => null, 'username' => null, 'password' => null, 'mime' => null, 'timeout' => null), (array) $config); $this->base_uri = $this->config['base_uri']; $template = Request::init()->mime($this->config['mime']); if ($this->config['username']) { $template->authenticateWith($this->config['username'], $this->config['password']); } Request::ini($template); }
/** * Construt a Request * * @param Auth $auth SkyHub Auth information to send on headers */ public function __construct(Auth $auth) { $this->auth = $auth; $this->jsonHandler = new \SkyHub\Handlers\JsonHandler(); $this->jsonHandler->init(array()); Httpful::register(\Httpful\Mime::JSON, $this->jsonHandler); // creates a request template, every request must have the auth headers $this->requestTemplate = HttpfulRequest::init()->expectsJson()->addHeader('X-User-Email', $this->auth->getEmail())->addHeader('X-User-Token', $this->auth->getToken()); HttpfulRequest::ini($this->requestTemplate); }
/** * Prepare request template with default values. All those can be simply * changed by calling appropriate methods on the request, which will override * the template set here. Here we set Basic Auth and Accept header to be * used in all requests. * */ protected function prepareRequestTemplate() { // Create the Request template. static::$requestTemplate = Request::init()->method(Http::GET)->basicAuth(static::$settings->isvuUsername, static::$settings->isvuPassword)->addHeader('Accept', static::$settings->defaultAcceptHeader); // Set it as a template. Request::ini(static::$requestTemplate); }
/** * Makes a call to the API. * * This method will make the actial API call by the given arguments. It * will return the response on success (200) or will throw an exception * on failure. * * @param string $method The HTTP method to use (e.g. Http::GET, Http::POST, etc.). * @param string $resourcePath The path to the resource (e.g. contacts/john@example.com/) * @param string $payload The data that is sent to the service. Not used for GET or DELETE. * @return \Httpful\Response The response object from the service. * @throws \Gan\ApiException */ public function call($method, $resourcePath, $payload = null) { $uri = $this->baseUri . $resourcePath; Request::ini($this->requestTemplate); $request = Request::init($method)->uri($uri); if ($payload) { $request->body($payload); } try { $response = $request->send(); } finally { Request::resetIni(); } if (floor($response->code / 100) != 2) { throw new ApiException($response); } return $response; }
private function sendRequest($endpoint, $method = Http::GET, $payload = null) { $template = Request::init()->method($method)->sendsType(Mime::JSON); if (isset($this->auth_header)) { $template->addHeader('Authorization', 'Basic ' . $this->auth_header); } Request::ini($template); $r = null; switch ($method) { case Http::GET: $r = Request::get(self::API_HOST . $endpoint . '?' . http_build_query($payload))->send(); break; case Http::POST: $r = Request::post(self::API_HOST . $endpoint)->body(json_encode($payload))->send(); break; default: throw new \Exception("Method not implemented."); break; } if ($r->hasErrors() || isset($r->body->status_code) && $r->body->status_code != 200) { throw new \Exception($r); } return $r; }
function testIni() { // Test setting defaults/templates // Create the template $template = Request::init()->method(Http::POST)->withStrictSsl()->expectsType(Mime::HTML)->sendsType(Mime::FORM); Request::ini($template); $r = Request::init(); $this->assertTrue($r->strict_ssl); $this->assertEquals(Http::POST, $r->method); $this->assertEquals(Mime::HTML, $r->expected_type); $this->assertEquals(Mime::FORM, $r->content_type); // Test the default accessor as well $this->assertTrue(Request::d('strict_ssl')); $this->assertEquals(Http::POST, Request::d('method')); $this->assertEquals(Mime::HTML, Request::d('expected_type')); $this->assertEquals(Mime::FORM, Request::d('content_type')); Request::resetIni(); }
public function search() { if (!Session::has('api_settings')) { return array('status' => 0); } $api_settings = Session::get('api_settings'); $first_time = $api_settings['current'] == 0; $url = sprintf("https://%s:%s@%s/api/v1/messages/search?q=%s", $api_settings['username'], $api_settings['password'], $api_settings['host'], $api_settings['query_string']); if (!$first_time) { $url .= sprintf("&from=%s&size=100", $api_settings['current']); } $http_template = RequestClient::init()->method('GET')->withoutStrictSsl()->withoutAutoParsing(); RequestClient::ini($http_template); $res = RequestClient::get($url)->send(); if (!$res->hasErrors() && $res->hasBody()) { $data = json_decode($res->body, true); if ($first_time) { $total = intval($data['search']['results']); $api_settings['total'] = $total; } $count = 0; foreach ($data['tweets'] as $tweet) { $flat = $this->array_flat($tweet); Storage::disk('local')->append($api_settings['file_name'], json_encode($flat, JSON_UNESCAPED_UNICODE) . ','); $count++; } $api_settings['current'] += $count; Session::put('api_settings', $api_settings); return array('status' => 1, 'total' => $api_settings['total'], 'current' => $api_settings['current']); } return array('status' => 0); }
/** * Initiates the HTTP request using the provided method (GET, POST, PUT, DELETE) * and returns the response XML as an Httpful\Request. * * @param string $method * * @throws MyPlexDataException * * @return HttpfulRequest */ public function send($method) { HttpfulRequest::ini($this->template); try { $response = HttpfulRequest::$method($this->endPoint)->send(); } catch (ConnectionErrorException $e) { throw new MyPlexDataException('Unable to connect to endPoint: ' . $e->getMessage(), 0, $e); } catch (\Exception $e) { throw new MyPlexDataException('Error in response from server: ' . $e->getMessage(), 0, $e); } $this->errorCheck($response); return $response; }
/** * Method to set template of request */ private function setTemplate($method) { // Register a new mime type handler if we expect a Json, so the response is decoded as an array. // Extracted from: http://stackoverflow.com/a/22597037 if ($this->mimeType == 'json') { $jsonHandler = new JsonHandler(array('decode_as_array' => true)); Httpful::register('application/json', $jsonHandler); } // Create template $template = Request::init()->method($this->getMethod($method))->expects($this->mimeType)->sendsType(Mime::FORM); // Add custom headers to request if ($this->headers !== null && is_array($this->headers) && !empty($this->headers)) { $template->addHeaders($this->headers); } // Set the template Request::ini($template); }
/** * Applies a Request template. * If you don't specify the $template parameter, the default template will be applied. * * @param \Httpful\Request $template */ public function set_template($template) { $this->template = $template instanceof Request ? $template : Request::init(); Request::ini($this->template); }
public function createTemplate() { $this->_template = Request::init()->withXProjectId($this->_project_id)->withXProjectKey($this->_project_key); Request::ini($this->_template); }
public function __construct($app_key = null, $secret = null, $base_uri = null) { $this->set_app_key($app_key); $this->set_secret($secret); if ($base_uri != null) { self::$base_uri = $base_uri; } $template = Request::init()->withoutStrictSsl()->expectsJson()->sendsType(Mime::JSON)->addHeader('yotpo_api_connector', 'PHP-' . Yotpo::VERSION); // Set it as a template Request::ini($template); }