vapor - Obtaining data from mysql using alsodecode() - fluent

I'm trying to obtain data from a mysql database joining two of my tables (usuarios and colegios) with the function alsodecode().
My code looks like this:
func usuariosHandler(_ req: Request) throws -> Future<View> {
return Usuario.query(on: req).join(\Colegio.id, to: \Usuario.colegioId).alsoDecode(Colegio.self).all().flatMap(to: View.self) { usuarios in
let contexto = UsuariosContext(title: "Usuarios de la aplicaciĆ³n", usuarios: usuarios)
return try req.view().render("usuarios", contexto)
}
}
The problem is that the result of the query is a tuple that I have to send to a view through leaf. In addition, this is the error into the compiler: Cannot convert value of type '[(Usuario, Colegio)]' to expected argument type '[Usuario]'
I'm a beginner of Swift and Vapor. Can anybody help me with this?
Thanks!

You need to use an Encodable structure to pass tuples - one structure to hold the tuple data and one for the context:
struct MyTuple: Encodable {
let name: String // put the fields from the join in here
}
struct MyContext Encodable {
let title: String
let usuarios: [MyTuple]
}
Then modify your route to create the data structure to pass as the context:
func usuariosHandler(_ req: Request) throws -> Future<View> {
return Usuario.query(on: req).join(\Colegio.id, to: \Usuario.colegioId).alsoDecode(Colegio.self).all().flatMap(to: View.self) { usuarios in
var usuariosTuples: [MyTuple] = []
for usuario in usuarios {
usuariosTuples.append( MyTuple( usuario.0 ) )
}
let contexto = UsuariosContext(title: "Usuarios de la aplicaciĆ³n", usuarios: usuariosTuples)
return try req.view().render("usuarios", contexto)
}
}
I've assumed the first field is a string.

Related

Automatic parsing JSON with Swift 5 and Alomofire

