Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
4.3 kB
7
Indexable
import Foundation
import _Concurrency
import PlaygroundSupport

PlaygroundPage.current.needsIndefiniteExecution = true

struct ResponseData: Decodable {
    let data: DataWrapper
}

struct DataWrapper: Decodable {
    let artist: Artist
}

struct Artist: Decodable {
    let videoStories: VideoStories
}

struct VideoStories: Decodable {
    let edges: [Edge]
}

struct Edge: Decodable {
    let node: VideoStory
}

enum FireFlyError: Error {
        case invalidFireflyRequestUrl
        case fireflyNetworkError
        case malformedFireflyRequestBody
    }

struct VideoStory: Decodable {
    let id: String
    let name: String
    let playbackId: String
    let videoType: String
    let duration: Int
    let images: [Image]
}

struct Image: Decodable {
    let url: String
    let imageType: String?
}
func test (stringval: String) throws -> String {
    if stringval == "Hi" {
        throw FireFlyError.fireflyNetworkError
    }
    return "lol"
}

func dummyrequest() async {
    let url = URL(string: "https://gql.music.amazon.dev/")!

    // Create a URLRequest for the GET request
    let request = URLRequest(url: url)
    let startTime = Date()
    do {
        let (data, _) = try await URLSession.shared.data(for: request)
        let elapsedTime = Date().timeIntervalSince(startTime)
        print("Connection elapsed time \(elapsedTime)")
            // Ignore the data
        print("GET request completed successfully")
    } catch {
            // Handle errors if needed
            print("Error: \(error)")
        }
}

func requests () async {
var headers = [
                "x-api-key": "amzn1.application.0fc8ceb4b7724acdb9b6695cf88b04b6",
                "Authorization": """
Bearer Atza|IwEBIBrzL8S6OqKXQx6RV3-qaXsBuqUqpTxWvCEXnqbYdXT3L_waTs3iw9smd3_aDON3lSK3iEBNEgmjq4h0vhyd25nfZ-gd1cGYYLvL5VFch650l2M-h48c7HPH4pvaSmeAci8FevlerZ91SRl3dtnC5ibidCSeNyahzno80GbNtn240qeNNIiR54DUWO0U2M92mtgKX6RerpuvZkcj78zCl2ju9YgIHluuqQ9GSCTbMB2dnku8r3-7ccqdmK7Nu-KjsNLblL7-SuP3-DU26VwsaiSPJw7m1MDDyGzeAC7pKYEgdPGzx6ilsBl2wWvrwy83DW4sD54N7BK_sgIBPk0xXb7ffE5P2PU1Ko8kzNcJxyyFLcvCvO_sNm_fDw71e9dEX2iOAaLlFCh9zUvP_uQumHnlcXR_7GfXWekykFVVhFUabw
""",
                "Content-Type": "application/json",
//                "x-amzn-feature-flags": "trace"
            ]
let artistAsin = "B0011XQAXQ" 
let gqlQuery = """
    query {
        artist(id:"\(artistAsin)"){
            videoStories {
                edges {
                    node {
                        id,
                        name,
                        playbackId,
                        videoType,
                        duration,
                        images {
                            url
                            imageType
                        }
                    }
                }
            }
        }
    }
    """

let jsonObjectArray = ["query": gqlQuery] as [String : Any]
var postData: Data
       
   do {
    postData = try JSONSerialization.data(withJSONObject: jsonObjectArray)
    let url = URL(string: "https://gql.music.amazon.dev/")
    var request = URLRequest(url: url!, cachePolicy: .returnCacheDataElseLoad, timeoutInterval: 10.0)
    let urlCache = URLCache.shared

    request.httpBody = postData as Data
    request.httpMethod = "POST"
    request.allHTTPHeaderFields = headers

    do {
        let startTime = Date()
        let (responseData, httpResponse) = try await URLSession.shared.data(for: request)

        let elapsedTime = Date().timeIntervalSince(startTime)
        print("Firefly Elapsed Time: \(elapsedTime) seconds")
        let decoder = JSONDecoder()
    
        let response = try decoder.decode(ResponseData.self, from: responseData)
        
        let videoStories = response.data.artist.videoStories.edges
            for edge in videoStories {
                let videoStory = edge.node
//                print(videoStory.images[0].url)
        }
    } catch let error as DecodingError {
        print("Error parsing the GQL response : \(error)")
    }
   } catch {
       print("postdataloss")
}
}

async{
    await dummyrequest()
    await requests()
        await requests()
            await requests()
}

//do {
//    try test(stringval: "Hi")
//} catch {
//    print("\(error)")
//}