Chilkat Online Tools

SQL Server / Cognite API v1 / Update relationships

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.

    -- {
    --   "items": [
    --     {
    --       "externalId": "dolor amet sit",
    --       "update": {
    --         "sourceType": {
    --           "set": "timeSeries"
    --         },
    --         "sourceExternalId": {
    --           "set": "qui labor"
    --         },
    --         "targetType": {
    --           "set": "file"
    --         },
    --         "targetExternalId": {
    --           "set": "laborum"
    --         },
    --         "confidence": {
    --           "setNull": true
    --         },
    --         "startTime": {
    --           "set": 97867670
    --         },
    --         "endTime": {
    --           "setNull": true
    --         },
    --         "dataSetId": {
    --           "set": 8379377116294748
    --         },
    --         "labels": {
    --           "set": [
    --             {
    --               "externalId": "ullamco anim"
    --             },
    --             {
    --               "externalId": "aliquip ut"
    --             }
    --           ]
    --         }
    --       }
    --     },
    --     {
    --       "externalId": "reprehenderit ut",
    --       "update": {
    --         "sourceType": {
    --           "set": "timeSeries"
    --         },
    --         "sourceExternalId": {
    --           "set": "adipisicing ullamco consequat"
    --         },
    --         "targetType": {
    --           "set": "sequence"
    --         },
    --         "targetExternalId": {
    --           "set": "aliqua"
    --         },
    --         "confidence": {
    --           "setNull": true
    --         },
    --         "startTime": {
    --           "setNull": false
    --         },
    --         "endTime": {
    --           "set": 51106464
    --         },
    --         "dataSetId": {
    --           "setNull": false
    --         },
    --         "labels": {
    --           "set": [
    --             {
    --               "externalId": "Lorem ipsum"
    --             },
    --             {
    --               "externalId": "officia consectetur pariatur"
    --             }
    --           ]
    --         }
    --       }
    --     }
    --   ]
    -- }

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

    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[0].externalId', 'dolor amet sit'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[0].update.sourceType.set', 'timeSeries'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[0].update.sourceExternalId.set', 'qui labor'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[0].update.targetType.set', 'file'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[0].update.targetExternalId.set', 'laborum'
    EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'items[0].update.confidence.setNull', 1
    EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'items[0].update.startTime.set', 97867670
    EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'items[0].update.endTime.setNull', 1
    EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'items[0].update.dataSetId.set', 123
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[0].update.labels.set[0].externalId', 'ullamco anim'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[0].update.labels.set[1].externalId', 'aliquip ut'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[1].externalId', 'reprehenderit ut'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[1].update.sourceType.set', 'timeSeries'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[1].update.sourceExternalId.set', 'adipisicing ullamco consequat'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[1].update.targetType.set', 'sequence'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[1].update.targetExternalId.set', 'aliqua'
    EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'items[1].update.confidence.setNull', 1
    EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'items[1].update.startTime.setNull', 0
    EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'items[1].update.endTime.set', 51106464
    EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'items[1].update.dataSetId.setNull', 0
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[1].update.labels.set[0].externalId', 'Lorem ipsum'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[1].update.labels.set[1].externalId', 'officia consectetur pariatur'

    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'content-type', 'application/json'
    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'api-key', '{{api-key}}'

    DECLARE @resp int
    EXEC sp_OAMethod @http, 'PostJson3', @resp OUT, 'https://domain.com/api/v1/projects/{{project}}/relationships/update', '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 "api-key: {{api-key}}"
	-H "content-type: application/json"
	-d '{
    "items": [
        {
            "externalId": "dolor amet sit",
            "update": {
                "sourceType": {
                    "set": "timeSeries"
                },
                "sourceExternalId": {
                    "set": "qui labor"
                },
                "targetType": {
                    "set": "file"
                },
                "targetExternalId": {
                    "set": "laborum"
                },
                "confidence": {
                    "setNull": true
                },
                "startTime": {
                    "set": 97867670
                },
                "endTime": {
                    "setNull": true
                },
                "dataSetId": {
                    "set": 8379377116294748
                },
                "labels": {
                    "set": [
                        {
                            "externalId": "ullamco anim"
                        },
                        {
                            "externalId": "aliquip ut"
                        }
                    ]
                }
            }
        },
        {
            "externalId": "reprehenderit ut",
            "update": {
                "sourceType": {
                    "set": "timeSeries"
                },
                "sourceExternalId": {
                    "set": "adipisicing ullamco consequat"
                },
                "targetType": {
                    "set": "sequence"
                },
                "targetExternalId": {
                    "set": "aliqua"
                },
                "confidence": {
                    "setNull": true
                },
                "startTime": {
                    "setNull": false
                },
                "endTime": {
                    "set": 51106464
                },
                "dataSetId": {
                    "setNull": false
                },
                "labels": {
                    "set": [
                        {
                            "externalId": "Lorem ipsum"
                        },
                        {
                            "externalId": "officia consectetur pariatur"
                        }
                    ]
                }
            }
        }
    ]
}'
https://domain.com/api/v1/projects/{{project}}/relationships/update

