-
Notifications
You must be signed in to change notification settings - Fork 33
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
Multiple API Sources (Laravel) #8
Comments
Good question, you're right, but it'is a documentation mistake. If you'are following the guide about the Wrapper (https://github.com/CristalTeam/php-api-wrapper/blob/master/docs/more-about-wrapper.md) you've now a $this->app->bind(CustomWrapper::class, function(){
$transport = new Basic(
'username',
'password',
'http://api.example.com/v1/',
$this->app->make(Curl::class)
);
return new CustomWrapper($transport);
}); We keep the issue open to fix the document. |
When you try to bind differents wrapper to differents entities, the last binding instruction override the first one. public function register()
{
$this->app->bind(VideoWrapper::class, function(){
$transport = new Transport(
'https://xxx.tv/',
$this->app->make(Curl::class)
);
return new VideoWrapper($transport);
});
$this->app->bind(StreamWrapper::class, function(){
$transport = new Bearer(
'',
'https://xxx.tv/',
$this->app->make(Curl::class)
);
//dd($transport);
return new StreamWrapper($transport);
});
}
public function boot()
{
// API models
Video::setApi($this->app->make(VideoWrapper::class));
Stream::setApi($this->app->make(StreamWrapper::class));
} so for example getVideo will not be accessible in VideoWrapper ... |
I'm not realy sure to understand your problem :
|
J'ai deux wrappers que je bind comme ceci dans le provider public function boot()
{
// API models
Stream::setApi($this->app->make(StreamWrapper::class));
Video::setApi($this->app->make(VideoWrapper::class));
} Lorsque je fais Video::find(1234) je récupère bien un objet Video complet App\Models\Api\Video {#208
#entity: "video"
#primaryKey: "id"
#casts: []
+exists: true
+wasRecentlyCreated: false
#attributes: array:22 [
"id" => "89d1adf3-9b49-4282-a89d-041c9e3f17a4"
"type" => "ASSET"
"title" => "10 Cloverfield Lane"
"shortSynopsis" => "After a car crash, a woman wakes up inside a sealed bunker with two men. Confused and frightened, she is told a devastating chemical attack has made the air outside deadly. But as she fears for her life inside, she must prepare for what awaits outside."
"longSynopsis" => "After a car crash, a woman wakes up inside a sealed bunker with two men. Confused and frightened, she is told a devastating chemical attack has made the air outside deadly. But as she fears for her life inside, she must prepare for what awaits outside."
"posterImage" => ""
"boxcoverImage" => ""
"adult" => false
"minimumAge" => 12
"parentalRating" => []
"duration" => 5940
"credits" => array:4 [
0 => array:2 [
"role" => "ACTOR"
"name" => "John Gallagher Jr."
]
1 => array:2 [
"role" => "ACTOR"
"name" => "John Goodman"
]
2 => array:2 [
"role" => "ACTOR"
"name" => "Mary Elizabeth Winstead"
]
3 => array:2 [
"role" => "DIRECTOR"
"name" => "Dan Trachtenberg"
]
]
"productionYear" => 2016
"productionLocations" => array:1 [
0 => "US"
]
"tags" => array:2 [
0 => "movies"
1 => "drama"
]
"genres" => array:1 [
0 => "3.4"
]
"streams" => array:1 [
0 => array:3 [
"id" => "a68a97bc-af3a-4528-969f-c61b322754da"
"type" => "FULL"
"resolution" => "SD"
]
]
"validFrom" => "2017-12-15T23:00:00Z"
"validUntil" => "2020-12-31T23:59:00Z"
"score" => 0
"operatorScore" => 0
"catalogs" => []
]
#original: array:22 [
"id" => "89d1adf3-9b49-4282-a89d-041c9e3f17a4"
"type" => "ASSET"
"title" => "10 Cloverfield Lane"
"shortSynopsis" => "After a car crash, a woman wakes up inside a sealed bunker with two men. Confused and frightened, she is told a devastating chemical attack has made the air outside deadly. But as she fears for her life inside, she must prepare for what awaits outside."
"longSynopsis" => "After a car crash, a woman wakes up inside a sealed bunker with two men. Confused and frightened, she is told a devastating chemical attack has made the air outside deadly. But as she fears for her life inside, she must prepare for what awaits outside."
"posterImage" => ""
"boxcoverImage" => ""
"adult" => false
"minimumAge" => 12
"parentalRating" => []
"duration" => 5940
"credits" => array:4 [
0 => array:2 [
"role" => "ACTOR"
"name" => "John Gallagher Jr."
]
1 => array:2 [
"role" => "ACTOR"
"name" => "John Goodman"
]
2 => array:2 [
"role" => "ACTOR"
"name" => "Mary Elizabeth Winstead"
]
3 => array:2 [
"role" => "DIRECTOR"
"name" => "Dan Trachtenberg"
]
]
"productionYear" => 2016
"productionLocations" => array:1 [
0 => "US"
]
"tags" => array:2 [
0 => "movies"
1 => "drama"
]
"genres" => array:1 [
0 => "3.4"
]
"streams" => array:1 [
0 => array:3 [
"id" => "a68a97bc-af3a-4528-969f-c61b322754da"
"type" => "FULL"
"resolution" => "SD"
]
]
"validFrom" => "2017-12-15T23:00:00Z"
"validUntil" => "2020-12-31T23:59:00Z"
"score" => 0
"operatorScore" => 0
"catalogs" => []
]
#changes: []
#appends: []
#dates: []
#dateFormat: "Y-m-d H:i:s"
#relations: []
#hidden: []
#visible: []
} par contre si j'inverse les bind dans le provider Video::setApi($this->app->make(VideoWrapper::class));
Stream::setApi($this->app->make(StreamWrapper::class)); et que je refais : Video::find(1234) l'objet est vide :
et donc inversement pour Stream. public function getVideo($id)
{
return $this->transport->request('/api/vod/v1/vod/' . $id);
} ça fonctionne... mais cette fonction devrait être dans le StreamWrapper. $video = new Video();
$stream = new Stream();
dd($video->getApi(), $stream->getApi()); j'obtiens ceci :
J'ai le même Wrapper pour les deux models |
Hum, ok ok, indeed, after checking, there may be a problem in the documentation. Try to define different $api properties on your Video and Stream models. For example : class Video extends Model
{
protected static $api = 'video'; // for example
} and, class Stream extends Model
{
protected static $api = 'stream'; // for example
} Please, tell me, if it's works. |
(careful your token is shown in trace) |
Yes, It works ! Many thanks for your help. ps: Token was altered before posting and only valid in my local environment but thank you for your benevolence. Great work, many thanks for this package ! |
Nice 👍 |
Hello, I’ve just started messing around with this package and was wondering if you could point me in the right direction.
I am working in Laravel and using the Laravel “stack”.
My test project draws data from three individual API sources (totally different URL). What would be the best practice to attach each endpoint to the three individual associated models?
I’ve followed the guide but it seems to relate to using one single API source.
The text was updated successfully, but these errors were encountered: