Download the PHP package cerbero/lazy-json-pages without Composer

On this page you can find all versions of the php package cerbero/lazy-json-pages. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package lazy-json-pages

๐Ÿ“œ Lazy JSON Pages

Author PHP Version Build Status Coverage Status Quality Score PHPStan Level Latest Version PER Total Downloads

Framework-agnostic API scraper to load items from any paginated JSON API into a Laravel lazy collection via async HTTP requests.

[!TIP] Need to read large JSON with no pagination in a memory-efficient way?

Consider using ๐Ÿผ Lazy JSON or ๐Ÿงฉ JSON Parser instead.

๐Ÿ“ฆ Install

Via Composer:

๐Ÿ”ฎ Usage

๐Ÿ‘ฃ Basics

Depending on our coding style, we can instantiate Lazy JSON Pages in 4 different ways:

The variable $source in our examples represents any source that points to a paginated JSON API. Once we define the source, we can then chain methods to define how the API is paginated:

When calling collect(), we indicate that the pagination structure is defined and that we are ready to collect the paginated items within a Laravel lazy collection, where we can loop through the items one by one and apply filters and transformations in a memory-efficient way.

๐Ÿ’ง Sources

A source is any means that can point to a paginated JSON API. A number of sources is supported by default:

Here are some examples of sources:

If none of the above sources satifies our use case, we can implement our own source.

