swisnl / json-api-client

A PHP package for mapping remote {json:api} resources to Eloquent like models and collections.
MIT License
205 stars 24 forks source link

How to Setup the "Post" Model? #30

Closed beonami closed 5 years ago

beonami commented 6 years ago

I use Laravel version 5.6. My application wants to create a model but a database from the API (https://jsonplaceholder.typicode.com/posts).

Can it be explained how I designed a model? I don't understand the purpose of the BlogRepository repository where it will be stored.

JaZo commented 6 years ago

Hi @lrfahmi, the link you provided is not a valid JSON:API document. This client is build according to the {json:api} specification. Please make sure you have read and understand the spec.

This client is for making HTTP-requests to a JSON:API server implementation such as swisnl/json-api-server. It is not meant to store items in a database. The repository given in the readme is an example implementation, you are free to use any pattern you like. Please see items for more information about creating your models. Does this answer your questions?

Fivell commented 5 years ago

@JaZo hello, thanks for this library seems it is the best one about building json api clients in PHP, however I also feel lack of documentation with full example and explanation

JaZo commented 5 years ago

@Fivell, thanks for the compliment! You are correct that some parts require (more) documentation or examples, we are still working on that. What are the parts you'd currently like to see explained?

Fivell commented 5 years ago

@JaZo I have experience using ruby and consider readme of it as more-less user friendly take a look https://github.com/JsonApiClient/json_api_client

would be nice to have a wiki or step by step guide how to build sdk using json-api-client with already existed json-api server. For instance if we have Post, Comment and Author models.

And jsonapi specific cases

Fivell commented 5 years ago

@JaZo also would be nice to see recommended way and full example to test client classes mocking HTTP requests

UPD seems already exists using https://github.com/swisnl/php-http-fixture-client

JaZo commented 5 years ago

I've created #46 for the missing documentation.

ammarfrahm commented 5 years ago

I'm sorry for commenting on closed issue. But i have similar confusion on using this package. Currently i'm using Laravel 5.8.

I've created repository for my item, but whenever i instantiate the repository, it return following error

Too few arguments to function Swis\JsonApi\Client\Repository::__construct(), 0 passed in ... exactly 2 expected

Where am i supposed to put the code to pass Swis\JsonApi\Client\Client and Swis\JsonApi\Client\JsonApi\Parser object? I'm thinking about creating base repository class specifically for this.

I thought it has been binded by the ServiceProvider, but somehow it's not working.

JaZo commented 5 years ago

@afrastgeek, thanks for using this package! You should setup your repository as a dependency of your class or resolve it from the container manually. Laravel will then know how to supply those arguments. More info can be found in the Laravel documentation: https://laravel.com/docs/5.8/container#resolving. I see the examples in the readme do not do that, so I'll update those. Below you can find a simple example.

Example (untested)

class BlogRepository extends \Swis\JsonApi\Client\Repository
{
    protected $endpoint = 'blogs';
}

// As dependency
class BlogController extends Controller
{
    private $blogRepository;

    public function __construct(BlogRepository $blogRepository)
    {
        $this->blogRepository = $blogRepository;
    }

    public function show(Request $request)
    {
        return $this->blogRepository->find($request->id)->getData();
    }
}

// Resolved manually
$blogRepository = app(BlogRepository::class);
$blog = $blogRepository->find('1')->getData();
ammarfrahm commented 5 years ago

Thanks for your response, @JaZo ! I'm still unfamiliar with Service Container in Laravel, so thanks for the clue.

I could get it working by passing the dependency in the repository constructor like below.

class BlogRepository extends Repository
{
    protected $endpoint = 'blogs';

    public function __construct()
    {
        parent::__construct(app(DocumentClientInterface::class), app(DocumentFactory::class));
    }
}

But your provided solution are way simpler. So i'll follow your way. And thank you for updating the readme!