I would create an Adapter Class, which take the response and handle it to fit my project, like extract data from the response to properties and the adapter can also expose some helpful methods when needed
Nov 10, 2018
2
Level 1
Best approach to handle third party API responses
What would be the method of choice for handling responses coming from a REST API?
For example consider the following response:
[
"name": "John Doe",
"username": "john",
"posts": [
0: => [
"title" => "First Post",
"body" => "This is the first post."
]
1: => [
"title" => "Second Post",
"body" => "This is the second post."
]
]
]
Basically what I'm trying to do is to have a separate object for each post returned by the API. I assume that one way would be to create two classes (not Eloquent models since I don't want the response to be stored in my database) "User" and "Post" and loop through the array to create objects accordingly.
According to this article I could use Laravel Resources to consume the response but that doesn't seem to work in my case.
Please or to participate in this conversation.