Chilkat Online Tools

PureBasic / Anypoint Platform APIs / Publish Project to Exchange

Back to Collection Items

IncludeFile "CkJsonObject.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkHttp.pb"
IncludeFile "CkHttpResponse.pb"

Procedure ChilkatExample()

    ; This example assumes the Chilkat API to have been previously unlocked.
    ; See Global Unlock Sample for sample code.

    http.i = CkHttp::ckCreate()
    If http.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success.i

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

    ; The following JSON is sent in the request body.

    ; {
    ;   "main": "api.raml",
    ;   "apiVersion": "1.0.0",
    ;   "version": "1.0.0",
    ;   "assetId": "publish-project-doc",
    ;   "groupId": "gid1234",
    ;   "classifier": "oas"
    ; }

    json.i = CkJsonObject::ckCreate()
    If json.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckUpdateString(json,"main","api.raml")
    CkJsonObject::ckUpdateString(json,"apiVersion","1.0.0")
    CkJsonObject::ckUpdateString(json,"version","1.0.0")
    CkJsonObject::ckUpdateString(json,"assetId","publish-project-doc")
    CkJsonObject::ckUpdateString(json,"groupId","gid1234")
    CkJsonObject::ckUpdateString(json,"classifier","oas")

    CkHttp::ckSetRequestHeader(http,"Content-Type","application/json")
    ; Adds the "Authorization: Bearer {{token}}" header.
    CkHttp::setCkAuthToken(http, "{{token}}")
    CkHttp::ckSetRequestHeader(http,"X-ANYPNT-ORG-ID","{{organization_id}}")
    CkHttp::ckSetRequestHeader(http,"x-owner-id","{{owner_id}}")

    resp.i = CkHttp::ckPostJson3(http,"https://domain.com/projects/{{project_id}}/branches/{{branch_name}}/publish/exchange","application/json",json)
    If CkHttp::ckLastMethodSuccess(http) = 0
        Debug CkHttp::ckLastErrorText(http)
        CkHttp::ckDispose(http)
        CkJsonObject::ckDispose(json)
        ProcedureReturn
    EndIf

    sbResponseBody.i = CkStringBuilder::ckCreate()
    If sbResponseBody.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkHttpResponse::ckGetBodySb(resp,sbResponseBody)

    jResp.i = CkJsonObject::ckCreate()
    If jResp.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckLoadSb(jResp,sbResponseBody)
    CkJsonObject::setCkEmitCompact(jResp, 0)

    Debug "Response Body:"
    Debug CkJsonObject::ckEmit(jResp)

    respStatusCode.i = CkHttpResponse::ckStatusCode(resp)
    Debug "Response Status Code = " + Str(respStatusCode)
    If respStatusCode >= 400
        Debug "Response Header:"
        Debug CkHttpResponse::ckHeader(resp)
        Debug "Failed."
        CkHttpResponse::ckDispose(resp)

        CkHttp::ckDispose(http)
        CkJsonObject::ckDispose(json)
        CkStringBuilder::ckDispose(sbResponseBody)
        CkJsonObject::ckDispose(jResp)
        ProcedureReturn
    EndIf

    CkHttpResponse::ckDispose(resp)

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

    ; {
    ;   "assetId": "test-publish-doc",
    ;   "groupId": "6a2f41a3-c54c-fce8-32d2-0324e1c32e22",
    ;   "organizationId": "6a2f41a3-c54c-fce8-32d2-0324e1c32e22",
    ;   "version": "1.0.0"
    ; }

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

    assetId.s = CkJsonObject::ckStringOf(jResp,"assetId")
    groupId.s = CkJsonObject::ckStringOf(jResp,"groupId")
    organizationId.s = CkJsonObject::ckStringOf(jResp,"organizationId")
    version.s = CkJsonObject::ckStringOf(jResp,"version")


    CkHttp::ckDispose(http)
    CkJsonObject::ckDispose(json)
    CkStringBuilder::ckDispose(sbResponseBody)
    CkJsonObject::ckDispose(jResp)


    ProcedureReturn
EndProcedure

Curl Command

curl -X POST
	-H "x-owner-id: {{owner_id}}"
	-H "X-ANYPNT-ORG-ID: {{organization_id}}"
	-H "Content-Type: application/json"
	-H "Authorization: Bearer {{token}}"
	-d '{
    "main": "api.raml",
    "apiVersion": "1.0.0",
    "version": "1.0.0",
    "assetId": "publish-project-doc",
    "groupId": "gid1234",
    "classifier": "oas"
}'
https://domain.com/projects/{{project_id}}/branches/{{branch_name}}/publish/exchange

Postman Collection Item JSON

