ApiFetcher
A wrapper class for biblionet's api.
This library will help you fetch books' data from biblionet database. It provides some helpful methods that simplify the communication with their api.
Tags
Table of Contents
- FETCH_BY_ID = 2
- FETCH_BY_MONTH = 1
- FILL_COMPANIES = 'get_title_companies'
- FILL_CONTRIBUTORS = 'get_contributors'
- FILL_SUBJECTS = 'get_title_subject'
- $apiPassword : string
- biblionet api password
- $apiUsername : string
- biblionet api username
- $fetchedItems : array<string|int, Book>
- keeps the array of fetched items as Book objects
- $logger : Logger
- an instance of the logger class
- $resultsPerPage : int
- the num of results that requested per call
- __construct() : mixed
- ApiFetcher constructor
- fetch() : ApiFetcher
- Fetch books from biblionet's api.
- fill() : ApiFetcher
- Fill with extra data the already fetched items.
- filter() : ApiFetcher
- Filter the already fetched items.
- getItems() : array<string|int, Book>
- Returns the fetched items.
- _makeRequest() : mixed
- Makes the api request.
- _mapResponseToObjects() : array<string|int, mixed>
- Maps api response to the predefined models.
Constants
FETCH_BY_ID
public
mixed
FETCH_BY_ID
= 2
FETCH_BY_MONTH
public
mixed
FETCH_BY_MONTH
= 1
FILL_COMPANIES
public
mixed
FILL_COMPANIES
= 'get_title_companies'
FILL_CONTRIBUTORS
public
mixed
FILL_CONTRIBUTORS
= 'get_contributors'
FILL_SUBJECTS
public
mixed
FILL_SUBJECTS
= 'get_title_subject'
Properties
$apiPassword
biblionet api password
private
string
$apiPassword
$apiUsername
biblionet api username
private
string
$apiUsername
$fetchedItems
keeps the array of fetched items as Book objects
private
array<string|int, Book>
$fetchedItems
= []
Tags
$logger
an instance of the logger class
private
Logger
$logger
Tags
$resultsPerPage
the num of results that requested per call
private
int
$resultsPerPage
Methods
__construct()
ApiFetcher constructor
public
__construct(string $username, string $password[, array<string|int, mixed> $log = [Logger::SUCCESS, Logger::ERROR, Logger::INFO, Logger::WARNING] ][, int $requestTimeout = 10 ][, int $resultsPerPage = 50 ]) : mixed
Parameters
- $username : string
-
Biblionet's api username that required for authentication
- $password : string
-
Biblionet's api password that required for authentication
- $log : array<string|int, mixed> = [Logger::SUCCESS, Logger::ERROR, Logger::INFO, Logger::WARNING]
-
Configure the Logger class
- $requestTimeout : int = 10
-
The timeout in seconds for an api request
- $resultsPerPage : int = 50
-
Number of items to fetch per request. Max value is 50
Return values
mixed —fetch()
Fetch books from biblionet's api.
public
fetch([string $fetchType = ApiFetcher::FETCH_BY_MONTH ][, string|int|array<string|int, mixed> $param1 = NULL ][, string $param2 = NULL ]) : ApiFetcher
You may call with method to fetch data for a specific book, or provide a month to fetch books published in that month. You may also provide two months to fetch books published in that period.
Parameters
- $fetchType : string = ApiFetcher::FETCH_BY_MONTH
-
Provide the fetch type.
- $param1 : string|int|array<string|int, mixed> = NULL
-
Depending on the fetch type, you may input a month or an array with specific book id
- $param2 : string = NULL
-
Used only when fetchType = ApiFetcher::FETCH_BY_MONTH.
Return values
ApiFetcher —fill()
Fill with extra data the already fetched items.
public
fill([array<string|int, mixed> $types = [self::FILL_CONTRIBUTORS, self::FILL_COMPANIES, self::FILL_SUBJECTS] ]) : ApiFetcher
You may use this method to fetch extra data from biblionet's api for the books that you have fetch with the fetch() method. This method, depending the params, makes extra api requests to the api to fetch the requested data, so it may be slow. Use this method if you want to fetch book's subjects, contributors or companies.
Parameters
- $types : array<string|int, mixed> = [self::FILL_CONTRIBUTORS, self::FILL_COMPANIES, self::FILL_SUBJECTS]
-
Provide the extra types of data that you want to fetch from the api. Accepted values are: ApiFetcher::FILL_CONTRIBUTORS, ApiFetcher::FILL_COMPANIES, ApiFetcher::FILL_SUBJECTS
Return values
ApiFetcher —filter()
Filter the already fetched items.
public
filter(string $field, mixed $value[, string $operator = "==" ]) : ApiFetcher
Use this method to narrow down the number of books that have already been fetched depending on specific filters. You may, for example, use this method to keep only the hardcopy books from the fetched items.
Parameters
- $field : string
-
provide a book property
- $value : mixed
-
the value to search for in the property
- $operator : string = "=="
-
the operation to use for the comparison
Return values
ApiFetcher —getItems()
Returns the fetched items.
public
getItems() : array<string|int, Book>
Use this method to get all the data that have been fetch from biblionet's api.
Tags
Return values
array<string|int, Book> —an array of Book objects
_makeRequest()
Makes the api request.
private
_makeRequest(string $method, array<string|int, mixed> $params) : mixed
Just a helper method to make the requested api request with the help of Guzzle
Parameters
- $method : string
-
the api method
- $params : array<string|int, mixed>
-
an array with params for the api call except authentication params
Return values
mixed —_mapResponseToObjects()
Maps api response to the predefined models.
private
_mapResponseToObjects(array<string|int, mixed> $responseData) : array<string|int, mixed>
Parameters
- $responseData : array<string|int, mixed>
-
the data returned by the api request
Return values
array<string|int, mixed> —a list of Book std objects