I am calling an API via Alomofire in Swift 5 and get the following Result Set in JSON format :
Here is the call :
AF.request("https://shopping.yahooapis.jp/ShoppingWebService/V1/json/itemSearch", parameters: parameters).responseJSON { response in
print(response.result)
}
I have the following info in the result : Content-Encoding: gzip, the solution may come from here)
Here is the JSON Response body :
{"ResultSet":{"totalResultsAvailable":"3","totalResultsReturned":3,"firstResultPosition":"1","0":{"Result":{"Request":{"Query":""},"Modules":"","0":{"Name":"\u3010\u30ad\u30e3\u30c3\u30b7\u30e5\u30ec\u30b95\uff05\u9084\u5143\u3011\u30ab\u30eb\u30d3\u30fc\u3000\u304b\u3063\u3071\u3048\u3073\u305b\u3093\u7d00\u5dde\u306e\u6885\u547370g\u3000\u3010\u30a4\u30fc\u30b8\u30e3\u30d1\u30f3\u30e2\u30fc\u30eb\u3011","Description":"\u3010\u691c\u7d22\u30ad\u30fc\u30ef\u30fc\u30c9\uff08\u5546\u54c1\u5185\u5bb9\u3092\u4fdd\u969c\u3059\u308b\u3082\u306e\u3067\u306f\u3042\u308a\u307e\u305b\u3093\uff09\u3011\u304a\u83d3\u5b50\u3000\u304a\u3084\u3064\u3000\u30b9\u30ca\u30c3\u30af\u3000\u30b9\u30ca\u30c3\u30af\u83d3\u5b50\u3000\u30ab\u30c3\u30d1\u30a8\u30d3\u30bb\u30f3\u3000\u3046\u3081\u5473\u3000\u30a6\u30e1\u3000\u6d77\u8001\u3000\u30ab\u30eb\u30b7\u30a6\u30e0","Headline":"","Url":"https:\/\/store.shopping.yahoo.co.jp\/ejapan\/4901330197711f.html","ReleaseDate":"","Availability":"instock","Code":"ejapan_4901330197711f","Condition":"new","Image":{"Id":"ejapan_4901330197711f","Small":"https:\/\/item-shopping.c.yimg.jp\/i\/c\/ejapan_4901330197711f","Medium":"https:\/\/item-shopping.c.yimg.jp\/i\/g\/ejapan_4901330197711f"},"Review":{"Rate":"0.00","Count":"0","Url":"https:\/\/shopping.yahoo.co.jp\/review\/item\/list?store_id=ejapan&page_key=4901330197711f"},"Affiliate":{"Rate":"1.0"},"Price":{"_attributes":{"currency":"JPY"},"_value":"121"},"PremiumPrice":"","PriceLabel":{"_attributes":{"taxIncluded":"true"},"FixedPrice":"","DefaultPrice":"121","SalePrice":"","PremiumPriceStatus":"0","PremiumPrice":"121","PremiumDiscountType":"","PremiumDiscountRate":"","PeriodStart":"","PeriodEnd":""},"Point":{"Amount":"1","Times":"1","PremiumAmount":"1","PremiumTimes":"1"},"Shipping":{"Code":"1","Name":"\u8a2d\u5b9a\u7121\u3057"},"Category":{"Current":{"Id":"13451","Name":"\u305d\u306e\u4ed6\u30b9\u30ca\u30c3\u30af\u3001\u304a\u83d3\u5b50\u3001\u304a\u3064\u307e\u307f"}},"CategoryIdPath":{"0":{"Id":"1"},"1":{"Id":"2498"},"2":{"Id":"4745"},"3":{"Id":"13451"},"_container":"Category"},"Brands":{"Name":"\u30ab\u30eb\u30d3\u30fc","Path":{"0":{"Id":"1"},"1":{"Id":"16267"},"_container":"Brand"}},"JanCode":"4901330197711","Model":"","IsbnCode":"","Store":{"Id":"ejapan","Name":"e\u30b8\u30e3\u30d1\u30f3","Url":"https:\/\/store.shopping.yahoo.co.jp\/ejapan\/","Payment":{"0":{"Code":"1","Name":"\u30af\u30ec\u30b8\u30c3\u30c8\u30ab\u30fc\u30c9"},"1":{"Code":"16","Name":"Yahoo!\u30a6\u30a9\u30ec\u30c3\u30c8\u306b\u767b\u9332\u3057\u3066\u3044\u308b\u30af\u30ec\u30b8\u30c3\u30c8\u30ab\u30fc\u30c9"},"2":{"Code":"4","Name":"\u5546\u54c1\u4ee3\u5f15"},"3":{"Code":"2","Name":"\u9280\u884c\u632f\u8fbc"},"4":{"Code":"8","Name":"\u90f5\u4fbf\u632f\u66ff"},"5":{"Code":"1024","Name":"\u30bd\u30d5\u30c8\u30d0\u30f3\u30af\u307e\u3068\u3081\u3066\u652f\u6255\u3044"},"6":{"Code":"4096","Name":"PayPay"},"_container":"Method"},"IsBestStore":"false","Ratings":{"Rate":"4.5","Count":"11871","Total":"51092","DetailRate":"4.3"},"Image":{"Id":"ejapan_1","Medium":"https:\/\/item-shopping.c.yimg.jp\/s\/h\/ejapan_1"},"IsPMallStore":"false"},"IsAdult":"0","Deliveryinfo":{"Area":"","Deadline":"","Day":""},"_attributes":{"index":"1"}},"1":{"Name":"\u3010\u30ad\u30e3\u30c3\u30b7\u30e5\u30ec\u30b95\uff05\u9084\u5143\u3011\u2605\u307e\u3068\u3081\u8cb7\u3044\u2605\u3000\u30ab\u30eb\u30d3\u30fc\u3000\u304b\u3063\u3071\u3048\u3073\u305b\u3093\u7d00\u5dde\u306e\u6885\u547370g\u3000\u3000\u00d712\u500b\u3010\u30a4\u30fc\u30b8\u30e3\u30d1\u30f3\u30e2\u30fc\u30eb\u3011","Description":"\u3010\u691c\u7d22\u30ad\u30fc\u30ef\u30fc\u30c9\uff08\u5546\u54c1\u5185\u5bb9\u3092\u4fdd\u969c\u3059\u308b\u3082\u306e\u3067\u306f\u3042\u308a\u307e\u305b\u3093\uff09\u3011\u304a\u83d3\u5b50\u3000\u304a\u3084\u3064\u3000\u30b9\u30ca\u30c3\u30af\u3000\u30b9\u30ca\u30c3\u30af\u83d3\u5b50\u3000\u30ab\u30c3\u30d1\u30a8\u30d3\u30bb\u30f3\u3000\u3046\u3081\u5473\u3000\u30a6\u30e1\u3000\u6d77\u8001\u3000\u30ab\u30eb\u30b7\u30a6\u30e0","Headline":"","Url":"https:\/\/store.shopping.yahoo.co.jp\/ejapan\/012-4901330197711f.html","ReleaseDate":"","Availability":"instock","Code":"ejapan_012-4901330197711f","Condition":"new","Image":{"Id":"ejapan_012-4901330197711f","Small":"https:\/\/item-shopping.c.yimg.jp\/i\/c\/ejapan_012-4901330197711f","Medium":"https:\/\/item-shopping.c.yimg.jp\/i\/g\/ejapan_012-4901330197711f"},"Review":{"Rate":"0.00","Count":"0","Url":"https:\/\/shopping.yahoo.co.jp\/review\/item\/list?store_id=ejapan&page_key=012-4901330197711f"},"Affiliate":{"Rate":"1.0"},"Price":{"_attributes":{"currency":"JPY"},"_value":"1398"},"PremiumPrice":"","PriceLabel":{"_attributes":{"taxIncluded":"true"},"FixedPrice":"","DefaultPrice":"1398","SalePrice":"","PremiumPriceStatus":"0","PremiumPrice":"1398","PremiumDiscountType":"","PremiumDiscountRate":"","PeriodStart":"","PeriodEnd":""},"Point":{"Amount":"13","Times":"1","PremiumAmount":"13","PremiumTimes":"1"},"Shipping":{"Code":"1","Name":"\u8a2d\u5b9a\u7121\u3057"},"Category":{"Current":{"Id":"13451","Name":"\u305d\u306e\u4ed6\u30b9\u30ca\u30c3\u30af\u3001\u304a\u83d3\u5b50\u3001\u304a\u3064\u307e\u307f"}},"CategoryIdPath":{"0":{"Id":"1"},"1":{"Id":"2498"},"2":{"Id":"4745"},"3":{"Id":"13451"},"_container":"Category"},"Brands":{"Name":"\u30ab\u30eb\u30d3\u30fc","Path":{"0":{"Id":"1"},"1":{"Id":"16267"},"_container":"Brand"}},"JanCode":"4901330197711","Model":"","IsbnCode":"","Store":{"Id":"ejapan","Name":"e\u30b8\u30e3\u30d1\u30f3","Url":"https:\/\/store.shopping.yahoo.co.jp\/ejapan\/","Payment":{"0":{"Code":"1","Name":"\u30af\u30ec\u30b8\u30c3\u30c8\u30ab\u30fc\u30c9"},"1":{"Code":"16","Name":"Yahoo!\u30a6\u30a9\u30ec\u30c3\u30c8\u306b\u767b\u9332\u3057\u3066\u3044\u308b\u30af\u30ec\u30b8\u30c3\u30c8\u30ab\u30fc\u30c9"},"2":{"Code":"4","Name":"\u5546\u54c1\u4ee3\u5f15"},"3":{"Code":"2","Name":"\u9280\u884c\u632f\u8fbc"},"4":{"Code":"8","Name":"\u90f5\u4fbf\u632f\u66ff"},"5":{"Code":"1024","Name":"\u30bd\u30d5\u30c8\u30d0\u30f3\u30af\u307e\u3068\u3081\u3066\u652f\u6255\u3044"},"6":{"Code":"4096","Name":"PayPay"},"_container":"Method"},"IsBestStore":"false","Ratings":{"Rate":"4.5","Count":"11875","Total":"51100","DetailRate":"4.3"},"Image":{"Id":"ejapan_1","Medium":"https:\/\/item-shopping.c.yimg.jp\/s\/h\/ejapan_1"},"IsPMallStore":"false"},"IsAdult":"0","Deliveryinfo":{"Area":"","Deadline":"","Day":""},"_attributes":{"index":"2"}},"2":{"Name":"\u3010\u30ad\u30e3\u30c3\u30b7\u30e5\u30ec\u30b95\uff05\u9084\u5143\u3011\u3010\u9001\u6599\u7121\u6599\u3011\u2605\u307e\u3068\u3081\u8cb7\u3044\u2605\u3000\u30ab\u30eb\u30d3\u30fc\u3000\u304b\u3063\u3071\u3048\u3073\u305b\u3093\u7d00\u5dde\u306e\u6885\u547370g\u3000\u3000\u00d712\u500b\u3010\u30a4\u30fc\u30b8\u30e3\u30d1\u30f3\u30e2\u30fc\u30eb\u3011","Description":"\u3010\u691c\u7d22\u30ad\u30fc\u30ef\u30fc\u30c9\uff08\u5546\u54c1\u5185\u5bb9\u3092\u4fdd\u969c\u3059\u308b\u3082\u306e\u3067\u306f\u3042\u308a\u307e\u305b\u3093\uff09\u3011\u304a\u83d3\u5b50\u3000\u304a\u3084\u3064\u3000\u30b9\u30ca\u30c3\u30af\u3000\u30b9\u30ca\u30c3\u30af\u83d3\u5b50\u3000\u30ab\u30c3\u30d1\u30a8\u30d3\u30bb\u30f3\u3000\u3046\u3081\u5473\u3000\u30a6\u30e1\u3000\u6d77\u8001\u3000\u30ab\u30eb\u30b7\u30a6\u30e0","Headline":"","Url":"https:\/\/store.shopping.yahoo.co.jp\/ejapan\/012-4901330197711fm.html","ReleaseDate":"","Availability":"instock","Code":"ejapan_012-4901330197711fm","Condition":"new","Image":{"Id":"ejapan_012-4901330197711fm","Small":"https:\/\/item-shopping.c.yimg.jp\/i\/c\/ejapan_012-4901330197711fm","Medium":"https:\/\/item-shopping.c.yimg.jp\/i\/g\/ejapan_012-4901330197711fm"},"Review":{"Rate":"0.00","Count":"0","Url":"https:\/\/shopping.yahoo.co.jp\/review\/item\/list?store_id=ejapan&page_key=012-4901330197711fm"},"Affiliate":{"Rate":"1.0"},"Price":{"_attributes":{"currency":"JPY"},"_value":"2278"},"PremiumPrice":"","PriceLabel":{"_attributes":{"taxIncluded":"true"},"FixedPrice":"","DefaultPrice":"2278","SalePrice":"","PremiumPriceStatus":"0","PremiumPrice":"2278","PremiumDiscountType":"","PremiumDiscountRate":"","PeriodStart":"","PeriodEnd":""},"Point":{"Amount":"22","Times":"1","PremiumAmount":"22","PremiumTimes":"1"},"Shipping":{"Code":"1","Name":"\u8a2d\u5b9a\u7121\u3057"},"Category":{"Current":{"Id":"13451","Name":"\u305d\u306e\u4ed6\u30b9\u30ca\u30c3\u30af\u3001\u304a\u83d3\u5b50\u3001\u304a\u3064\u307e\u307f"}},"CategoryIdPath":{"0":{"Id":"1"},"1":{"Id":"2498"},"2":{"Id":"4745"},"3":{"Id":"13451"},"_container":"Category"},"Brands":{"Name":"\u30ab\u30eb\u30d3\u30fc","Path":{"0":{"Id":"1"},"1":{"Id":"16267"},"_container":"Brand"}},"JanCode":"4901330197711","Model":"","IsbnCode":"","Store":{"Id":"ejapan","Name":"e\u30b8\u30e3\u30d1\u30f3","Url":"https:\/\/store.shopping.yahoo.co.jp\/ejapan\/","Payment":{"0":{"Code":"1","Name":"\u30af\u30ec\u30b8\u30c3\u30c8\u30ab\u30fc\u30c9"},"1":{"Code":"16","Name":"Yahoo!\u30a6\u30a9\u30ec\u30c3\u30c8\u306b\u767b\u9332\u3057\u3066\u3044\u308b\u30af\u30ec\u30b8\u30c3\u30c8\u30ab\u30fc\u30c9"},"2":{"Code":"4","Name":"\u5546\u54c1\u4ee3\u5f15"},"3":{"Code":"2","Name":"\u9280\u884c\u632f\u8fbc"},"4":{"Code":"8","Name":"\u90f5\u4fbf\u632f\u66ff"},"5":{"Code":"1024","Name":"\u30bd\u30d5\u30c8\u30d0\u30f3\u30af\u307e\u3068\u3081\u3066\u652f\u6255\u3044"},"6":{"Code":"4096","Name":"PayPay"},"_container":"Method"},"IsBestStore":"false","Ratings":{"Rate":"4.5","Count":"11874","Total":"51099","DetailRate":"4.3"},"Image":{"Id":"ejapan_1","Medium":"https:\/\/item-shopping.c.yimg.jp\/s\/h\/ejapan_1"},"IsPMallStore":"false"},"IsAdult":"0","Deliveryinfo":{"Area":"","Deadline":"","Day":""},"_attributes":{"index":"3"}},"_container":"Hit"}}}}
So I would like to find the way to automatically parse this JSON body in order to exploit the data later,
Does the solution come from the encoding format ?
I have not been successful implementing decompressing using Gzip (link here)
Would it be possible to do it automatically via Decodable or the data is maybe directly accessible in Alomofire ?
Thank you in advance,
Charles
When it comes to decoding and holding data from JSON responses from REST APIs, I have used the following approach:
Include the bellow two PODS
1) Alamofire
2) TRON
Now, you'll have to create a class you'll use as a datasource interface between the API and the actual class model. In the following example, I'm pulling data from a movie database service and I have the bellow class for the data sourcing:
class MovieDataSource: JSONDecodable {
var movies: [Movie] = []
let cellid = "cellid"
required init(json: JSON) throws {
guard let moviesArray = json["results"].array else {
print("Error on the JSON Format")
return
}
self.movies = moviesArray.map({return Movie(json: $0)})
}
}
class JSONError: JSONDecodable {
required init(json: JSON) throws {
print("JSON error\n", json)
}
}
Then I have the actual data holding class/structure:
struct Movie {
let title: String
let movieId: Int
let overview: String
let releaseDate: String
let posterPathURL: String
let genres: [Int]
let vote_average: Float
let popularity: Float
let isForAdults: Bool
init(json: JSON) {
self.title = json["title"].stringValue
self.movieId = json["id"].intValue
self.overview = json["overview"].stringValue
self.releaseDate = json["release_date"].stringValue
self.posterPathURL = json["poster_path"].stringValue
self.genres = json["genre_ids"].arrayObject as! [Int]
self.vote_average = json["vote_average"].floatValue
self.popularity = json["popularity"].floatValue
self.isForAdults = json["adult"].boolValue
}
}
I think the above information should be enough to get you an idea on how to move forward, but If you need further guidance, I found this video series very educative on this subject: https://www.youtube.com/watch?v=tt67VikEovY&list=PL0dzCUj1L5JE1wErjzEyVqlvx92VN3DL5&index=13
Good luck!