Click here to see how to implement a custom source. To implement a custom source, we need to extend `Source` and implement 2 methods: The parent class `Source` gives us access to 2 properties: - `$source`: the custom source for our use case - `$client`: the Guzzle HTTP client The methods to implement turn our custom source into a PSR-7 request and a PSR-7 response. Please refer to the [already existing sources](https://github.com/cerbero90/json-parser/tree/master/src/Sources) to see some implementations. Once the custom source is implemented, we can instruct Lazy JSON Pages to use it: If you find yourself implementing the same custom source in different projects, feel free to send a PR and we will consider to support your custom source by default. Thank you in advance for any contribution!

๐Ÿ›๏ธ Pagination structure

After defining the source, we need to let Lazy JSON Pages know what the paginated API looks like.

If the API uses a query parameter different from page to specify the current page - for example ?current_page=1 - we can chain the method pageName():

Otherwise, if the number of the current page is present in the URI path - for example https://example.com/users/page/1 - we can chain the method pageInPath():

By default the last integer in the URI path is considered the page number. However we can customize the regular expression used to capture the page number, if need be:

Some API paginations may start with a page different from 1. If that's the case, we can define the first page by chaining the method firstPage():

Now that we have customized the basic structure of the API, we can describe how items are paginated depending on whether the pagination is cursor based.

๐Ÿ“ Length-aware paginations

The term "length-aware" indicates any pagination containing at least one of the following length information:

Lazy JSON Pages only needs one of these details to work properly:

If the length information is nested in the JSON body, we can use dot-notation to indicate the level of nesting. For example, pagination.total_pages means that the total number of pages sits in the object pagination, under the key total_pages.

Otherwise, if the length information is displayed in the headers, we can use the same methods to gather it by simply defining the name of the header:

APIs can expose their length information in the form of numbers (total_pages: 10) or URIs (last_page: "https://example.com?page=10"), Lazy JSON Pages supports both.

If the pagination works with an offset, we can configure it with the offset() method. The value of the offset will be calculated based on the number of items present on the first page:

โ†ช๏ธ Cursor-aware paginations

Not all paginations are length-aware, some may be built in a way where each page has a cursor pointing to the next page.

We can tackle this kind of pagination by indicating the key or the header holding the cursor:

The cursor may be a number, a string or a URI: Lazy JSON Pages supports them all.

๐Ÿ”— Link header paginations

Some paginated API responses include a header called Link. An example is GitHub: if we inspect the response headers, we can see the Link header looking like this:

To lazy-load items from a Link header pagination, we can chain the method linkHeader():

๐Ÿ‘ฝ Custom paginations

Lazy JSON Pages provides several methods to extract items from the most popular pagination mechanisms. However if we need a custom solution, we can implement our own pagination.

Click here to see how to implement a custom pagination. To implement a custom pagination, we need to extend `Pagination` and implement 1 method: The parent class `Pagination` gives us access to 3 properties: - `$source`: the [source](#-sources) pointing to the paginated JSON API - `$client`: the Guzzle HTTP client - `$config`: the configuration that we generated by chaining methods like `totalPages()` The method `getIterator()` defines the logic to extract paginated items in a memory-efficient way. Please refer to the [already existing paginations](https://github.com/cerbero90/json-parser/tree/master/src/Paginations) to see some implementations. Once the custom pagination is implemented, we can instruct Lazy JSON Pages to use it: If you find yourself implementing the same custom pagination in different projects, feel free to send a PR and we will consider to support your custom pagination by default. Thank you in advance for any contribution!

๐Ÿš€ Requests optimization

Paginated APIs differ from each other, so Lazy JSON Pages lets us tweak our HTTP requests specifically for our use case.

By default HTTP requests are sent synchronously. If we want to send more than one request without waiting for the response, we can call the async() method and set the number of concurrent requests:

[!NOTE]
Please note that asynchronous requests improve speed at the expense of memory, as more responses are going to be loaded at once.

Several APIs set rate limits to reduce the number of allowed requests for a period of time. We can instruct Lazy JSON Pages to respect such limits by throttling our requests:

Internally, Lazy JSON Pages uses Guzzle as its HTTP client. We can customize the client behavior by adding as many middleware as we need:

If we need a middleware to be added every time we invoke Lazy JSON Pages, we can add a global middleware:

Sometimes writing Guzzle middleware might be cumbersome. Alternatively Lazy JSON Pages provides convenient methods to fire callbacks when sending a request or receiving a response:

We can also tweak the number of allowed seconds before an API connection times out or the allowed duration of the entire HTTP request (by default they are both set to 5 seconds):

If the 3rd party API is faulty or error-prone, we can indicate how many times we want to retry failing HTTP requests and the backoff strategy to compute the milliseconds to wait before retrying (by default failing requests are repeated 3 times after an exponential backoff of 100, 400 and 900 milliseconds):

๐Ÿ’ข Errors handling

If something goes wrong during the scraping process, we can intercept the error and execute a custom logic to handle it:

Any exception thrown by this package extends the LazyJsonPagesException class. This makes it easy to handle all exceptions in a single catch block:

For reference, here is a comprehensive table of all the exceptions thrown by this package:

Cerbero\LazyJsonPages\Exceptions\ thrown when
InvalidKeyException a JSON key does not contain a valid value
InvalidPageInPathException a page cannot be found in the URI path
InvalidPaginationException a pagination implementation is not valid
OutOfAttemptsException an HTTP request failed too many times
RequestNotSentException a JSON source didn't send any HTTP request
UnsupportedPaginationException a pagination is not supported
UnsupportedSourceException a JSON source is not supported

๐Ÿค Laravel integration

If used in a Laravel project, Lazy JSON Pages automatically fires events when:

This is especially handy for debugging tools like Laravel Telescope or Spatie Ray or for triggering the related event listeners.

๐Ÿ“† Change log

Please see CHANGELOG for more information on what has changed recently.

๐Ÿงช Testing

๐Ÿ’ž Contributing

Please see CODE_OF_CONDUCT for details.

๐Ÿงฏ Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

๐Ÿ… Credits

โš–๏ธ License

The MIT License (MIT). Please see License File for more information.


All versions of lazy-json-pages with dependencies

PHP Build Version
Package Version
Requires php Version ^8.1
cerbero/json-parser Version ^1.1
guzzlehttp/guzzle Version ^7.2
illuminate/collections Version >=8.12
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package cerbero/lazy-json-pages contains the following files

Loading the files please wait ....