{"id":19453460,"url":"https://github.com/jgardezi/pxc-json-client","last_synced_at":"2025-04-25T04:30:55.628Z","repository":{"id":57046083,"uuid":"159612316","full_name":"jgardezi/pxc-json-client","owner":"jgardezi","description":"Copy of swisnl/json-api-client package with configurable headers in the request","archived":false,"fork":false,"pushed_at":"2019-11-22T05:44:52.000Z","size":23,"stargazers_count":1,"open_issues_count":0,"forks_count":3,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-04-03T16:39:04.521Z","etag":null,"topics":["client","json-api","laravel","laravel-package","lumen","php7"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jgardezi.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-11-29T05:27:50.000Z","updated_at":"2022-02-12T10:51:35.000Z","dependencies_parsed_at":"2022-08-24T05:00:21.569Z","dependency_job_id":null,"html_url":"https://github.com/jgardezi/pxc-json-client","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jgardezi%2Fpxc-json-client","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jgardezi%2Fpxc-json-client/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jgardezi%2Fpxc-json-client/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jgardezi%2Fpxc-json-client/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jgardezi","download_url":"https://codeload.github.com/jgardezi/pxc-json-client/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250754596,"owners_count":21481841,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["client","json-api","laravel","laravel-package","lumen","php7"],"created_at":"2024-11-10T17:04:45.296Z","updated_at":"2025-04-25T04:30:52.669Z","avatar_url":"https://github.com/jgardezi.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# { json:api } Client\n\n[![PHP from Packagist](https://img.shields.io/packagist/php-v/swisnl/json-api-client.svg)](https://packagist.org/packages/swisnl/json-api-client)\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/swisnl/json-api-client.svg)](https://packagist.org/packages/swisnl/json-api-client)\n[![Software License](https://img.shields.io/packagist/l/swisnl/json-api-client.svg)](LICENSE)\n[![Build Status](https://travis-ci.org/swisnl/json-api-client.svg?branch=master)](https://travis-ci.org/swisnl/json-api-client)\n[![Scrutinizer Coverage](https://img.shields.io/scrutinizer/coverage/g/swisnl/json-api-client.svg)](https://scrutinizer-ci.com/g/swisnl/json-api-client/?branch=master)\n[![Scrutinizer Code Quality](https://img.shields.io/scrutinizer/g/swisnl/json-api-client.svg)](https://scrutinizer-ci.com/g/swisnl/json-api-client/?branch=master)\n[![Made by PrimeXConnect](https://img.shields.io/badge/%F0%9F%9A%80-made%20by%20SWIS-%23D9021B.svg)](http://www.primexconnect.com/)\n\nA PHP package for mapping remote [{json:api}](http://jsonapi.org/) resources to Eloquent like models and collections.\n\nA copy of [json-api-client](https://github.com/swisnl/json-api-client/)\n\n### @todo this documentation needs to be updated.\n\n## Upgrade from 1.0.x to 1.2.x\n\nThe dependency `swisnl/json-api-client` has been upgraded from '0.10.x' to '0.20.x', supporting Lumen 5.8. In your `ServiceProvider`\n\n- Replace `use Swis\\JsonApi\\Client\\Interfaces\\ParserInterface;\n` with `use Swis\\JsonApi\\Client\\Interfaces\\ResponseParserInterface;` \n- Replace `use Swis\\JsonApi\\Client\\ItemDocumentSerializer;` with `use PXC\\JsonApi\\Client\\ItemDocumentSerializerInterface;`\n- Change the other part of the file accordingly.\n\n## Installation\n\n``` bash\ncomposer require pxc/json-api-client\n```\n\nN.B. Make sure you have installed a HTTP Client before you install this package or install one at the same time e.g. `composer require swisnl/json-api-client php-http/guzzle6-adapter`.\n\n### HTTP Client\n\nWe are decoupled from any HTTP messaging client with the help of [PHP-HTTP](http://php-http.org/).\nThis requires another package providing [php-http/client-implementation](https://packagist.org/providers/php-http/client-implementation).\nTo use Guzzle 6, for example, simply require `php-http/guzzle6-adapter`:\n\n``` bash\ncomposer require php-http/guzzle6-adapter\n```\n\n### Laravel Service Provider\n\nIf you are using Laravel \u003c 5.5 or have disabled package auto discover, you must add the service provider to your `config/app.php` file:\n\n``` php\n'providers' =\u003e [\n    ...,\n    \\Swis\\JsonApi\\Client\\Providers\\ServiceProvider::class,\n],\n```\n\n\n## Getting started\n\nYou can simply require the client as a dependency and use it in your class.\nThis allows you to, for example, make a repository that uses the [DocumentClient](#documentclient):\n\n``` php\nuse Swis\\JsonApi\\Client\\Interfaces\\DocumentClientInterface;\nuse Swis\\JsonApi\\Client\\Interfaces\\ItemDocumentInterface;\n\nclass BlogRepository\n{\n    protected $client;\n\n    public function __construct(DocumentClientInterface $client)\n    {\n        $this-\u003eclient = $client;\n    }\n\n    public function all(array $parameters = [])\n    {\n        return $this-\u003eclient-\u003eget('blogs?'.http_build_query($parameters));\n    }\n\n    public function create(ItemDocumentInterface $document, array $parameters = [])\n    {\n        return $this-\u003eclient-\u003epost('blogs?'.http_build_query($parameters), $document);\n    }\n\n    public function find(string $id, array $parameters = [])\n    {\n        return $this-\u003eclient-\u003eget('blogs/'.urlencode($id).'?'.http_build_query($parameters));\n    }\n\n    public function update(ItemDocumentInterface $document, array $parameters = [])\n    {\n        return $this-\u003eclient-\u003epatch('blogs/'.urlencode($document-\u003egetData()-\u003egetId()).'?'.http_build_query($parameters), $document);\n    }\n\n    public function delete(string $id, array $parameters = [])\n    {\n        return $this-\u003eclient-\u003edelete('blogs/'.urlencode($id).'?'.http_build_query($parameters));\n    }\n}\n```\n\n## Configuration\n\nThe following is the default configuration provided by this package:\n\n``` php\nreturn [\n    /*\n    |--------------------------------------------------------------------------\n    | Base URI\n    |--------------------------------------------------------------------------\n    |\n    | Specify a base uri which will be prepended to every URI.\n    |\n    | Default: empty string\n    |\n    */\n    'base_uri' =\u003e '',\n];\n```\n        \n### Publish Configuration\n\nIf you would like to make changes to the default configuration, publish and edit the configuration file:\n\n``` bash\nphp artisan vendor:publish --provider=\"Swis\\JsonApi\\Client\\Providers\\ServiceProvider\" --tag=\"config\"\n```\n\n\n## Clients\n\nThis package offers two clients; `DocumentClient` and `Client`.\n   \n### DocumentClient\n\nThis is the client that you would generally use.\nPer the [JSON API spec](http://jsonapi.org/format/#document-structure), all requests and responses are documents.\nTherefore, this client always expects a `\\Swis\\JsonApi\\Client\\Interfaces\\DocumentInterface` as input when posting data and always returns this same interface.\nThis can be a plain `Document` when there is no data, an `ItemDocument` for an item, a `CollectionDocument` for a collection or an `InvalidResponseDocument` when the server responds with a non 2xx response.\n\nThe `DocumentClient` follows the following steps internally:\n 1. Send the request using your HTTP client;\n 2. Use [art4/json-api-client](https://github.com/art4/json-api-client) to parse and validate the response;\n 3. Create the correct document instance;\n 4. Hydrate every item by using the item model registered with the `TypeMapper` or a `\\Swis\\JsonApi\\Client\\Item` as fallback;\n 5. Hydrate all relationships;\n 6. Add meta data to the document such as [errors](http://jsonapi.org/format/#errors), [links](http://jsonapi.org/format/#document-links) and [meta](http://jsonapi.org/format/#document-meta).\n\n### Client\n\nThis client is a more low level client and can be used, for example, for posting binary data such as images.\nIt can take everything your request factory takes as input data and returns the 'raw' `\\Psr\\Http\\Message\\ResponseInterface` wrapped in a `\\Swis\\JsonApi\\Client\\Response`.\nIt does not parse or validate the response or hydrate items!\n\n\n## Items\n\nBy default, all items are an instance of `\\Swis\\JsonApi\\Client\\Item`.\nThe `Item` extends [jenssegers/model](https://github.com/jenssegers/model), which provides a Laravel Eloquent-like base class.\nPlease see it's documentation about the features it provides.\nYou can define your own models by extending `\\Swis\\JsonApi\\Client\\Item` or by implementing the `\\Swis\\JsonApi\\Client\\Interfaces\\ItemInterface` yourself.\nThis can be useful if you want to define, for example, hidden attributes, casts or get/set mutators.\nIf you use custom models, you must register them with the [TypeMapper](#typemapper).\n\n\n### Relations\n\nOn top of [jenssegers/model](https://github.com/jenssegers/model), this package has implemented [Laravel Eloquent-like relations](https://laravel.com/docs/eloquent-relationships).\nThese relations provide a fluent interface to retrieve the related items.\nThere are currently four relations available:\n\n * `HasOneRelation`\n * `HasManyRelation`\n * `MorphToRelation`\n * `MorphToManyRelation`\n\nPlease see the following example about defining the relationships:\n\n``` php\nuse Swis\\JsonApi\\Client\\Item;\n\nclass AuthorItem extends Item\n{\n    protected $type = 'author';\n\n    public function blogs()\n    {\n        return $this-\u003ehasMany(BlogItem::class);\n    }\n}\n\nclass BlogItem extends Item\n{\n    protected $type = 'blog';\n\n    public function author()\n    {\n        return $this-\u003ehasOne(AuthorItem::class);\n    }\n}\n```\n\n\n## Collections\n\nThis package uses [Laravel Collections](https://laravel.com/docs/collections) as a wrapper for item arrays.\n\n\n## TypeMapper\n\nAll custom models must be registered with the `TypeMapper`.\nThis `TypeMapper` maps, as the name suggests, JSON API types to custom [item models](#item-models).\n\n### Mapping types\n\nYou can manually register items with the `\\Swis\\JsonApi\\Client\\TypeMapper` or use the supplied `\\Swis\\JsonApi\\Client\\Providers\\TypeMapperServiceProvider`:\n\n``` php\nuse Swis\\JsonApi\\Client\\Providers\\TypeMapperServiceProvider as BaseTypeMapperServiceProvider;\n\nclass TypeMapperServiceProvider extends BaseTypeMapperServiceProvider\n{\n    /**\n     * A list of class names implementing \\Swis\\JsonApi\\Client\\Interfaces\\ItemInterface.\n     *\n     * @var string[]\n     */\n    protected $items = [\n        \\App\\Items\\Author::class,\n        \\App\\Items\\Blog::class,\n        \\App\\Items\\Comment::class,\n    ];\n}\n```\n\n\n## Service Provider\n\nThe `\\Swis\\JsonApi\\Client\\Providers\\ServiceProvider` registers the `TypeMapper`, `JsonApi\\Parser` and both clients; `Client` and `DocumentClient`.\nEach section can be overwritten to allow extended customization.\n\n### Bind TypeMapper\n\nThe service provider registers the `TypeMapper` as a singleton so your entire application has the same mappings available.\n\n### Bind Clients\n\nThe service provider registers the `Client` and `DocumentClient` to your application.\nBy default it uses [php-http/discovery](https://github.com/php-http/discovery) to find a HTTP client and message factory.\nYou can specify your own message factory by overwriting the `getMessageFactory()` method and your own HTTP client by overwriting the `getHttpClient()` method.\nThe first should return a message factory and the latter should return a HTTP client, both implementing HTTPlug.\nThese methods are the perfect place to add extra options to your HTTP client or register a mock HTTP client for your tests:\n\n``` php\nprotected function getHttpClient(): HttpClient\n{\n    if (app()-\u003eenvironment('testing')) {\n        return new \\Swis\\Http\\Fixture\\Client(\n            new \\Swis\\Http\\Fixture\\ResponseBuilder('/path/to/fixtures');\n        );\n    }\n\n    return \\Http\\Adapter\\Guzzle6\\Client::createWithConfig(\n        [\n            'timeout' =\u003e 2,\n        ]\n    );\n}\n```\n\nN.B. This example uses our [swisnl/php-http-fixture-client](https://github.com/swisnl/php-http-fixture-client) when in testing environment.\nThis package allows you to easily mock requests with static fixtures.\nDefinitely worth a try!\n\nIf you register your own service provider and use package auto discover, don't forget to exclude this package in your `package.json`:\n\n``` json\n\"extra\": {\n  \"laravel\": {\n    \"dont-discover\": [\n      \"swisnl/json-api-client\"\n    ]\n  }\n},\n```\n\n\n## Change log\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.\n\n\n## Testing\n\n``` bash\ncomposer test\n```\n\n\n## Contributing\n\nPlease see [CONTRIBUTING](CONTRIBUTING.md) and [CODE_OF_CONDUCT](CODE_OF_CONDUCT.md) for details.\n\n\n## Security\n\nIf you discover any security related issues, please email security@swis.nl instead of using the issue tracker.\n\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE) for more information.\n\n## SWIS\n\n[SWIS](https://www.swis.nl) is a web agency from Leiden, the Netherlands. We love working with open source software.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjgardezi%2Fpxc-json-client","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjgardezi%2Fpxc-json-client","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjgardezi%2Fpxc-json-client/lists"}