Decode JSON single object or array of object dynamically [duplicate]

This question already has an answer here:
Swift 4 JSON Codable - value returned is sometimes an object, others an array
(1 answer)
Closed 3 years ago.
Let's say I have an array of JSON response from the GET method like :
[{
"id":"1",
"Name":"John Doe",
},{
"id":"2",
"Name":"Jane Doe",
}]
And from the POST method using id param I only have 1 object JSON response :
{
"id":"1",
"Name":"John Doe",
}
how can I write a method to decode both the JSON dynamically?
At the moment, this is what I'm using :
func convertJSON<T:Decodable>(result: Any?, model: T.Type) -> T? {
if let res = result {
do {
let data = try JSONSerialization.data(withJSONObject: res, options: JSONSerialization.WritingOptions.prettyPrinted)
return try JSONDecoder().decode(model, from: data)
} catch {
print(error)
return nil
}
} else {
return nil
}
}
The method can be used to decode a single object using dynamic model, but I just can't figure it out to handle a single object / an array of objects dynamically.
The most I can get with is just using a duplicate of the method but replacing T with
[T] in the method parameter and return type, if the response is an array.
I'm open to any suggestion, any help is appreciated, Thank You in advance.
Edit : If this question is duplicate of this , I'm not sure how the marked answer could be a solution.
One solution could be to always return [Model]?.
Inside your function first try to decode as Model, on success return an array with that single decoded object inside it. If this fails then try to decode as [Model], on success return the decoded object else return nil.
Using your sample JSONs I created a struct:
struct Person: Codable {
let id, name: String
enum CodingKeys: String, CodingKey {
case id
case name = "Name"
}
}
Then I created a struct with a couple of methods to decode from either a String or an optional Data.
struct Json2Type<T: Decodable> {
// From data to type T
static public func convertJson(_ data: Data?) -> [T]? {
// Check data is not nil
guard let data = data else { return nil }
let decoder = JSONDecoder()
// First try to decode as a single object
if let singleObject = try? decoder.decode(T.self, from: data) {
// On success return the single object inside an array
return [singleObject]
}
// Try to decode as multiple objects
guard let multipleObjects = try? decoder.decode([T].self, from: data) else { return nil }
return multipleObjects
}
// Another function to decode from String
static public func convertJson(_ string: String) -> [T]? {
return convertJson(string.data(using: .utf8))
}
}
Finally call the method you prefer:
Json2Type<Person>.convertJson(JsonAsDataOrString)
Update: #odin_123, a way to have either a Model or [Model] as return value can be accomplish using an enum. We can even add the error condition there to avoid returning optionals. Let's define the enum as:
enum SingleMulipleResult<T> {
case single(T)
case multiple([T])
case error
}
Then the struct changes to something like this:
struct Json2Type<T: Decodable> {
static public func convertJson(_ data: Data?) -> SingleMulipleResult<T> {
guard let data = data else { return .error }
let decoder = JSONDecoder()
if let singleObject = try? decoder.decode(T.self, from: data) {
return .single(singleObject)
}
guard let multipleObjects = try? decoder.decode([T].self, from: data) else { return .error }
return .multiple(multipleObjects)
}
static public func convertJson(_ string: String) -> SingleMulipleResult<T> {
return convertJson(string.data(using: .utf8))
}
}
You can call it the same way we did before:
let response = Json2Type<Person>.convertJson(JsonAsDataOrString)
And use a switch to check every possible response value:
switch(response) {
case .single(let object):
print("One value: \(object)")
case .multiple(let objects):
print("Multiple values: \(objects)")
case .error:
print("Error!!!!")
}

