Chilkat Online Tools

Swift / Datadog API Collection / Trigger Synthetic tests

Back to Collection Items

func chilkatTest() {
    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    let http = CkoHttp()
    var success: Bool

    // Use this online tool to generate code from sample JSON: Generate Code to Create JSON

    // The following JSON is sent in the request body.

    // {
    //   "tests": [
    //     {
    //       "public_id": "aaa-aaa-aaa",
    //       "metadata": {
    //         "ci": {
    //           "pipeline": {
    //             "url": "aliquip in ex do"
    //           },
    //           "provider": {
    //             "name": "nostrud culpa"
    //           }
    //         },
    //         "git": {
    //           "branch": "esse",
    //           "commitSha": "occaecat incididunt aute exercitation"
    //         }
    //       }
    //     },
    //     {
    //       "public_id": "aaa-aaa-aaa",
    //       "metadata": {
    //         "ci": {
    //           "pipeline": {
    //             "url": "pariatur ullamco sed"
    //           },
    //           "provider": {
    //             "name": "adipisicing exercitation laborum non"
    //           }
    //         },
    //         "git": {
    //           "branch": "tempor fugiat consequat Lorem",
    //           "commitSha": "esse aliqua"
    //         }
    //       }
    //     }
    //   ]
    // }

    let json = CkoJsonObject()
    json.UpdateString("tests[0].public_id", value: "aaa-aaa-aaa")
    json.UpdateString("tests[0].metadata.ci.pipeline.url", value: "aliquip in ex do")
    json.UpdateString("tests[0].metadata.ci.provider.name", value: "nostrud culpa")
    json.UpdateString("tests[0].metadata.git.branch", value: "esse")
    json.UpdateString("tests[0].metadata.git.commitSha", value: "occaecat incididunt aute exercitation")
    json.UpdateString("tests[1].public_id", value: "aaa-aaa-aaa")
    json.UpdateString("tests[1].metadata.ci.pipeline.url", value: "pariatur ullamco sed")
    json.UpdateString("tests[1].metadata.ci.provider.name", value: "adipisicing exercitation laborum non")
    json.UpdateString("tests[1].metadata.git.branch", value: "tempor fugiat consequat Lorem")
    json.UpdateString("tests[1].metadata.git.commitSha", value: "esse aliqua")

    http.SetRequestHeader("Content-Type", value: "application/json")
    http.SetRequestHeader("Accept", value: "application/json")

    var resp: CkoHttpResponse? = http.PostJson3("https://api.app.ddog-gov.com/api/v1/synthetics/tests/trigger", contentType: "application/json", json: json)
    if http.LastMethodSuccess == false {
        print("\(http.LastErrorText)")
        return
    }

    let sbResponseBody = CkoStringBuilder()
    resp!.GetBodySb(sbResponseBody)

    let jResp = CkoJsonObject()
    jResp.LoadSb(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.")
        resp = nil
        return
    }

    resp = nil

    // Sample JSON response:
    // (Sample code for parsing the JSON response is shown below)

    // {
    //   "batch_id": "anim quis",
    //   "locations": [
    //     {
    //       "id": -79104764,
    //       "name": "laborum"
    //     },
    //     {
    //       "id": -12473023,
    //       "name": "ullamco eu"
    //     }
    //   ],
    //   "results": [
    //     {
    //       "device": "laptop_large",
    //       "location": -53475818,
    //       "public_id": "Excepteur et",
    //       "result_id": "in"
    //     },
    //     {
    //       "device": "laptop_large",
    //       "location": 59458053,
    //       "public_id": "ut labore",
    //       "result_id": "quis tempor cillum est eu"
    //     }
    //   ],
    //   "triggered_check_ids": [
    //     "in irure culpa",
    //     "sit ad do Ut esse"
    //   ]
    // }

    // Sample code for parsing the JSON response...
    // Use this online tool to generate parsing code from sample JSON: Generate JSON Parsing Code

    var id: Int
    var name: String?
    var device: String?
    var location: Int
    var public_id: String?
    var result_id: String?
    var strVal: String?

    var batch_id: String? = jResp.StringOf("batch_id")
    var i: Int = 0
    var count_i: Int = jResp.SizeOfArray("locations").intValue
    while i < count_i {
        jResp.I = i
        id = jResp.IntOf("locations[i].id").intValue
        name = jResp.StringOf("locations[i].name")
        i = i + 1
    }

    i = 0
    count_i = jResp.SizeOfArray("results").intValue
    while i < count_i {
        jResp.I = i
        device = jResp.StringOf("results[i].device")
        location = jResp.IntOf("results[i].location").intValue
        public_id = jResp.StringOf("results[i].public_id")
        result_id = jResp.StringOf("results[i].result_id")
        i = i + 1
    }

    i = 0
    count_i = jResp.SizeOfArray("triggered_check_ids").intValue
    while i < count_i {
        jResp.I = i
        strVal = jResp.StringOf("triggered_check_ids[i]")
        i = i + 1
    }


}