Postman Collection Item JSON

{
  "id": "updateRelationships",
  "name": "Update relationships",
  "request": {
    "url": {
      "host": "{{baseUrl}}",
      "path": [
        "api",
        "v1",
        "projects",
        "{{project}}",
        "relationships",
        "update"
      ],
      "query": [
      ],
      "variable": [
      ]
    },
    "method": "POST",
    "header": [
      {
        "key": "api-key",
        "value": "{{api-key}}",
        "description": "An admin can create API keys in the Cognite console."
      },
      {
        "key": "content-type",
        "value": "application/json"
      }
    ],
    "description": "Update relationships between resources according to the partial definitions of the relationships given in the payload of the request. This means that fields not mentioned in the payload will remain unchanged. Up to 1000 relationships can be updated in one operation.\nTo delete a value from an optional value the `setNull` field should be set to `true`.\nThe order of the updated relationships in the response equals the order in the request.",
    "body": {
      "mode": "raw",
      "raw": "{\n    \"items\": [\n        {\n            \"externalId\": \"dolor amet sit\",\n            \"update\": {\n                \"sourceType\": {\n                    \"set\": \"timeSeries\"\n                },\n                \"sourceExternalId\": {\n                    \"set\": \"qui labor\"\n                },\n                \"targetType\": {\n                    \"set\": \"file\"\n                },\n                \"targetExternalId\": {\n                    \"set\": \"laborum\"\n                },\n                \"confidence\": {\n                    \"setNull\": true\n                },\n                \"startTime\": {\n                    \"set\": 97867670\n                },\n                \"endTime\": {\n                    \"setNull\": true\n                },\n                \"dataSetId\": {\n                    \"set\": 8379377116294748\n                },\n                \"labels\": {\n                    \"set\": [\n                        {\n                            \"externalId\": \"ullamco anim\"\n                        },\n                        {\n                            \"externalId\": \"aliquip ut\"\n                        }\n                    ]\n                }\n            }\n        },\n        {\n            \"externalId\": \"reprehenderit ut\",\n            \"update\": {\n                \"sourceType\": {\n                    \"set\": \"timeSeries\"\n                },\n                \"sourceExternalId\": {\n                    \"set\": \"adipisicing ullamco consequat\"\n                },\n                \"targetType\": {\n                    \"set\": \"sequence\"\n                },\n                \"targetExternalId\": {\n                    \"set\": \"aliqua\"\n                },\n                \"confidence\": {\n                    \"setNull\": true\n                },\n                \"startTime\": {\n                    \"setNull\": false\n                },\n                \"endTime\": {\n                    \"set\": 51106464\n                },\n                \"dataSetId\": {\n                    \"setNull\": false\n                },\n                \"labels\": {\n                    \"set\": [\n                        {\n                            \"externalId\": \"Lorem ipsum\"\n                        },\n                        {\n                            \"externalId\": \"officia consectetur pariatur\"\n                        }\n                    ]\n                }\n            }\n        }\n    ]\n}"
    }
  }
}