Wednesday, February 8, 2023
HomeiOS DevelopmentWhat am I doing flawed to name this API?

What am I doing flawed to name this API?


import Basis
import UIKit

struct ID: Codable{
var id: String

enum CodingKeys: String, CodingKey{
    case id = " "
}

}

struct AnimalKeys: Codable{
var names: String?
var taxonomy: Taxonomy
var areas: String?
var traits: String?

enum CodingKeys: String, CodingKey{
    case names = "identify"
    case taxonomy = "taxonomy"
    case areas = "areas"
    case traits = "traits"
}

}

struct Taxonomy: Codable{
var kingdom: String?
var phylum: String?
var class1: String?
var household: String?
var scientificname: String?

enum CodingKeys: String, CodingKey{
    case kingdom = "kingdom"
    case phylum = "phylum"
    case class1 = "class"
    case household = "household"
    case scientificname = "scientific_name"
}

}

class AnimalModel {

static let share = AnimalModel()

var id: [ID] = []

var animal: [AnimalKeys] = []

var taxonomy: [Taxonomy] = []



init(identify: String){
    getAnimalApi(names: identify)
}

personal init(){
    
}

func getAnimalApi(names: String) {
    
    let headers = [
        "X-RapidAPI-Key": "e81434a738mshf53d97dda2d961ep12e8bejsnc53f1a032c78",
        "X-RapidAPI-Host": "animals-by-api-ninjas.p.rapidapi.com"
    ]
    
    let request = NSMutableURLRequest(url: NSURL(string: "https://animals-by-api-ninjas.p.rapidapi.com/v1/animals?identify=(names)")! as URL,
                                      cachePolicy: .useProtocolCachePolicy,
                                      timeoutInterval: 10.0)
    
    request.httpMethod = "GET"
    request.allHTTPHeaderFields = headers
    
    let session = URLSession.shared
    let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (knowledge, response, error) -> Void in
        if (error != nil) {
            print(error)
        } else {
            let httpResponse = response as? HTTPURLResponse
            print(httpResponse)
            print("good")
            var consequence: AnimalKeys?
            request.httpBody = knowledge
            guard let knowledge = knowledge else{return}

            do {
                consequence = attempt JSONDecoder().decode(AnimalKeys.self, from: knowledge)
                print(consequence?.names)

            } catch {
                print("The file couldn't be loaded")
            }
        }
    })
    dataTask.resume()
}

}

That is my code and it states that the file may very well be loaded

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments