I am now trying to create a simple Twitter client using SwiftUI, and currently at the point where I try to fetch my timeline.
Reading the official Twitter API v2 document, there is a way to fetch timeline, but it only offers tweet ID, text, created_at, and author Id.
so I tried to fetch author info using the author Id in loop, which causes too many fetching data and many of them are redundant because a single user tweet many times.
This way makes my account easily reach the api limit, but I am struggling to find better ways.
I am not an enginner and write codes just for fun, so I am not really familiar with techniques enginners should know... I would appreciate if you give me even a hint.
Here is my code below.
func getStream(client: TwitterAPIClient, id: String) async throws {
let response = await client.v2.timeline.getUserReverseChronological(.init(
id: id,
expansions: TwitterTweetExpansionsV2.all, maxResults: 20,
mediaFields: TwitterMediaFieldsV2.all,
tweetFields: TwitterTweetFieldsV2.all.subtracting([.promotedMetrics, .nonPublicMetrics, .organicMetrics]),
userFields: TwitterUserFieldsV2.all)
)
.responseDecodable(type: TwitterDataResponseV2<
[TwitterTimelineV2],
TwitterTimelineV2.Include,
TwitterTimelineV2.Meta>.self)
if let error = response.error {
print(error)
throw error
} else {
print(response.success!)
}
print(response)
let timeline = try response.map { $0.data }.result.get()
for tweet in timeline {
let response = try await getUserInfo(client: client, id: tweet.authorId).result.get()
print(response)
}
}
Note: TwitterAPIClinet is a client of a third party library.