Chilkat Online Tools

SQL Server / Anypoint Platform APIs / Add automated policies (example spike-control)

Back to Collection Items

-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    -- This example assumes the Chilkat API to have been previously unlocked.
    -- See Global Unlock Sample for sample code.

    DECLARE @http int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.Http', @http OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    DECLARE @success int

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

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

    -- {
    --   "configurationData": {
    --     "maximumRequests": 1,
    --     "timePeriodInMilliseconds": 1000,
    --     "delayTimeInMillis": 1000,
    --     "delayAttempts": 1,
    --     "queuingLimit": 5,
    --     "exposeHeaders": false
    --   },
    --   "id": null,
    --   "pointcutData": null,
    --   "ruleOfApplication": {
    --     "environmentId": "{{environment_id}}",
    --     "organizationId": "{{organization_id}}",
    --     "range": {
    --       "from": "4.1.1"
    --     }
    --   },
    --   "groupId": "68ef9520-24e9-4cf2-b2f5-620025690913",
    --   "assetId": "spike-control",
    --   "assetVersion": "1.1.5"
    -- }

    DECLARE @json int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.JsonObject', @json OUT

    EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'configurationData.maximumRequests', 1
    EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'configurationData.timePeriodInMilliseconds', 1000
    EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'configurationData.delayTimeInMillis', 1000
    EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'configurationData.delayAttempts', 1
    EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'configurationData.queuingLimit', 5
    EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'configurationData.exposeHeaders', 0
    EXEC sp_OAMethod @json, 'UpdateNull', @success OUT, 'id'
    EXEC sp_OAMethod @json, 'UpdateNull', @success OUT, 'pointcutData'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'ruleOfApplication.environmentId', '{{environment_id}}'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'ruleOfApplication.organizationId', '{{organization_id}}'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'ruleOfApplication.range.from', '4.1.1'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'groupId', '68ef9520-24e9-4cf2-b2f5-620025690913'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'assetId', 'spike-control'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'assetVersion', '1.1.5'

    -- Adds the "Authorization: Bearer {{token}}" header.
    EXEC sp_OASetProperty @http, 'AuthToken', '{{token}}'

    DECLARE @resp int
    EXEC sp_OAMethod @http, 'PostJson3', @resp OUT, 'https://domain.com/apimanager/api/v1/organizations/{{organization_id}}/automated-policies', 'application/json', @json
    EXEC sp_OAGetProperty @http, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 = 0
      BEGIN
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @json
        RETURN
      END

    EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
    PRINT @iTmp0
    EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
    PRINT @sTmp0
    EXEC @hr = sp_OADestroy @resp


    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @json


END
GO

Curl Command

curl -X POST
	-H "Authorization: Bearer {{token}}"
	-d '{
   "configurationData":{
      "maximumRequests":1,
      "timePeriodInMilliseconds":1000,
      "delayTimeInMillis":1000,
      "delayAttempts":1,
      "queuingLimit":5,
      "exposeHeaders":false
   },
   "id":null,
   "pointcutData":null,
   "ruleOfApplication":{
      "environmentId":"{{environment_id}}",
      "organizationId":"{{organization_id}}",
      "range":{
         "from":"4.1.1"
      }
   },
   "groupId":"68ef9520-24e9-4cf2-b2f5-620025690913",
   "assetId":"spike-control",
   "assetVersion":"1.1.5"
}'
https://domain.com/apimanager/api/v1/organizations/{{organization_id}}/automated-policies

Postman Collection Item JSON

{
  "name": "Add automated policies (example spike-control)",
  "request": {
    "method": "POST",
    "header": [
      {
        "key": "Authorization",
        "value": "Bearer {{token}}",
        "type": "text"
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "{\r\n   \"configurationData\":{\r\n      \"maximumRequests\":1,\r\n      \"timePeriodInMilliseconds\":1000,\r\n      \"delayTimeInMillis\":1000,\r\n      \"delayAttempts\":1,\r\n      \"queuingLimit\":5,\r\n      \"exposeHeaders\":false\r\n   },\r\n   \"id\":null,\r\n   \"pointcutData\":null,\r\n   \"ruleOfApplication\":{\r\n      \"environmentId\":\"{{environment_id}}\",\r\n      \"organizationId\":\"{{organization_id}}\",\r\n      \"range\":{\r\n         \"from\":\"4.1.1\"\r\n      }\r\n   },\r\n   \"groupId\":\"68ef9520-24e9-4cf2-b2f5-620025690913\",\r\n   \"assetId\":\"spike-control\",\r\n   \"assetVersion\":\"1.1.5\"\r\n}",
      "options": {
        "raw": {
          "language": "json"
        }
      }
    },
    "url": {
      "raw": "{{url}}/apimanager/api/v1/organizations/{{organization_id}}/automated-policies",
      "host": [
        "{{url}}"
      ],
      "path": [
        "apimanager",
        "api",
        "v1",
        "organizations",
        "{{organization_id}}",
        "automated-policies"
      ]
    }
  },
  "response": [
  ]
}