2017 © Pedro Peláez
 

library api-data-fetcher

A client for communication with ATK14 restful API

image

atk14/api-data-fetcher

A client for communication with ATK14 restful API

  • Friday, June 8, 2018
  • by yarri
  • Repository
  • 2 Watchers
  • 0 Stars
  • 2,975 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 14 Versions
  • 17 % Grown

The README.md

ApiDataFetcher

Build Status, (*1)

Client library designed for communication with ATK14 restful API. It should be usable also for other JSON APIs., (*2)

Basic usage

$adf = new ApiDataFetcher("https://skelet.atk14.net/api/");

$data = $adf->get("articles/detail",["id" => 123]);
// or
$data = $adf->get("articles/detail/?id=123");

$title = $data["title"];

In fact in this example a HTTP GET request is made on URL https://skelet.atk14.net/api/en/articles/detail/?id=123&format=json and decoded JSON data is returned., (*3)

A post request can be made this way:, (*4)

$data = $adf->post("articles/create_new",[
  "title" => "Brand New Article",
  "body" => "Once upon a time..."
]);
// or
$data = $adf->post("articles/create_new/?title=Brand+New+Article&body=Once+upon+a+time...");

The parameter format=json is added automatically to every request. It's a convention in ATK14 APIs. This behaviour can be disabled by setting an option in the constructor., (*5)

For completeness there are also methods put and delete., (*6)

$data = $adf->put("articles",[
  "title" => "Brand New Article",
  "body" => "Once upon a time..."
]);
// or
$data = $adf->put("articles/?title=Brand+New+Article&body=Once+upon+a+time...");

$data = $adf->delete("article",["id" => 123]);
// or
$data = $adf->delete("article/?id=123");

A method for uploading a file:, (*7)

$data = $adf->postFile("images/create_new","/path/to/image.jpg",["title" => "Beautiful Flower", "description" => "..."]);
// or
$file_specs = [
  "path" => "/path/to/file",
  "postname" => "image",
  "name" => "flower.jpg",
  "mime_type" => "image/jpeg",
];
$data = $adf->postFile("images/create_new",$file_specs,["title" => "Beautiful Flower", "description" => "..."]);

A handy method for posting JSON:, (*8)

$params = ["param1" => "val1", "param2" => "val2"];
$json = json_encode($params);

$data = $adf->postJson("endpoint",$json);
// or
$data = $adf->postJson("endpoint",$params);

Handling error codes

By default ApiDataFetcher throws an exception in case of a non 2XX response code. In order to handle valid error codes on an API method, specify the codes in the option acceptable_error_codes., (*9)

$data = $adf->post("logins/create_new",[
  "login" => "johny.long",
  "password" => "JulieIsNoMore"
],[
  "acceptable_error_codes" => [
    401, // Unauthorized: Bad password
    404, // Not Found: There is no such user
  ]
]);

if(!$data){
  if($adf->getStatusCode()==401){
     // Bad password
  }
  if($adf->getStatusCode()==404){
     // There is no such user
  }
}

Language

ApiDataFetcher tries to detect automatically currently used language in the running application and use it in the API method call., (*10)

Language can be also specified in the constructor or in the specific API method call., (*11)

$adf = new ApiDataFetcher("https://skelet.atk14.net/api/",["lang" => "en"]);

$data_in_english = $adf->get("articles/detail",["id" => 123]); // performs call to https://skelet.atk14.net/api/en/articles/detail/?id=123&format=json

$data_in_czech = $adf->get("articles/detail",["id" => 123],["lang" => "cs"]); // performs call to https://skelet.atk14.net/api/cs/articles/detail/?id=123&format=json

On a non-ATK14 API you may want to disable language considering at all., (*12)

$adf = new ApiDataFetcher("http://somewhere-on-the.net/json-api/",["lang" => ""]);

$data = $adf->get("articles",["id" => 123]);

HTTP Basic Authentication

Does an API require basic authentication? No problem for the ApiDataFetcher!, (*13)

