Swift
Swift
Lightspeed - Create a Product
See more Lightspeed Examples
Create a new product based on the given parameters.Chilkat Swift Downloads
func chilkatTest() {
var success: Bool = false
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
let http = CkoHttp()!
// Implements the following CURL command:
// curl -u API_KEY:API_SECRET \
// -H "Content-Type: application/json" \
// -X POST \
// -d '{
// "product": {
// "visibility": "visible",
// "data01": "",
// "data02": "",
// "data03": "",
// "title": "Lookin Sharp T-Shirt",
// "fulltitle": "Lookin Sharp T-Shirt",
// "description": "Description of the Lookin Sharp T-Shirt",
// "content": "Long Description of the Lookin Sharp T-Shirt",
// "deliverydate": 6488,
// "supplier": 78794,
// "brand": 1171202
// }
// }' \
// "https://api.webshopapp.com/en/products.json"
// Use the following online tool to generate HTTP code from a CURL command
// Convert a cURL Command to HTTP Source Code
http.login = "API_KEY"
http.password = "API_SECRET"
// Use this online tool to generate code from sample JSON:
// Generate Code to Create JSON
// The following JSON is sent in the request body.
// {
// "product": {
// "visibility": "visible",
// "data01": "",
// "data02": "",
// "data03": "",
// "title": "Lookin Sharp T-Shirt",
// "fulltitle": "Lookin Sharp T-Shirt",
// "description": "Description of the Lookin Sharp T-Shirt",
// "content": "Long Description of the Lookin Sharp T-Shirt",
// "deliverydate": 6488,
// "supplier": 78794,
// "brand": 1171202
// }
// }
let json = CkoJsonObject()!
json.updateString(jsonPath: "product.visibility", value: "visible")
json.updateString(jsonPath: "product.data01", value: "")
json.updateString(jsonPath: "product.data02", value: "")
json.updateString(jsonPath: "product.data03", value: "")
json.updateString(jsonPath: "product.title", value: "Lookin Sharp T-Shirt")
json.updateString(jsonPath: "product.fulltitle", value: "Lookin Sharp T-Shirt")
json.updateString(jsonPath: "product.description", value: "Description of the Lookin Sharp T-Shirt")
json.updateString(jsonPath: "product.content", value: "Long Description of the Lookin Sharp T-Shirt")
json.updateInt(jsonPath: "product.deliverydate", value: 6488)
json.updateInt(jsonPath: "product.supplier", value: 78794)
json.updateInt(jsonPath: "product.brand", value: 1171202)
http.setRequestHeader(name: "Content-Type", value: "application/json")
// Use the correct cluster for your shop. Here are the choices:
// eu1 https://api.webshopapp.com/en/
// us1 https://api.shoplightspeed.com/en/
let resp = CkoHttpResponse()!
success = http.httpJson(verb: "POST", url: "https://api.webshopapp.com/en/products.json", json: json, contentType: "application/json", response: resp)
if success == false {
print("\(http.lastErrorText!)")
return
}
let sbResponseBody = CkoStringBuilder()!
resp.getBodySb(sb: sbResponseBody)
let jResp = CkoJsonObject()!
jResp.loadSb(sb: sbResponseBody)
jResp.emitCompact = false
print("Response Body:")
print("\(jResp.emit()!)")
var respStatusCode: Int = resp.statusCode.intValue
print("Response Status Code = \(respStatusCode)")
if respStatusCode >= 400 {
print("Response Header:")
print("\(resp.header!)")
print("Failed.")
return
}
// Sample JSON response:
// (Sample code for parsing the JSON response is shown below)
// {
// "product": {
// "id": 20974460,
// "createdAt": "2019-05-28T20:14:26+00:00",
// "updatedAt": "2019-05-28T20:14:26+00:00",
// "isVisible": true,
// "visibility": "visible",
// "hasMatrix": false,
// "data01": "",
// "data02": "",
// "data03": "",
// "url": "lookin-sharp-t-shirt",
// "title": "Lookin' Sharp T-Shirt",
// "fulltitle": "Lookin' Sharp T-Shirt",
// "description": "Description of the Lookin' Sharp T-Shirt",
// "content": "<p>Long Description of the Lookin' Sharp T-Shirt</p>",
// "set": false,
// "brand": {
// "resource": {
// "id": 1171202,
// "url": "brands/1171202",
// "link": "https://api.shoplightspeed.com/us/brands/1171202.json"
// }
// },
// "categories": {
// "resource": {
// "id": false,
// "url": "categories/products?product=20974460",
// "link": "https://api.shoplightspeed.com/us/categories/products.json?product=20974460"
// }
// },
// "deliverydate": {
// "resource": {
// "id": 6488,
// "url": "deliverydates/6488",
// "link": "https://api.shoplightspeed.com/us/deliverydates/6488.json"
// }
// },
// "image": false,
// "images": false,
// "relations": {
// "resource": {
// "id": false,
// "url": "products/20974460/relations",
// "link": "https://api.shoplightspeed.com/us/products/20974460/relations.json"
// }
// },
// "metafields": {
// "resource": {
// "id": false,
// "url": "products/20974460/metafields",
// "link": "https://api.shoplightspeed.com/us/products/20974460/metafields.json"
// }
// },
// "reviews": {
// "resource": {
// "id": false,
// "url": "reviews?product=20974460",
// "link": "https://api.shoplightspeed.com/us/reviews.json?product=20974460"
// }
// },
// "type": false,
// "attributes": {
// "resource": {
// "id": false,
// "url": "products/20974460/attributes",
// "link": "https://api.shoplightspeed.com/us/products/20974460/attributes.json"
// }
// },
// "supplier": {
// "resource": {
// "id": 78794,
// "url": "suppliers/78794",
// "link": "https://api.shoplightspeed.com/us/suppliers/78794.json"
// }
// },
// "tags": {
// "resource": {
// "id": false,
// "url": "tags/products?product=20974460",
// "link": "https://api.shoplightspeed.com/us/tags/products.json?product=20974460"
// }
// },
// "variants": {
// "resource": {
// "id": false,
// "url": "variants?product=20974460",
// "link": "https://api.shoplightspeed.com/us/variants.json?product=20974460"
// }
// },
// "movements": {
// "resource": {
// "id": false,
// "url": "variants/movements?product=20974460",
// "link": "https://api.shoplightspeed.com/us/variants/movements.json?product=20974460"
// }
// }
// }
// }
// Sample code for parsing the JSON response...
// Use the following online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON
var productId: Int = jResp.int(of: "product.id").intValue
var productCreatedAt: String? = jResp.string(of: "product.createdAt")
var productUpdatedAt: String? = jResp.string(of: "product.updatedAt")
var productIsVisible: Bool = jResp.bool(of: "product.isVisible")
var productVisibility: String? = jResp.string(of: "product.visibility")
var productHasMatrix: Bool = jResp.bool(of: "product.hasMatrix")
var productData01: String? = jResp.string(of: "product.data01")
var productData02: String? = jResp.string(of: "product.data02")
var productData03: String? = jResp.string(of: "product.data03")
var productUrl: String? = jResp.string(of: "product.url")
var productTitle: String? = jResp.string(of: "product.title")
var productFulltitle: String? = jResp.string(of: "product.fulltitle")
var productDescription: String? = jResp.string(of: "product.description")
var productContent: String? = jResp.string(of: "product.content")
var productSet: Bool = jResp.bool(of: "product.set")
var productBrandResourceId: Int = jResp.int(of: "product.brand.resource.id").intValue
var productBrandResourceUrl: String? = jResp.string(of: "product.brand.resource.url")
var productBrandResourceLink: String? = jResp.string(of: "product.brand.resource.link")
var productCategoriesResourceId: Bool = jResp.bool(of: "product.categories.resource.id")
var productCategoriesResourceUrl: String? = jResp.string(of: "product.categories.resource.url")
var productCategoriesResourceLink: String? = jResp.string(of: "product.categories.resource.link")
var productDeliverydateResourceId: Int = jResp.int(of: "product.deliverydate.resource.id").intValue
var productDeliverydateResourceUrl: String? = jResp.string(of: "product.deliverydate.resource.url")
var productDeliverydateResourceLink: String? = jResp.string(of: "product.deliverydate.resource.link")
var productImage: Bool = jResp.bool(of: "product.image")
var productImages: Bool = jResp.bool(of: "product.images")
var productRelationsResourceId: Bool = jResp.bool(of: "product.relations.resource.id")
var productRelationsResourceUrl: String? = jResp.string(of: "product.relations.resource.url")
var productRelationsResourceLink: String? = jResp.string(of: "product.relations.resource.link")
var productMetafieldsResourceId: Bool = jResp.bool(of: "product.metafields.resource.id")
var productMetafieldsResourceUrl: String? = jResp.string(of: "product.metafields.resource.url")
var productMetafieldsResourceLink: String? = jResp.string(of: "product.metafields.resource.link")
var productReviewsResourceId: Bool = jResp.bool(of: "product.reviews.resource.id")
var productReviewsResourceUrl: String? = jResp.string(of: "product.reviews.resource.url")
var productReviewsResourceLink: String? = jResp.string(of: "product.reviews.resource.link")
var productType: Bool = jResp.bool(of: "product.type")
var productAttributesResourceId: Bool = jResp.bool(of: "product.attributes.resource.id")
var productAttributesResourceUrl: String? = jResp.string(of: "product.attributes.resource.url")
var productAttributesResourceLink: String? = jResp.string(of: "product.attributes.resource.link")
var productSupplierResourceId: Int = jResp.int(of: "product.supplier.resource.id").intValue
var productSupplierResourceUrl: String? = jResp.string(of: "product.supplier.resource.url")
var productSupplierResourceLink: String? = jResp.string(of: "product.supplier.resource.link")
var productTagsResourceId: Bool = jResp.bool(of: "product.tags.resource.id")
var productTagsResourceUrl: String? = jResp.string(of: "product.tags.resource.url")
var productTagsResourceLink: String? = jResp.string(of: "product.tags.resource.link")
var productVariantsResourceId: Bool = jResp.bool(of: "product.variants.resource.id")
var productVariantsResourceUrl: String? = jResp.string(of: "product.variants.resource.url")
var productVariantsResourceLink: String? = jResp.string(of: "product.variants.resource.link")
var productMovementsResourceId: Bool = jResp.bool(of: "product.movements.resource.id")
var productMovementsResourceUrl: String? = jResp.string(of: "product.movements.resource.url")
var productMovementsResourceLink: String? = jResp.string(of: "product.movements.resource.link")
}