Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

pedroroccon's avatar

Dealing with external API Responses

Greetings!

I completed the Laracasts tutorial "Build a Video Game Aggregator". Everything works fine, but there is one more thing that I want to improve.

My application makes a call to the IGDB API, which is a Twitch database that contains a lot of informations about games. Sometimes the API won't returns me all fields that I expect, so in my components I have a function called formatForView() that handles the API request and normalizes the output.

Since I need to repeat this function in my controllers and sometimes in my Livewire components, there is a easier way to do that? Maybe some pattern, or tutorial to handle and normalizes API responses?

You can check my code here: https://github.com/pedroroccon/gamefy

Thanks! Regards!

0 likes
13 replies
Sergiu17's avatar

Hi, you could use a trait for it, parent class or a helper function globally registered

bugsysha's avatar
bugsysha
Best Answer
Level 61

DRY it out and then use DataTransferObject/ValueObject to transfer that information.

1 like
pedroroccon's avatar

I'll create a class called GameData, with some default attributes. That way, if the request does not provide me the attribute, the class will get the default property.

pedroroccon's avatar

After researching I decide to use the Data Transfer Object approach. I downloaded the Spatie package, and start refactoring my code :)

Much better now without repeating the formatForView() in every controller.

Here is my repo after modifications: https://github.com/pedroroccon/gamefy

aurawindsurfing's avatar

Hey @pedroroccon

Now this might seem like too simple to be true but what I have been using lately is simple Laravel build in Http client: https://laravel.com/docs/8.x/http-client

you can simply use it like this:

 $transactions = Http::post(env('PLAID_ENV') . '/transactions/get', [
            'client_id'    => env('PLAID_CLIENT_ID'),
            'secret'       => env('PLAID_SECRET'),
            'access_token' => $this->access_token,
            'start_date'   => '2018-01-01',
            'end_date'     => '2020-12-31',
        ]);

and the data:

$transactions->json();

array:5 [▼
  "accounts" => array:9 [▶]
  "item" => array:7 [▶]
  "request_id" => "5gct38tRiRmz0IW"
  "total_transactions" => 6
  "transactions" => array:6 [▶]
]

add in your model casting to array or object or even better a custom cast and you get your magic. Here is a good link to what @bugsysha is talking about: https://jessarcher.com/blog/casting-json-columns-to-value-objects/

Your code looks good by the way!

bugsysha's avatar

In my mind there is no reason why response should be persisted to the database unless that is the way he caches it for future usage.

martinbean's avatar

@pedroroccon If you’re converting the representation of something in another system, to an internal representation for your application, then that’s the perfect use case for the data mapper pattern.

You’d create a VideoGameDataMapper that has methods to convert a video game from the API response to a VideoGame object in your application, and back again if you needed to send video game data back to the API.

1 like
bugsysha's avatar

If you’re converting the representation of something in another system, to an internal representation for your application, then that’s the perfect use case for the data mapper pattern.

I think that data mapper is bi-directional and since he is only retrieving the data from that external API data mapper is not the correct choice here?

martinbean's avatar

Why does that mean a data mapper is no longer appropriate? A data mapper can be one way if that’s all you need. All it does is map data from one representation to another.

bugsysha's avatar

Cause data mapper is usually between in memory object and persistence layer. Since he only needs a place for received fields and to remedy for the fields that are missing data, data transfer object is the way to go.

On the other hand if he wants a place to even persist that data, then definitely data mapper is the way to go. But then what is the point of Eloquent model?

pedroroccon's avatar

Hey guys. Thanks for replies. I liked the Data Mapper approach as @martinbean said.

I don't need to store the informations anywhere in my application. Just need to read and then show the informations for the user.

The point here is that for some API calls, not all attributes are returned by default, so I need to check everytime if the attribute exists. I have 03 sample codes:

https://github.com/pedroroccon/gamefy/blob/master/app/Http/Controllers/GamesController.php https://github.com/pedroroccon/gamefy/blob/master/app/Http/Livewire/ComingSoon.php https://github.com/pedroroccon/gamefy/blob/master/app/Http/Livewire/PopularGames.php

All of these files makes a request for the API and has a "formatForView()" method. I want to extract the formatView(), so that way, I don't need to repeat this function everytime, because everytime that i call this function I need to format the API parameters.

Please or to participate in this conversation.