Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using the library outside Laravel #74

Closed
xavier83ar opened this issue Mar 4, 2020 · 7 comments
Closed

Using the library outside Laravel #74

xavier83ar opened this issue Mar 4, 2020 · 7 comments
Labels
question Further information is requested

Comments

@xavier83ar
Copy link

Is it possible to use the library in a framework agnostic way? I am building a package which consumes a json api, and I'd love to take advantage of this package, but it is not clear if it requiere laravel or can be used without it.

If the anwser is true, is there some examples? Thank you so much!

@JaZo
Copy link
Member

JaZo commented Mar 4, 2020

This library is framework agnostic and includes some extras for easier usage with Laravel. It also uses some components of Laravel under the hood. I know some people who succesfully use it in other frameworks such as Drupal. Currently we do not have any examples, sorry.

@JaZo JaZo added the question Further information is requested label Mar 4, 2020
@bbrala
Copy link
Member

bbrala commented Aug 13, 2020

Perhaps this is usefull; a base PHP file and composer.json to get things working :)

<?php
require '../vendor/autoload.php';

use Swis\JsonApi\Client\Parsers\CollectionParser;
use Swis\JsonApi\Client\Parsers\DocumentParser;
use Swis\JsonApi\Client\Parsers\ErrorCollectionParser;
use Swis\JsonApi\Client\Parsers\ErrorParser;
use Swis\JsonApi\Client\Parsers\ItemParser;
use Swis\JsonApi\Client\Parsers\JsonapiParser;
use Swis\JsonApi\Client\Parsers\LinksParser;
use Swis\JsonApi\Client\Parsers\MetaParser;
use Swis\JsonApi\Client\TypeMapper;

$apiClient = new \Swis\JsonApi\Client\Client();

// I choose not to set base url because the "NEXT" links normally contain the whole url
// $apiClient->setBaseUri($baseurl);

/**
 * @return \Swis\JsonApi\Client\Parsers\ResponseParser
 */
function createResponseParser() {
  $metaParser = new MetaParser();
  $linksParser = new LinksParser($metaParser);
  $itemParser = new ItemParser(new TypeMapper(), $linksParser, $metaParser);
  $errorCollectionParser = new ErrorCollectionParser(
    new ErrorParser($linksParser, $metaParser)
  );

  $documentParser = new DocumentParser(
    $itemParser,
    new CollectionParser($itemParser),
    $errorCollectionParser,
    $linksParser,
    new JsonapiParser($metaParser),
    $metaParser
  );

  return new \Swis\JsonApi\Client\Parsers\ResponseParser(
    $documentParser
  );
}
$client = new \Swis\JsonApi\Client\DocumentClient($apiClient, createResponseParser());

$endpointUrl = 'https://cms.contentacms.io/api/recipes';
$response = $client->get($endpointUrl);

/** @var Swis\JsonApi\Client\Collection $collection */
$collection = $response->getData();

// While there is a "next" link there are more pages
while ($response->getLinks()->offsetExists('next')) {
  $response = $client->get($response->getLinks()->offsetGet('next')->getHref());

  // Merge the next page with the current collection
  $collection = $collection->merge($response->getData());
}

/** @var \Swis\JsonApi\Client\Item $item */
foreach ($collection as $item) {
  // Do stuff with the items
  if ($item->hasAttribute('title')) {
    echo $item->title . '<br>';
  }
}

And the composer.json

{
    "name": "bbrala/base-jsonapi-client",
    "require": {
        "swisnl/json-api-client": "^1.1",
        "guzzlehttp/guzzle": "^7.0"
    },
    "authors": [
        {
            "name": "Björn Brala",
            "email": "bjorn@swis.nl"
        }
    ]
}

@JaZo
Copy link
Member

JaZo commented Aug 13, 2020

Seeing this example I think it will be nice to have some kind of factory to setup the ResponseParser so you don't have to do all that yourself if you don't use a container.

@bbrala
Copy link
Member

bbrala commented Aug 13, 2020

Yeah we need to have a factory for creating the base client. That would make this only a few lines long.

@bbrala
Copy link
Member

bbrala commented Aug 13, 2020

Anothing think i notice, i feel $apiClient = new \Swis\JsonApi\Client\Client(); is like the http client, and $client = new \Swis\JsonApi\Client\DocumentClient($apiClient, createResponseParser()); is the actual api client. The naming feels weird :)

@JaZo
Copy link
Member

JaZo commented Aug 13, 2020

That's a fair point. \Swis\JsonApi\Client\Client is basically an enhanced PSR-18 client. It was originally separated so you can use that for file-uploads or other requests that aren't spec compliant, while still having a type hinted JSON:API client. Maybe we can look into combining those in a 2.0 release.

@JaZo
Copy link
Member

JaZo commented Sep 25, 2020

I've added some factory methods to make it easier to use this client outside of Laravel without an IOC container. See https://github.com/swisnl/json-api-client#getting-started for a basic example. Please feel free to report any issues you run into!

@JaZo JaZo closed this as completed Sep 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants