Created
January 12, 2022 21:35
-
-
Save daneden/84fed951e7936462424ce3a65fe96f36 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"data": [ | |
{ | |
"id": "123456789", | |
"name": "Person A", | |
"username": "PersonA" | |
}, | |
{ | |
"id": "987654321", | |
"name": "Person B", | |
"username": "PersonB" | |
}, | |
], | |
"includes": { | |
"tweets": [ | |
{ | |
"id": "12345678", | |
"text": "Example text" | |
} | |
] | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"data": { | |
"id": "123456789", | |
"name": "Person A", | |
"username": "PersonA" | |
}, | |
"includes": { | |
"tweets": [ | |
{ | |
"id": "12345678", | |
"text": "Example text" | |
} | |
] | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct User: Codable, Identifiable { | |
typealias ID = String | |
let id: ID | |
let name: String | |
let username: String | |
// Assume that items can be optionally set in getUser | |
var items: UserItems? | |
} | |
struct UserItems: Codable { | |
let tweets: [Tweet] | |
} | |
struct ManyUsers: Codable { | |
let data: [User] | |
var items: UserItems? | |
} | |
protocol APIProvider { | |
// Option A | |
// Tuple type for cases with many users | |
func getUser(id: User.ID) async -> User | |
func getUsers(ids: [User.ID]) async -> ([User], UserItems?) | |
// Option B | |
// A separate struct type for many users | |
// func getUser... (as above) | |
func getUsers(ids: [User.ID]) async -> ManyUsers | |
// Option C | |
// Just ignore expanded items | |
// func getUser... (as above) | |
func getUsers(ids: [User.ID]) async -> [User] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
API nerds, I could use some help! The Twitter v2 API lets you optionally expand fields on users (namely pinned tweets). These expansions end up in the
includes
field on the returned JSON.It’s easy enough to set those items on a User struct when calling the API for a single user, but when calling multiple users, it’s hard to keep a decent mapping between each user in the
data
field and their respective tweets in theincludes
field.