Skip to content

Commit

Permalink
Use PSR-18 compatible HttpClient
Browse files Browse the repository at this point in the history
  • Loading branch information
jdecool committed Sep 30, 2020
1 parent 9a342ee commit b190965
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 23 deletions.
14 changes: 5 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
Clockify API client
====================

[![Build Status](https://travis-ci.org/jdecool/clockify-api.svg?branch=master)](https://travis-ci.org/jdecool/clockify-api?branch=master)
[![Build Status](https://img.shields.io/endpoint.svg?url=https%3A%2F%2Factions-badge.atrox.dev%2Fjdecool%2Fclockify-api%2Fbadge%3Fref%3Dmaster&style=flat)](https://actions-badge.atrox.dev/jdecool/clockify-api/goto?ref=master)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/jdecool/clockify-api/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/jdecool/clockify-api/?branch=master)
[![Latest Stable Version](https://poser.pugx.org/jdecool/clockify-api/v/stable.png)](https://packagist.org/packages/jdecool/clockify-api)
Expand All @@ -10,19 +9,16 @@ PHP client for [Clockify.me API](https://clockify.me/developers-api).

## Install it

Install using [composer](https://getcomposer.org), with guzzle6-adapter:
You need to install the library with a PSR-18 compliant HTTP client.

```bash
composer require jdecool/clockify-api php-http/guzzle6-adapter
```

Install using [composer](https://getcomposer.org), with guzzle7-adapter:
Example using Guzzle:

```bash
composer require jdecool/clockify-api php-http/guzzle7-adapter
composer require jdecool/clockify-api guzzlehttp/guzzle http-interop/http-factory-guzzle
```

The library is decoupled from any HTTP message client with [HTTPlug](http://httplug.io). That's why you need to install a client implementation `http://httplug.io/` in this example.
The library is decoupled from any HTTP message client with [HTTPlug](http://httplug.io).
That's why you need to install a client implementation `http://httplug.io/` in this example.

## Getting started

Expand Down
12 changes: 6 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@
}
],
"require": {
"php": "^7.2",
"php": "^7.3",
"ext-json": "*",
"myclabs/php-enum": "^1.7",
"php-http/client-common": "^2.0",
"php-http/client-implementation": "^1.0",
"php-http/discovery": "^1.4",
"php-http/httplug": "^2.0"
"php-http/client-common": "^2.3",
"php-http/discovery": "^1.12",
"php-http/httplug": "^2.1"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^2.15",
"php-http/mock-client": "^1.4",
"guzzlehttp/guzzle": "^7.0",
"http-interop/http-factory-guzzle": "^1.0",
"phpstan/phpstan": "^0.11.16",
"phpunit/phpunit": "^8.0"
},
Expand Down
17 changes: 9 additions & 8 deletions src/ClientBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,26 @@

use Http\Client\{
Common\HttpMethodsClient,
HttpClient
HttpClient,
};
use Http\Message\MessageFactory;
use Psr\Http\Message\RequestFactoryInterface;
use Http\Discovery\{
HttpClientDiscovery,
MessageFactoryDiscovery
Psr17FactoryDiscovery,
Psr18ClientDiscovery,
};

class ClientBuilder
{
private const ENDPOINT_V1 = 'https://api.clockify.me/api/v1/';

private $httpClient;
private $messageFactory;
private $requestFactory;

public function __construct(?HttpClient $httpClient = null, ?MessageFactory $messageFactory = null)
public function __construct(?HttpClient $httpClient = null, ?RequestFactoryInterface $requestFactory = null)
{
$this->httpClient = $httpClient ?? HttpClientDiscovery::find();
$this->messageFactory = $messageFactory ?? MessageFactoryDiscovery::find();
$this->httpClient = $httpClient ?? Psr18ClientDiscovery::find();
$this->requestFactory = $requestFactory ?? Psr17FactoryDiscovery::findRequestFactory();
}

public function createClientV1(string $apiKey): Client
Expand All @@ -34,7 +35,7 @@ public function createClientV1(string $apiKey): Client

public function create(string $endpoint, string $apiKey): Client
{
$http = new HttpMethodsClient($this->httpClient, $this->messageFactory);
$http = new HttpMethodsClient($this->httpClient, $this->requestFactory);

return new Client($http, $endpoint, $apiKey);
}
Expand Down

0 comments on commit b190965

Please sign in to comment.