Curl Command

curl -X POST
	-H "Content-Type: application/json"
	-H "Accept: application/json"
	-d '{
  "tests": [
    {
      "public_id": "aaa-aaa-aaa",
      "metadata": {
        "ci": {
          "pipeline": {
            "url": "aliquip in ex do"
          },
          "provider": {
            "name": "nostrud culpa"
          }
        },
        "git": {
          "branch": "esse",
          "commitSha": "occaecat incididunt aute exercitation"
        }
      }
    },
    {
      "public_id": "aaa-aaa-aaa",
      "metadata": {
        "ci": {
          "pipeline": {
            "url": "pariatur ullamco sed"
          },
          "provider": {
            "name": "adipisicing exercitation laborum non"
          }
        },
        "git": {
          "branch": "tempor fugiat consequat Lorem",
          "commitSha": "esse aliqua"
        }
      }
    }
  ]
}'
https://api.app.ddog-gov.com/api/v1/synthetics/tests/trigger

Postman Collection Item JSON

{
  "name": "Trigger Synthetic tests",
  "request": {
    "method": "POST",
    "header": [
      {
        "key": "Content-Type",
        "value": "application/json"
      },
      {
        "key": "Accept",
        "value": "application/json"
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n  \"tests\": [\n    {\n      \"public_id\": \"aaa-aaa-aaa\",\n      \"metadata\": {\n        \"ci\": {\n          \"pipeline\": {\n            \"url\": \"aliquip in ex do\"\n          },\n          \"provider\": {\n            \"name\": \"nostrud culpa\"\n          }\n        },\n        \"git\": {\n          \"branch\": \"esse\",\n          \"commitSha\": \"occaecat incididunt aute exercitation\"\n        }\n      }\n    },\n    {\n      \"public_id\": \"aaa-aaa-aaa\",\n      \"metadata\": {\n        \"ci\": {\n          \"pipeline\": {\n            \"url\": \"pariatur ullamco sed\"\n          },\n          \"provider\": {\n            \"name\": \"adipisicing exercitation laborum non\"\n          }\n        },\n        \"git\": {\n          \"branch\": \"tempor fugiat consequat Lorem\",\n          \"commitSha\": \"esse aliqua\"\n        }\n      }\n    }\n  ]\n}",
      "options": {
        "raw": {
          "headerFamily": "json",
          "language": "json"
        }
      }
    },
    "url": {
      "raw": "{{baseUrl}}/api/v1/synthetics/tests/trigger",
      "host": [
        "{{baseUrl}}"
      ],
      "path": [
        "api",
        "v1",
        "synthetics",
        "tests",
        "trigger"
      ]
    },
    "description": "Trigger a set of Synthetic tests."
  },
  "response": [
    {
      "name": "OK",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "value": "application/json"
          },
          {
            "key": "Accept",
            "value": "application/json"
          },
          {
            "description": "Added as a part of security scheme: apikey",
            "key": "DD-API-KEY",
            "value": "<API Key>"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n  \"tests\": [\n    {\n      \"public_id\": \"aaa-aaa-aaa\",\n      \"metadata\": {\n        \"ci\": {\n          \"pipeline\": {\n            \"url\": \"aliquip in ex do\"\n          },\n          \"provider\": {\n            \"name\": \"nostrud culpa\"\n          }\n        },\n        \"git\": {\n          \"branch\": \"esse\",\n          \"commitSha\": \"occaecat incididunt aute exercitation\"\n        }\n      }\n    },\n    {\n      \"public_id\": \"aaa-aaa-aaa\",\n      \"metadata\": {\n        \"ci\": {\n          \"pipeline\": {\n            \"url\": \"pariatur ullamco sed\"\n          },\n          \"provider\": {\n            \"name\": \"adipisicing exercitation laborum non\"\n          }\n        },\n        \"git\": {\n          \"branch\": \"tempor fugiat consequat Lorem\",\n          \"commitSha\": \"esse aliqua\"\n        }\n      }\n    }\n  ]\n}",
          "options": {
            "raw": {
              "headerFamily": "json",
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "{{baseUrl}}/api/v1/synthetics/tests/trigger",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "api",
            "v1",
            "synthetics",
            "tests",
            "trigger"
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n  \"batch_id\": \"anim quis\",\n  \"locations\": [\n    {\n      \"id\": -79104764,\n      \"name\": \"laborum\"\n    },\n    {\n      \"id\": -12473023,\n      \"name\": \"ullamco eu\"\n    }\n  ],\n  \"results\": [\n    {\n      \"device\": \"laptop_large\",\n      \"location\": -53475818,\n      \"public_id\": \"Excepteur et\",\n      \"result_id\": \"in\"\n    },\n    {\n      \"device\": \"laptop_large\",\n      \"location\": 59458053,\n      \"public_id\": \"ut labore\",\n      \"result_id\": \"quis tempor cillum est eu\"\n    }\n  ],\n  \"triggered_check_ids\": [\n    \"in irure culpa\",\n    \"sit ad do Ut esse\"\n  ]\n}"
    },
    {
      "name": "Bad Request",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "value": "application/json"
          },
          {
            "key": "Accept",
            "value": "application/json"
          },
          {
            "description": "Added as a part of security scheme: apikey",
            "key": "DD-API-KEY",
            "value": "<API Key>"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n  \"tests\": [\n    {\n      \"public_id\": \"aaa-aaa-aaa\",\n      \"metadata\": {\n        \"ci\": {\n          \"pipeline\": {\n            \"url\": \"aliquip in ex do\"\n          },\n          \"provider\": {\n            \"name\": \"nostrud culpa\"\n          }\n        },\n        \"git\": {\n          \"branch\": \"esse\",\n          \"commitSha\": \"occaecat incididunt aute exercitation\"\n        }\n      }\n    },\n    {\n      \"public_id\": \"aaa-aaa-aaa\",\n      \"metadata\": {\n        \"ci\": {\n          \"pipeline\": {\n            \"url\": \"pariatur ullamco sed\"\n          },\n          \"provider\": {\n            \"name\": \"adipisicing exercitation laborum non\"\n          }\n        },\n        \"git\": {\n          \"branch\": \"tempor fugiat consequat Lorem\",\n          \"commitSha\": \"esse aliqua\"\n        }\n      }\n    }\n  ]\n}",
          "options": {
            "raw": {
              "headerFamily": "json",
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "{{baseUrl}}/api/v1/synthetics/tests/trigger",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "api",
            "v1",
            "synthetics",
            "tests",
            "trigger"
          ]
        }
      },
      "status": "Bad Request",
      "code": 400,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n  \"errors\": [\n    \"Bad Request\",\n    \"Bad Request\"\n  ]\n}"
    },
    {
      "name": "Too many requests",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "value": "application/json"
          },
          {
            "key": "Accept",
            "value": "application/json"
          },
          {
            "description": "Added as a part of security scheme: apikey",
            "key": "DD-API-KEY",
            "value": "<API Key>"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n  \"tests\": [\n    {\n      \"public_id\": \"aaa-aaa-aaa\",\n      \"metadata\": {\n        \"ci\": {\n          \"pipeline\": {\n            \"url\": \"aliquip in ex do\"\n          },\n          \"provider\": {\n            \"name\": \"nostrud culpa\"\n          }\n        },\n        \"git\": {\n          \"branch\": \"esse\",\n          \"commitSha\": \"occaecat incididunt aute exercitation\"\n        }\n      }\n    },\n    {\n      \"public_id\": \"aaa-aaa-aaa\",\n      \"metadata\": {\n        \"ci\": {\n          \"pipeline\": {\n            \"url\": \"pariatur ullamco sed\"\n          },\n          \"provider\": {\n            \"name\": \"adipisicing exercitation laborum non\"\n          }\n        },\n        \"git\": {\n          \"branch\": \"tempor fugiat consequat Lorem\",\n          \"commitSha\": \"esse aliqua\"\n        }\n      }\n    }\n  ]\n}",
          "options": {
            "raw": {
              "headerFamily": "json",
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "{{baseUrl}}/api/v1/synthetics/tests/trigger",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "api",
            "v1",
            "synthetics",
            "tests",
            "trigger"
          ]
        }
      },
      "status": "Too Many Requests",
      "code": 429,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n  \"errors\": [\n    \"Bad Request\",\n    \"Bad Request\"\n  ]\n}"
    }
  ]
}