jetcamp / laravel-json-api-client
Laravel client for easy access of data from {json:api} APIs
Installs: 99
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:laravel-package
Requires
- guzzlehttp/guzzle: ^7.0.1
- woohoolabs/yang: ^3.0
- woohoolabs/yin: ^4.3.0
This package is auto-updated.
Last update: 2025-03-30 12:05:50 UTC
README
Client for easy access of data from {json:api} API. Making requests to API by using PHP HTTP Clients like Guzzle or CURL requires to much code. This package tries to simplify this process, by allowing to get data from API as simply as:
$response = JsonApiClient::get('users'); //Let's return this data to view return view('users', ['users'=> $response->data]);
Installation
- Install package
composer require jetcamp\laravel-json-api-client
. - Add service provider
JetCamp\JsonApiClient\Providers\JsonApiServiceProvider::class
toconfig/app.php
- Publish vendor config
php artisan vendor:publish
.
Usage
Getting the client
You can get client in several ways:
- Via Facade
\JetCamp\JsonApiClient\Facades\JsonAPIClient
- Resolving from service container
JetCamp\JsonApiClient\JsonApiClient::class
Making requests
- get($endpoint)
JsonApiClient::get('users'); //get users
- post($endpoint)
JsonApiClient::jsonData([])->post('users');//store user
- patch($endpoint)
JsonApiClient::jsonData([])->patch('users');//do patch request
Request options
JsonApiClient::get('users')->withIncludes(['posts'])
- adds query paraminclude=posts
to request URL. See http://jsonapi.org/format/#fetching-includesJsonApiClient::get('users')->withFields(['user'=> ['id','name']])
- adds query paramfields[users]=id,name
. See http://jsonapi.org/format/#fetching-sparse-fieldsetsJsonApiClient::get('users')->withFilters(['users'=>['id'=>['eq'=>1]]])
- adds query paramfilter[users][id][eq]=1
. {json:api} is agnostic about filtering, so you can choose your filtering strategy and pass what ever array you want. See http://jsonapi.org/format/#fetching-filtering.JsonApiClient::get('users')->withQuery(['field'=>1])
- adds query paramfield=1=1
. In theory adding filter, includes, fields and pagination fields should be sufficient.JsonApiClient::limit($limit, $offset)->get('users')
- add result constraints to query param `page[limit]=x&page[offet]=y. See http://jsonapi.org/format/#fetching-paginationJsonApiClient::formData(['name'=>'John'])->post('users')
- define post form data. Form data can contain files i.eJsonApiClient::formData(['image'=> $request->file('image')])->post('photos')
.JsonApiClient::jsonData(['name'=>'John'])->post('users')
- define post JSON data.JsonApiClient::throwException(true)
- whenever non 2xx status code received and appropriate exception is thrown. Set tofalse
if you want handel request response status yourself.JsonApiClient::token($accessToken)->get('users')
- define access token which will be added toAuthorization:Bearer {$accessToken}
header. Note, if there isjwt
session variable there is no need to set token here, because access token will be added to request automatically.
Handling response
Requests will return JsonApiResponse
object. It will contain public variables:
$resopnse->data
- contains response data.$resopnse->meta
- contains meta data of a response.$resopnse->errors
- contains errors data of a response.$resopnse->status
- holds HTTP status code of request. Also it has helper functions$resopnse->meta('count')
- return meta data with keycount
.
Exceptions
//@todo