Home > OS >  What is the best way to make this API request in Swift?
What is the best way to make this API request in Swift?

Time:05-24

Sorry if this question is too repetitive here but I cant figured out how to made it :(

I need to send data to an API (https://www.opticutter.com/public/doc/api#introduction) my data comes from somes TextFields and this is what the API expect.

curl -H "Content-type: application/json" \
-H "Authorization: Bearer <TOKEN>" \
-X POST -d '{
    "stocks" : [{
        "length" : "60",
        "width" : "40",
        "count": "10",
        "grainDirection": null
    },{
        "length" : "40",
        "width" : "35",
        "grainDirection": null
    }],
    "requirements" : [{
        "length" : "30", 
        "width" : "20", 
        "count": "8",
        "grainDirection": null
    },
    {
        "width" : "20", 
        "length" : "20", 
        "count": "3",
        "grainDirection": null
    }],
    "settings" : {
    "kerf": "0"
    }
}' 

So I already have the code to make the request but I cant realize how convert the data into that.

Hope do you understand what I need. Thanks in advance!

CodePudding user response:

The best approach in my opinion is to map your data into structs/classes and use the Decodable protocol.

struct Stock: Encodable {
    let length: Int
    let width: Int
    let count: Int
    let grainDirection: Int?
}

struct Requirement: Encodable {
    let length: Int
    let width: Int
    let count: Int
    let grainDirection: Int?
}

struct Setting: Encodable {
    let kerf: Int
}

struct MyObject: Encodable {
    let stocks: [Stock]
    let requirements: [Requirement]
    let settings: [Setting]
}

Then you build the MyObject with the data from the textfields:

let myObjectData = MyObject(stocks: [
                                Stock(length: 60, width: 40, count: 10, grainDirection: nil),
                                Stock(length: 40, width: 35, count: 0, grainDirection: nil)
                            ],
                            requirements: [
                                Requirement(length: 30, width: 20, count: 8, grainDirection: nil),
                                Requirement(length: 20, width: 20, count: 3, grainDirection: nil),
                            ],
                            settings: [Setting(kerf: 0)])

Finally on the request you would have to encode the data, converting it into a JSON String for example:

do {
    let jsonData = try JSONEncoder().encode(myObjectData)
    let jsonString = String(data: jsonData, encoding: .utf8)
    print(jsonString)
} catch {
    debugPrint(error)
}

This will produce a json string with the following result:

{
   "stocks":[
      {
         "width":40,
         "count":10,
         "length":60
      },
      {
         "width":35,
         "count":0,
         "length":40
      }
   ],
   "requirements":[
      {
         "width":20,
         "count":8,
         "length":30
      },
      {
         "width":20,
         "count":3,
         "length":20
      }
   ],
   "settings":[
      {
         "kerf":0
      }
   ]
}
  • Related