$adf = new ApiDataFetcher("https://username:password@api-on-the.net/api/");

Caching

$adf->get("articles/index",["year" => "100"],["cache" => 600]); // caching fetched content for 10 minutes (600 seconds)

// use expired cached content when an error occurs
$adf->get("articles/index",["year" => "100"],[
  "cache" => 600,
  "return_cached_content_on_error" => true
]); 

Tracy panel integration

ApiDataFetcher package comes with ApiDataFetcherPanel for easy integration into the popular debugger Tracy (https://packagist.org/packages/tracy/tracy), (*14)

$tracy_bar = Tracy\Debugger::getBar();
$tracy_bar->addPanel(new ApiDataFetcherPanel($api_data_fetcher));

Installation

Use the Composer to install the ApiDataFetcher., (*15)

cd path/to/your/atk14/project/
composer require atk14/api-data-fetcher

In the project configuration file the constant API_DATA_FETCHER_BASE_URL can be defined., (*16)

define("API_DATA_FETCHER_BASE_URL","https://skelet.atk14.net/api/");

Licence

ApiDataFetcher is free software distributed under the terms of the MIT license, (*17)

The Versions

08/06 2018

dev-master

9999999-dev https://github.com/atk14/ApiDataFetcher

A client for communication with ATK14 restful API

  Sources   Download

MIT

The Requires

 

The Development Requires

api restful client atk14

08/06 2018

v1.5.1

1.5.1.0 https://github.com/atk14/ApiDataFetcher

A client for communication with ATK14 restful API

  Sources   Download

MIT

The Requires

 

The Development Requires

api restful client atk14

03/04 2018

v1.5

1.5.0.0 https://github.com/atk14/ApiDataFetcher

Client library for communication with ATK14 restful API

  Sources   Download

MIT

The Requires

 

The Development Requires

api client atk14

01/04 2018

dev-better_statistics

dev-better_statistics https://github.com/atk14/ApiDataFetcher

Client library for communication with ATK14 restful API

  Sources   Download

MIT

The Requires

 

The Development Requires

api client atk14

29/03 2018

1.4.1

1.4.1.0 https://github.com/atk14/ApiDataFetcher

Client library for communication with ATK14 restful API

  Sources   Download

MIT

The Requires

 

The Development Requires

api client atk14

23/03 2018

v1.4

1.4.0.0 https://github.com/atk14/ApiDataFetcher

Client library for communication with ATK14 restful API

  Sources   Download

MIT

The Requires

 

The Development Requires

api client atk14

04/03 2018

v1.3.1

1.3.1.0 https://github.com/atk14/ApiDataFetcher

Client library for communication with ATK14 restful API

  Sources   Download

MIT

The Requires

 

The Development Requires

api client atk14

16/02 2018

v1.3

1.3.0.0 https://github.com/atk14/ApiDataFetcher

Client library for communication with ATK14 restful API

  Sources   Download

MIT

The Requires

 

The Development Requires

api client atk14

14/02 2018

v1.2.1

1.2.1.0 https://github.com/atk14/ApiDataFetcher

Client library for communication with ATK14 restful API

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

api client atk14

14/02 2018

v1.2

1.2.0.0 https://github.com/atk14/ApiDataFetcher

Client library for communication with ATK14 restful API

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

api client atk14

01/02 2018

v1.1

1.1.0.0 https://github.com/atk14/ApiDataFetcher

Client library for communication with ATK14 restful API

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

api client atk14

30/11 2017

v1.0.2

1.0.2.0 https://github.com/atk14/ApiDataFetcher

Client library for communication with ATK14 restful API

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

api client atk14

24/05 2017

v1.0.1

1.0.1.0 https://github.com/atk14/ApiDataFetcher

Client library for communication with ATK14 restful API

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

api client atk14

15/09 2016

v1.0

1.0.0.0 https://github.com/atk14/ApiDataFetcher

Client library for communication with ATK14 restful API

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

api client atk14