-
-
Notifications
You must be signed in to change notification settings - Fork 2
PHP YouTrack REST
YouTrack REST API PHP Client uses PSR-7 (HTTP Message Interface) to connect with JetBrains YouTrack REST API.
Part of the PHP YouTrack SDK.
Starting with YouTrack 2017.1 release authorization based on permanent tokens is recommended as the main approach for the authorization in your REST API calls.
// Instantiate PSR-7 HTTP Client
$psrHttpClient = new \GuzzleHttp\Client([
'base_uri' => 'https://example.com',
]);
// Instantiate YouTrack API HTTP Client Adapter
$httpClient = new \Cog\YouTrack\Rest\HttpClient\GuzzleHttpClient($psrHttpClient);
// Instantiate YouTrack API Token Authorizer
$authorizer = new \Cog\YouTrack\Rest\Authorizer\TokenAuthorizer('YOUTRACK_API_TOKEN');
// Instantiate YouTrack API Client
$youtrack = new \Cog\YouTrack\Rest\Client\YouTrackClient($httpClient, $authorizer);
// Instantiate PSR-7 HTTP Client
$psrHttpClient = new \GuzzleHttp\Client([
'base_uri' => 'https://example.com',
]);
// Instantiate YouTrack API HTTP Client Adapter
$httpClient = new \Cog\YouTrack\Rest\HttpClient\GuzzleHttpClient($psrHttpClient);
// Instantiate YouTrack API Cookie Authenticator
$authenticator = new \Cog\YouTrack\Rest\Authenticator\CookieAuthenticator('YOUTRACK_USERNAME', 'YOUTRACK_PASSWORD');
// Instantiate YouTrack API Cookie Authorizer
$authorizer = new \Cog\YouTrack\Rest\Authorizer\CookieAuthorizer($authenticator);
// Instantiate YouTrack API Client
$youtrack = new \Cog\YouTrack\Rest\Client\YouTrackClient($httpClient, $authorizer);
$method = 'POST'; // GET, POST, PUT, DELETE, PATCH or any custom ones
$response = $youtrack->request($method, '/issue', [
'project' => 'TEST',
'summary' => 'New test issue',
'description' => 'Test description',
]);
You can customize requests created and transferred by a client using request options. Request options control various aspects of a request including, headers, query string parameters, timeout settings, the body of a request, and much more.
$options = [
'debug' => true,
'sink' => '/path/to/dump/file',
];
$response = $youtrack->request('POST', '/issue', [
'project' => 'TEST',
'summary' => 'New test issue',
'description' => 'Test description',
], $options);
$response = $youtrack->get('/issue/TEST-1');
$response = $youtrack->post('/issue', [
'project' => 'TEST',
'summary' => 'New test issue',
'description' => 'Test description',
]);
$response = $youtrack->put('/issue/TEST-1', [
'summary' => 'Updated summary',
'description' => 'Updated description',
]);
$response = $youtrack->delete('/issue/TEST-1');
$youtrack->withHeader('Cookie', 'Overriden cookie');
$response = $youtrack->get('/issue/TEST-1');
$youtrack->withHeaders([
'Cookie' => 'Overriden cookie',
'foo' => 'bar',
]);
$response = $youtrack->get('/issue/TEST-1');
Each successful request to the API returns instance of \Cog\Contracts\YouTrack\Rest\Response\Response
contract. By default it's \Cog\YouTrack\Rest\Response\YouTrackResponse
class.
PSR HTTP response could be accessed by calling httpResponse
method on API Response.
$youtrackResponse = $youtrack->get('/issue/TEST-1');
$psrResponse = $youtrackResponse->httpResponse();
Returns headers as string from the HTTP response.
$apiResponse = $youtrack->get('/issue/TEST-1');
$headerString = $apiResponse->header('Any-Header-You-Want');
Returns Set-Cookie
headers as string from the HTTP response.
$apiResponse = $youtrack->get('/issue/TEST-1');
$cookieString = $apiResponse->cookie();
Returns Location
header from the HTTP response.
$apiResponse = $youtrack->get('/issue/TEST-1');
$location = $apiResponse->location();
$apiResponse = $youtrack->get('/issue/TEST-1');
$responseArray = $apiResponse->toArray();
$apiResponse = $youtrack->get('/issue/TEST-1');
$body = $apiResponse->body();
$apiResponse = $youtrack->get('/issue/TEST-1');
$isSuccess = $apiResponse->isStatusCode(200);
$apiResponse = $youtrack->get('/issue/TEST-1');
$location = $apiResponse->statusCode();
$apiResponse = $youtrack->get('/issue/TEST-1');
$isSuccess = $apiResponse->isSuccess();
$apiResponse = $youtrack->get('/issue/TEST-1');
$isRedirect = $apiResponse->isRedirect();
$apiResponse = $youtrack->get('/issue/TEST-1');
$isRedirect = $apiResponse->isClientError();
$apiResponse = $youtrack->get('/issue/TEST-1');
$isRedirect = $apiResponse->isServerError();