/** * @inheritdoc */ public function request($action, array $getData = [], array $postData = []) { try { $curlOptions = [CURLOPT_RETURNTRANSFER => true]; if ($postData) { $json = json_encode($postData, JSON_PRETTY_PRINT); if ($json === false) { throw new RequesterException('Failed to serialize data into JSON. Data: ' . var_export($postData, true)); } $curlOptions = $curlOptions + [CURLOPT_POSTFIELDS => $json, CURLOPT_HTTPHEADER => ['Content-Type: application/json', 'Content-Length: ' . strlen($json)]]; } $getParams = $getData ? '?' . http_build_query($getData) : ''; $curl = curl_init($this->endpoint->getUrl() . $action . $getParams); curl_setopt_array($curl, $curlOptions); $result = curl_exec($curl); if ($result === false) { throw new RequesterException(sprintf('cURL error: [%d] %s', curl_errno($curl), curl_error($curl))); } $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); curl_close($curl); } catch (RequesterException $e) { throw $e; } catch (\Exception $e) { $result = empty($result) ? '' : ', result: ' . $result; throw new RequesterException('An error occurred during the transfer' . $result, null, $e); } if ($httpCode !== 200) { throw new RequesterException(sprintf("Request resulted in HTTP code '%d'. Response result:\n%s", $httpCode, $result)); } return new Response($result); }
/** * @inheritdoc */ public function request($action, array $getData = [], array $postData = []) { try { $options = ['http' => ['method' => 'GET', 'ignore_errors' => true]]; if ($postData) { $json = json_encode($postData, JSON_PRETTY_PRINT); if ($json === false) { throw new RequesterException('Failed to serialize data into JSON. Data: ' . var_export($postData, true)); } $options['http'] = $options['http'] + ['method' => 'POST', 'header' => sprintf("Content-Type: application/json\r\nContent-Length: %d\r\n", strlen($json)), 'content' => $json]; } $getParams = $getData ? '?' . http_build_query($getData) : ''; $context = stream_context_create($options); $fp = @fopen($this->endpoint->getUrl() . $action . $getParams, 'r', false, $context); // @ intentionally if ($fp === false) { $error = error_get_last(); throw new RequesterException(sprintf('fopen failed: [%d] %s', $error['type'], $error['message'])); } $result = stream_get_contents($fp); $metadata = stream_get_meta_data($fp); fclose($fp); $httpCode = 0; foreach ($metadata['wrapper_data'] as $header) { if (strpos($header, 'HTTP') === 0) { list($version, $httpCode, $phrase) = explode(' ', $header, 3); $httpCode = (int) $httpCode; break; } } } catch (RequesterException $e) { throw $e; } catch (\Exception $e) { $result = empty($result) ? '' : ', result: ' . $result; $message = 'An error occurred during the transfer' . $result . "\n\n" . "Please consider installing cURL and it's PHP extension - it is recommended."; throw new RequesterException($message, null, $e); } if ($httpCode !== 200) { throw new RequesterException(sprintf("Request resulted in HTTP code '%d'. Response result:\n%s", $httpCode, $result)); } return new Response($result); }
private function seedUsersEndpoints() { $users = Api::where('name', '=', 'users')->first(); // endpoints scopes ApiEndpoint::create(array('name' => 'get-user-info', 'active' => true, 'api_id' => $users->id, 'route' => '/api/v1/users/me', 'http_method' => 'GET')); $profile_scope = ApiScope::where('name', '=', 'profile')->first(); $email_scope = ApiScope::where('name', '=', 'email')->first(); $address_scope = ApiScope::where('name', '=', 'address')->first(); $get_user_info_endpoint = ApiEndpoint::where('name', '=', 'get-user-info')->first(); $get_user_info_endpoint->scopes()->attach($profile_scope->id); $get_user_info_endpoint->scopes()->attach($email_scope->id); $get_user_info_endpoint->scopes()->attach($address_scope->id); }
public function testRemoveRequiredScope() { $api_endpoint = ApiEndpoint::where('name', '=', 'update-api-endpoint-status')->first(); $this->assertTrue(!is_null($api_endpoint)); $scope = ApiScope::where('name', '=', sprintf('%s/api-endpoint/update', $this->current_realm))->first(); $this->assertTrue(!is_null($scope)); $response = $this->action("DELETE", "ApiEndpointController@removeRequiredScope", array('id' => $api_endpoint->id, 'scope_id' => $scope->id), array(), array(), array()); $this->assertResponseStatus(200); $content = $response->getContent(); $response = json_decode($content); $this->assertTrue($response === 'ok'); $response = $this->action("GET", "ApiEndpointController@get", $parameters = array('id' => $api_endpoint->id), array(), array(), array()); $content = $response->getContent(); $response_api_endpoint = json_decode($content); $this->assertTrue(is_array($response_api_endpoint->scopes) && count($response_api_endpoint->scopes) == 1); $this->assertResponseStatus(200); }
private function seedConsultantsEndpoints() { $consultants = Api::where('name', '=', 'consultants')->first(); $current_realm = Config::get('app.url'); // endpoints scopes ApiEndpoint::create(array('name' => 'get-consultants', 'active' => true, 'api_id' => $consultants->id, 'route' => '/api/v1/marketplace/consultants', 'http_method' => 'GET')); ApiEndpoint::create(array('name' => 'get-consultant', 'active' => true, 'api_id' => $consultants->id, 'route' => '/api/v1/marketplace/consultants/{id}', 'http_method' => 'GET')); ApiEndpoint::create(array('name' => 'get-consultant-offices', 'active' => true, 'api_id' => $consultants->id, 'route' => '/api/v1/marketplace/consultants/{id}/offices', 'http_method' => 'GET')); $consultant_read_scope = ApiScope::where('name', '=', sprintf('%s/consultants/read', $current_realm))->first(); $endpoint = ApiEndpoint::where('name', '=', 'get-consultants')->first(); $endpoint->scopes()->attach($consultant_read_scope->id); $endpoint = ApiEndpoint::where('name', '=', 'get-consultant')->first(); $endpoint->scopes()->attach($consultant_read_scope->id); $endpoint = ApiEndpoint::where('name', '=', 'get-consultant-offices')->first(); $endpoint->scopes()->attach($consultant_read_scope->id); }