Home > Back-end >  API Fetch returns nil
API Fetch returns nil

Time:12-28

The fetch doesn't throw any error, it just returns nil, I don't know why, this is the api URL:

https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd&include_market_cap=true&include_24hr_vol=true&include_24hr_change=true&include_last_updated_at=true

Note: There is an infinite amount of tokens, not only Bitcoin, so I can't hardcode it.

it looks like:

{
    "bitcoin": {
        "usd": 51110,
        "usd_market_cap": 964544028181.903,
        "usd_24h_vol": 19080677597.123028,
        "usd_24h_change": 2.163373654971005,
        "last_updated_at": 1640575480
    },
    thereCanBeMoreWhichIcan'tPredict: {
        ...
    }
}

This is the model I created:

struct TokenSimplePriceCoingeckoUsdModel: Codable, Hashable {
    let usd, usd_market_cap, usd_24h_vol, usd_24h_change: Double?
    let last_updated_at: Int?
}

and this is how I'm fetching it with Alamofire:

func afRequest(url: URL) async throws -> Data {
    try await withUnsafeThrowingContinuation { continuation in
        // If another error try lowering the request error: (statusCode: 200..<300)
        // timeoutInterval is in seconds
        AF.request(url, method: .get, requestModifier: { $0.timeoutInterval = .infinity }).validate(statusCode: 200..<600).responseData { response in
            
            if let data = response.data {
                continuation.resume(returning: data)
                return
            }
            
            if case let .failure(error) = response.result {
                print("           
  • Related