I am new to Amadeus API, I can’t seem to figure out how to use the data I receive from the API Call. I can print out the data. Can someone guide me in the right direction? Or maybe show me the simplest way to use the data in my app?
I import Amadeus and make a Struct that conforms to codable.
struct Response: Codable {
var results = [Result]()
}
struct Result: Codable{
struct price: Codable {
let total: String
}
struct links: Codable {
let flightDates: String
let flightOffers: String
}
let type: String
let origin: String
let destination: String
let departureDate: String
let returnDate: String
let price: price
let links: links
}
Then I have a function that makes the API Call ->
func testFlightDestinations() {
amadeus.shopping.flightDestinations.get(
params: ["origin": "MAD", "departureDate": "2022-04-20", "duration": "5"],
onCompletion: {
result in
switch result {
case .success(let response):
print(response.data[0])
case .failure(let error):
print("Fail\(error)")
}
})
}
Now this work all fine, I am able to print out the response.data[0], but no matter how I try to decode it, I am not able to use this data… Can anyone with Amadeus experience help me?
Here is my latest version of decoding:
func testFlightDestinations() async {
amadeus.shopping.flightDestinations.get(
params: ["origin": "MAD", "departureDate": "2022-04-20", "duration": "5"],
onCompletion: {
result in
switch result {
case .success(let response):
print(response.data[0])
let data = response.data
let decoder = JSONDecoder()
do{
let decodedData = try decoder.decode(Response, from: response.data)
print(results[0].origin)
} catch {
print("error")
}
case .failure(let error):
print("Fail\(error)")
}
})
}
}
Sample JSON:
[{
"departureDate" : "2022-04-20",
"origin" : "MAD",
"links" : {
"flightOffers" : "https:\/\/test.api.amadeus.com\/v2\/shopping\/flight-offers?originLocationCode=MAD&destinationLocationCode=PMI&departureDate=2022-04-20&returnDate=2022-04-25&adults=1&nonStop=false",
"flightDates" : "https:\/\/test.api.amadeus.com\/v1\/shopping\/flight-dates?origin=MAD&destination=PMI&departureDate=2022-04-20&oneWay=false&duration=5&nonStop=false&viewBy=DURATION"
},
"destination" : "PMI",
"type" : "flight-destination",
"returnDate" : "2022-04-25",
"price" : {
"total" : "46.39"
}
},
New struct from app.quicktype.io =
struct Result: Codable {
let type, returnDate, destination: String
let price: Price
let origin, departureDate: String
let links: Links
}
struct Links: Codable {
let flightOffers, flightDates: String
}
struct Price: Codable {
let total: String
}
From the README file in Amadeus SDK: ## Response
Responses are based on swift closures, that are self-contained blocks of functionality that can be passed around and used in your code.
Every API call response is handled by the OnCompletion
closure which returns
a Result value that
represents either a success, which contains the JSON
object, or a failure,
containing the error.
If the API call contained a JSON response, the SDK will parse that JSON into
.result
attribute. If it contains a data
key, that is made available in
.data
attribute. The raw body of the response is always available in .body
attribute.
Good morning,
did you review this repository: https://github.com/amadeus4dev/amadeus-code-examples
You could find several examples with Swift.