Combining multiple JSONs with id

I'm trying to fetch data from a public API. However, all the data I need is accessible only by calling multiple URLs.
However, each JSON provided have a station_id and I'm trying to combine the data based on this value.
I am not sure which strategy I should use to "merge" the results (see code below)
I tried calling both URL at the same time.
Also tried to add the data from the second URL after calling the first URL.
first URL (https://api-core.bixi.com/gbfs/es/station_information.json)
{"last_updated":1565466677,
"ttl":10,
"data":
{"stations":
[
{"station_id":"25",
"external_id":"0b100854-08f3-11e7-a1cb-3863bb33a4e4",
"name":"de la Commune / Place Jacques-Cartier",
"short_name":"6026",
"lat":45.50761009451047,
"lon":-73.55183601379395,
"capacity":89,}]
// ...
Second URL (https://api-core.bixi.com/gbfs/en/station_status.json)
{"last_updated":1565466677,
"ttl":10,
"data":
{"stations":
[
{"station_id":"25",
"num_bikes_available": 39,
"num_docks_available":50,}]
// ...
Excepted Result (This is the structure I am looking for, not the final code)
{"last_updated":1565466677,
"ttl":10,
"data":
{"stations":
[
{"station_id":"25",
"external_id":"0b100854-08f3-11e7-a1cb-3863bb33a4e4",
"name":"de la Commune / Place Jacques-Cartier",
"short_name":"6026",
"lat":45.50761009451047,
"lon":-73.55183601379395,
"capacity":89,
"num_bikes_available": 39,
"num_docks_available":50}]
//...
Structure I tried to pass the data in
struct BixiApiDataModel: Codable {
let last_updated: Int
let ttl: Int
let data: Stations
}
struct Stations: Codable {
let stations: [Station]
}
struct Station: Codable {
let station_id: String
let num_bikes_available: Int
let num_docks_available: Int
let external_id: String
let name: String
let short_name: String
let lat: Float
let lon: Float
let capacity: Int
}
Calling the URL
class Webservice {
func loadBixiApiDataModel(url: URL, completion: #escaping ([Station]?) -> ()) {
URLSession.shared.dataTask(with: url) { data, response, error in
guard let data = data, error == nil else {
completion(nil)
return
}
let response = try? JSONDecoder().decode(BixiApiDataModel.self, from: data)
if let response = response {
DispatchQueue.main.async {
completion(response.data.stations)
}
}
}.resume()
}
}
I'm trying to display the combined information of a station. I assume the data I fetch after calling the first URL isn't stored when I call the second URL.
Should I call both APIs separately, store the data and then combine everything using the station_id value?
Or is it possible to call each APIs and append the data from the second URL based on the station_id?
Thanks in advance for your help.
I would do it like this
Handle each download separately
Keep the resulting data in separate structs
Merge them into a third struct and then use that third struct internally in the app
Handle each download separately
Download the station information first and store it in a dictionary with station_id as key and then download station status and use the same id to match the downloaded elements
Keep the resulting data in separate structs
Since the content of the downloaded data is quite different between the to API calls I would use two different structs for them, StationInformation and StationStatus. Looking at the type of data you might actually want to download status more often than information which seems to be more static so that is another reason to keep them separate.
Merge them into a third struct...
I would create a third struct that contains information from the two other structs, either as just two properties (shown below) or with properties that are extracted from the others
Here is an example of how the third struct could be implemented
struct Station {
let information: StationInformation
var status: StationStatus?
init(information: StationInformation) {
self.information = information
}
var id: String {
return information.stationId
}
mutating func merge(status: StationStatus) {
guard self.id == status.stationId else { return }
self.status = status
}
}
The download function could be modified to be generic to simplify the code. First the structs need to be modified
struct BixiApiDataModel<T: Decodable>: Decodable {
let data: Stations<T>
}
struct Stations<T: Decodable>: Decodable {
let stations: [T]
}
struct StationInformation: Codable {
let stationId: String
let externalId: String
//... rest of properties
}
struct StationStatus: Codable {
let stationId: String
let numBikesAvailable: Int
let numDocksAvailable: Int
}
then the function signature needs to be changed to
func loadBixiApiDataModel<T: Decodable>(url: URL, completion: #escaping ([T]?) -> ()) {
and the decoding needs to be changed (notice the improved error handling, never use try?)
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
do {
let response = try decoder.decode(BixiApiDataModel<T>.self, from: data)
completion(response.data.stations)
} catch {
print(error)
}
And a simplified example of calling the function (but without any merging)
var informationArray: [StationInformation] = []
var statusArray: [StationStatus] = []
if let url = URL(string: "https://api-core.bixi.com/gbfs/es/station_information.json") {
loadBixiApiDataModel(url: url, completion: {(arr: [StationInformation]?) in
if let arr = arr { informationArray = arr }
print(informationArray.count)
})
} else { print("Not a valid url")}
if let url = URL(string: "https://api-core.bixi.com/gbfs/en/station_status.json") {
loadBixiApiDataModel(url: url, completion: {(arr: [StationStatus]?) in
if let arr = arr { statusArray = arr }
print(statusArray.count)
})
} else { print("Not a valid url")}

How can i append JSON with multiple struct into array?

In order to parse the JSON, I needed to use 3 structs.
struct AppleApi: Decodable {
let feed: Feed
}
struct Feed: Decodable {
let results: [Result]
}
struct Result: Decodable {
let artistName: String
let artWorkUrl: String
enum CodingKeys : String, CodingKey {
case artistName = "artistName"
case artWorkUrl = "artworkUrl100"
}
}
But when I try to populate the array with that parsed data I got that message:
Cannot convert value of type '[Result]' to expected argument type
'AppleApi'
This is my error message:
do {
let appData = try JSONDecoder().decode(AppleApi.self, from: jsonData)
print(appData.feed.results.count)
var dataApp = appData.feed.results
print(appData)
DispatchQueue.main.async {
self.feedReseult.append(dataApp)
self.myCollectionView.reloadData()
}
} catch let err {
print("Error",err)
}
And this is my array:
var feedReseult = [AppleApi]()
I probably need to reach to 3. struct to reach the array inside JSON in order to have same type of argument type. How can I do that?
Your declaration of feedReseult should be this,
var feedReseult = [Result]()
and append the dataApp as below,
DispatchQueue.main.async {
self.feedReseult.append(contentsOf: dataApp)
self.myCollectionView.reloadData()
}
Also i feel a typo, feedResult instead of feedReseult
It looks like your struct from Json is not put together correct, you should just need one struct per one JSON load. Could you give JSON sample please?
If you want to Decode Feed which is part of AppleAPI then you should create an object that is of type AppleApi.Feed and put results into that.
Hope this helps a little

Use generic argument in method body <T>

I'm trying to create a generic method like the following:
private func map<T>(type:T, jsonString:String) -> T
{
do
{
let model = try Mapper<type>().map(JSONString: jsonString)!
return model
}
catch
{
Log.error("Failed to convert JSON jsonString to model object: \(jsonString)")
}
return EmptyModel()
}
but it result in compile error:
Error: use of undeclared type 'type'
How can I change it to use the specified type (a class object) with the Mapper's generic value?
You can use T instead of type:
let model = try Mapper<T>().map(JSONString: jsonString)!
You might want to change method signature, so it returns an instance of T and not the type T itself:
private func map<T>(type: T.Type, jsonString: String) -> T
That being said, Swift already has its JSONDecoder. It might already support what you are trying to implement.
let decoder = JSONDecoder()
let model = try decoder.decode(Model.self, from: data)