Core: what is the best way to make a proxy/gateway resource to a different rest API

Created on 17 Apr 2019  路  3Comments  路  Source: api-platform/core

Hi, i've used the platform api before and loved it. The 2.4 release had some nice new features.

A feature I'm still missing is getting resources from a different REST API which I would need to make a gateway REST API.

Ideally I'd love to write a resource in the gateway rest API like the code example below to reference a different rest API, but I'd like to do it the proper way.

/**
 * @ProxyApiResource(url="https://demo.api-platform.com/docs.jsonld", remoteResource="Book")
 */
class Book {
    public $title;
    public $isbn;
}

Or maybe with environment variable support for some flexibility?

/**
 * @ProxyApiResource(url="%env(APP_DEMO_API_URL)%", remoteResource="Book")
 */
class Book {
    public $title;
    public $isbn;
}

So my question is: what is the proper way to start such a task? Maybe it's nice to have as a core feature.

question

Most helpful comment

Use a custom DataProvider and DataPersister.

All 3 comments

Why don't you create a custom Data Provider? Configuring via annotation will enforce to decorate a metadata factory, so I suggest to implement some interface:

interface ProxiedResource {
     public function getTarget(): string
}

class Book implements ProxiedResource {
     public function getTarget(): string
         {
               return 'http://...';
         }
}

and check against ProxiedResource implementation within RestrictedDataSource. Piece of cake.

Use a custom DataProvider and DataPersister.

thanks. I think I have enough ideas to write a library.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

CvekCoding picture CvekCoding  路  3Comments

vViktorPL picture vViktorPL  路  3Comments

mahmoodbazdar picture mahmoodbazdar  路  3Comments

stipic picture stipic  路  3Comments

lukasluecke picture lukasluecke  路  3Comments