How can I download multiple files from an API quickly using HTTPResquest or is there a better way?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Anon

This is the code for my college, I am using to access youtube api and output title and thumbnail in godot using itemlist node. HTTPResquest downloading take some time before it fetches next request as a result itemlist displays nothing until the ongoing download is completed so put an “await” to next fetch next link after one second interval and this giving me required result with 1 sec delay. Is there anyway to show all titles and thumbnail at same time like it happens on youtube website, if it takes 2-3secs to load everything that’s fine too. Please help me.

main.gd

var I_APIFetch = C_APIFetch.new()
var U_APIRequest = "https://youtube.googleapis.com/youtube/v3/videos?part=snippet&id"
func F_DisplayRes():
for i in range(0, db.query_result.size()):
	I_APIFetch.F_FetchTitle(db.query_result[i]["Source"], U_APIRequest, U_APIKey)
	await get_tree().create_timer(1).timeout
	U_SearchRes.add_item(I_APIFetch.API_Title, I_APIFetch.U_Thumbnail, true)

C_APIFetch class

extends Control
class_name C_APIFetch

var U_Thumbnail
var API_Title = "Blank"
var API_Thumbnail
var API_YT = HTTPRequest.new()
var http_request = HTTPRequest.new()
var J_File

func F_FetchTitle(P_Link, P_APIRequest, P_APIKey):
    var P_FetchLink = P_APIRequest + P_Link + P_APIKey
    print(P_FetchLink)
    API_YT.request_completed.connect(self._on_HTTPRequest_request_completed)
    API_YT.request(P_FetchLink)
    print("2")


func _on_HTTPRequest_request_completed(_result, _response_code, _headers, body):
    J_File = JSON.parse_string(body.get_string_from_utf8())
    print("3")
    print(J_File.items[0].snippet.title)
    API_Title = J_File.items[0].snippet.title
    API_Thumbnail = J_File.items[0].snippet.thumbnails.medium.url
    F_FetchThumbnail(API_Thumbnail)


 func F_FetchThumbnail(P_Link):
    http_request.request_completed.connect(self._http_request_completed)
    http_request.request(P_Link)


 func _http_request_completed(_result, _response_code, _headers, body):
    var image = Image.new()
    image.load_jpg_from_buffer(body)
    U_Thumbnail = ImageTexture.create_from_image(image)

Thank You

Could you create multiple instances of your HttpClient, and download them asynchronously?

SteveSmith | 2023-06-27 20:09