Giter Site home page Giter Site logo

nbp-web-api-php's Introduction

NBP Web API Client

Latest Stable Version Build Status

Simple and fluent interface to retrieve data from NBP Web API.
This library structure is strongly inspired by knplabs/github-api package.

Installation

This package is decoupled from any HTTP client thanks to the HTTPlug.
Requires packages that provide http-client-implementation and psr/http-factory-implementation.
For more information visit HTTPlug for library users.

$ composer require kreyu/nbp-web-api

Usage

use Kreyu\NBPWebApi\Client;
use Kreyu\NBPWebApi\Api\ExchangeRates;

$client = new Client();

// Example call
$client->exchangeRates(ExchangeRates::TABLE_TYPE_A)->forCurrency('EUR')->latest(5);

Changing content type

If you wish to retrieve responses in XML format rather than JSON, you can change it in the client:

use Kreyu\NBPWebApi\Client;
use Kreyu\NBPWebApi\ClientInterface;

$client = new Client();
$client->setContentType(ClientInterface::CONTENT_TYPE_XML);

Providing HTTP client and factories

By default, the HTTP client builder uses HTTP client and factories provided by the discovery.
If you have to provide specific PSR-18 compliant client or any PSR-17 factory, you can pass them to the HTTP client builder:

use Kreyu\NBPWebApi\Client;
use Kreyu\NBPWebApi\Http\ClientBuilder as HttpClientBuilder;

/**
 * @var $client         Psr\Http\Client\ClientInterface
 * @var $requestFactory Psr\Http\Message\RequestFactoryInterface
 * @var $streamFactory  Psr\Http\Message\StreamFactoryInterface
 * @var $uriFactory     Psr\Http\Message\UriFactoryInterface
 */

$builder = new HttpClientBuilder(
    $client,
    $requestFactory,
    $streamFactory,
    $uriFactory
);

$client = new Client($builder);

Caching

To use caching mechanism, provide any PSR-6 compliant item pool to the HTTP client builder:

use Kreyu\NBPWebApi\Client;
use Kreyu\NBPWebApi\Http\ClientBuilder as HttpClientBuilder;

/** @var $pool Psr\Cache\CacheItemPoolInterface */

$builder = new HttpClientBuilder();
$builder->setCache($pool);

$client = new Client($builder);

Using HTTPlug plugins

You can provide any HTTPlug plugin to the HTTP client builder:

use Kreyu\NBPWebApi\Client;
use Kreyu\NBPWebApi\Http\ClientBuilder as HttpClientBuilder;

/** @var $plugin Http\Client\Common\Plugin */

$builder = new HttpClientBuilder();
$builder->addPlugin($plugin);

$client = new Client($builder);

nbp-web-api-php's People

Contributors

kreyu avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Forkers

radziejewicz

nbp-web-api-php's Issues

Possible bug in CurrencyExchangeRates

return $this->get(sprintf('exchangerates/rates/%s/%s', $this->currencyCode, $this->tableType));

I think thant it should be in other sort. As of http://api.nbp.pl/ show that table type must be before currency symbol like

http://api.nbp.pl/api/exchangerates/rates/a/USD/2021-01-04

your class make it like

sprintf('exchangerates/rates/%s/%s', $this->currencyCode, $this->tableType)

Other methods have the same problem ;)

Corrected me if im wrong.

PS Good lib ๐Ÿ‘

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.