{
  "name": "Publish Project to Exchange",
  "request": {
    "method": "POST",
    "header": [
      {
        "description": "(Required) User's ownerId, which identifies the user performing the operation. \nThis is NOT the username, but an UUID that can be found in the users page by the organization administrator.\n",
        "key": "x-owner-id",
        "value": "{{owner_id}}"
      },
      {
        "description": "(Required) OrganizationId, which identifies the organization to which perform the operation. \nThis can be found in the organizations page by the organization administrator.\n",
        "key": "X-ANYPNT-ORG-ID",
        "value": "{{organization_id}}"
      },
      {
        "key": "Content-Type",
        "value": "application/json"
      },
      {
        "key": "Authorization",
        "value": "Bearer {{token}}",
        "type": "text"
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n    \"main\": \"api.raml\",\n    \"apiVersion\": \"1.0.0\",\n    \"version\": \"1.0.0\",\n    \"assetId\": \"publish-project-doc\",\n    \"groupId\": \"gid1234\",\n    \"classifier\": \"oas\"\n}"
    },
    "url": {
      "raw": "{{url}}/projects/{{project_id}}/branches/{{branch_name}}/publish/exchange",
      "host": [
        "{{url}}"
      ],
      "path": [
        "projects",
        "{{project_id}}",
        "branches",
        "{{branch_name}}",
        "publish",
        "exchange"
      ]
    },
    "description": "Publish project to Exchange. As a result, an Exchange asset is created."
  },
  "response": [
    {
      "name": "Successful response",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "description": "(Required) User's ownerId, which identifies the user performing the operation. \nThis is NOT the username, but an UUID that can be found in the users page by the organization administrator.\n",
            "key": "x-owner-id",
            "value": "{{owner_id}}"
          },
          {
            "description": "(Required) OrganizationId, which identifies the organization to which perform the operation. \nThis can be found in the organizations page by the organization administrator.\n",
            "key": "X-ANYPNT-ORG-ID",
            "value": "{{organization_id}}"
          },
          {
            "key": "Content-Type",
            "value": "application/json"
          },
          {
            "key": "Authorization",
            "value": "Bearer {{token}}",
            "type": "text"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n    \"main\": \"api.raml\",\n    \"apiVersion\": \"1.0.0\",\n    \"version\": \"1.0.0\",\n    \"assetId\": \"publish-project-doc\",\n    \"groupId\": \"gid1234\",\n    \"classifier\": \"oas\"\n}"
        },
        "url": {
          "raw": "{{url}}/projects/{{project_id}}/branches/{{branch_name}}/publish/exchange",
          "host": [
            "{{url}}"
          ],
          "path": [
            "projects",
            "{{project_id}}",
            "branches",
            "{{branch_name}}",
            "publish",
            "exchange"
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n \"assetId\": \"test-publish-doc\",\n \"groupId\": \"6a2f41a3-c54c-fce8-32d2-0324e1c32e22\",\n \"organizationId\": \"6a2f41a3-c54c-fce8-32d2-0324e1c32e22\",\n \"version\": \"1.0.0\"\n}"
    },
    {
      "name": "401 - Authorization failed",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "description": "(Required) User's ownerId, which identifies the user performing the operation. \nThis is NOT the username, but an UUID that can be found in the users page by the organization administrator.\n",
            "key": "x-owner-id",
            "value": "{{owner_id}}"
          },
          {
            "description": "(Required) OrganizationId, which identifies the organization to which perform the operation. \nThis can be found in the organizations page by the organization administrator.\n",
            "key": "X-ANYPNT-ORG-ID",
            "value": "{{organization_id}}"
          },
          {
            "key": "Content-Type",
            "value": "application/json"
          },
          {
            "key": "Authorization",
            "value": "Bearer 00001",
            "type": "text",
            "description": "Generate error with a invalid token "
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n    \"main\": \"api.raml\",\n    \"apiVersion\": \"1.0.0\",\n    \"version\": \"1.0.0\",\n    \"assetId\": \"publish-project-doc\",\n    \"groupId\": \"gid1234\",\n    \"classifier\": \"oas\"\n}"
        },
        "url": {
          "raw": "{{url}}/projects/{{project_id}}/branches/{{branch_name}}/publish/exchange",
          "host": [
            "{{url}}"
          ],
          "path": [
            "projects",
            "{{project_id}}",
            "branches",
            "{{branch_name}}",
            "publish",
            "exchange"
          ]
        }
      },
      "status": "Unauthorized",
      "code": 401,
      "_postman_previewlanguage": "text",
      "header": [
        {
          "key": "Content-Type",
          "value": "text/plain"
        }
      ],
      "cookie": [
      ],
      "body": ""
    }
  ]
}