Example of fast creation of PHP client for Jira REST API.

Hi! In this article I want to demostrate the way you can create PHP clients to the REST APIs. And the example will be for Jira REST API.
First of all I shall use my package wich will help me in this quest.

First of all we need class wich will represent connection to tha API. IT will not be a real connection but it will contain all necessary information for performing requests to the API:

class Connection
{
private $connection = null; /**
* Connection to the Jira REST API
*
* @param string $url
* jira server URL
* @param string $login
* user login
* @param string $token
* access token. See this page https://confluence.atlassian.com/cloud/api-tokens-938839638.html for more information
*/
public function __construct(string $url = '', string $login = '', string $token = '')
{
if ($url !== '') {
$this->connect($url, $login, $token);
}
}
/**
* Connection to the Jira REST API
*
* @param string $url
* jira server URL
* @param string $login
* user login
* @param string $token
* access token. See this page https://confluence.atlassian.com/cloud/api-tokens-938839638.html for more information
*/
public function connect(string $url, string $login, string $token): void
{
$this->connection = new CustomClient($url, [
'Authorization: Basic ' . base64_encode($login . ':' . $token)
]);
}
/**
* Method sends GET request to Jira server
*
* @param string $endpoint
* endpoint to REST method
* @return object result
*/
public function sendGetRequest(string $endpoint): object
{
// trait string as JSON object
return json_decode($this->connection->sendGetRequest($endpoint), false);
}
}

Quite simple yeah? )

Now let’s fetch some projects as a proof that our class works fine:

class ProjectsRepository extends ArrayObject
{
/**
* Connection to Jira
*
* @var Connection
*/
private $connection = null;
/**
* Constructor
*
* @param Connection $connection
* connection to Jira
*/
public function __construct(Connection $connection)
{
$this->connection = $connection;
}
/**
* Method loads list of projects from server
*/
public function loadProjects(): void
{
// initial setup
$result = [];
$startAt = 0;
// main loop
do {
// reading data
$page = $this->connection->sendGetRequest('/project/search/?startAt=' . $startAt);
// and here we read all projects in loop
} while (! $page->isLast);
}
}

As I’ve said earlier it is quite easy )

PS you can download the full package with the example of Jira Client via this link.

--

--

--

php developer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Must have composer packages for Laravel developers.

Must have composer packages for Laravel developers

Get Started with RT-Thread Smart OpenSource Microkernel Operating System!

Working with web services through ASP.NET Core and DotVVM (A step by step guide)

I have a github account now what?

Grand Community Boosting Program

Build A Voice-Controlled Mouse In 5 minutes

READ/DOWNLOAD%+ Coaching Agile Teams: A Companion for ScrumMasters, Agile Coaches, and Project…

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Dodonov Alexey

Dodonov Alexey

php developer

More from Medium

How can I create a Mediumblob within the Laravel schema builder?

How to set up Laravel with PostgreSQL? 💡

Laravel 8 Group By Query Example

Get started with Symfony 6 for beginners