2

Test BTP SAP Print Service With Postman

 1 year ago
source link: https://blogs.sap.com/2022/12/25/test-btp-sap-print-service-with-postman/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

In the blog SAP Print Service, Markus Berg has introduce BTP SAP Print Service. In my following 2 blogs, I will introduce how to configure, how to test, and how to use it in UI5 . There is mission Add Printing Label to Your User Experience for SAP S/4HANA Cloud.in which a S4Hana Cloud side by side extension for label printing has been introduced in detail.  Configure printing service is big part of it . You can find detailed information in the mission also .

Prerequisite:

You have installed postman on your computer .

Step 1, Check entitlement in BTP cockpit in your sub-account, the following 3 printing service plans should be listed .

To do this, the role Subaccount Admin should be assigned to your user . If you can not find the following 3 print service plans , please contact your BTP customer success manager .

p01-scaled.jpg

Step 2, Onboarding.

In this step you will create service instances for plan sender and receiver.Service keys for plan sender and receiver will be created .To take out this step you can use Booster from BTP cockpit in global account. Plan receiver is used to upload document and create printing task. Plan sender will be used by cloud printing manager to connect.

p2-1.jpg
p3-1-scaled.jpg

Step 3, Create subscription for print service plan standard configure authorization

You can following the document to create print service subscription .

For authorization configuration , you can follow the following documents :

Defining and Bundling Roles

Assigning Role Collections to Users

Configuring Role Collection

Step 3, Create printQ .

p4-1-scaled.jpg
p5-1.jpg
p6-1-scaled.jpg
p7-1.jpg

Step 4, Note down service key information of service instance of plan receiver .

p8-1-scaled.jpg

Note down the information in the red frame .

p9-1.jpg

Step 5, Save postman collection content as a  json file.

{
	"info": {
		"_postman_id": "43b9377d-9355-4e0b-bd4f-044c9deb6d9d",
		"name": "print",
		"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
	},
	"item": [
		{
			"name": "Step1 Get Token",
			"event": [
				{
					"listen": "test",
					"script": {
						"exec": [
							"pm.collectionVariables.set(\"bearToken\",\"Bearer \".concat(pm.response.json().access_token))"
						],
						"type": "text/javascript"
					}
				}
			],
			"request": {
				"auth": {
					"type": "basic",
					"basic": [
						{
							"key": "password",
							"value": "{{printclientsecret}}",
							"type": "string"
						},
						{
							"key": "username",
							"value": "{{printclientkey}}",
							"type": "string"
						}
					]
				},
				"method": "POST",
				"header": [],
				"body": {
					"mode": "urlencoded",
					"urlencoded": [
						{
							"key": "grant_type",
							"value": "client_credentials",
							"type": "default"
						}
					]
				},
				"url": {
					"raw": "{{printtokenurl}}",
					"host": [
						"{{printtokenurl}}"
					]
				}
			},
			"response": []
		},
		{
			"name": "Step2 Get PrintQ",
			"event": [
				{
					"listen": "test",
					"script": {
						"exec": [
							"pm.collectionVariables.set(\"printQ\",pm.response.json()[0].qname)"
						],
						"type": "text/javascript"
					}
				},
				{
					"listen": "prerequest",
					"script": {
						"exec": [
							"pm.request.addHeader(\"Authorization\",pm.collectionVariables.get(\"bearToken\"));"
						],
						"type": "text/javascript"
					}
				}
			],
			"request": {
				"auth": {
					"type": "oauth2",
					"oauth2": [
						{
							"key": "clientId",
							"value": "{{printclientkey}}",
							"type": "string"
						},
						{
							"key": "accessTokenUrl",
							"value": "{{printtokenurl}}",
							"type": "string"
						},
						{
							"key": "grant_type",
							"value": "client_credentials",
							"type": "string"
						},
						{
							"key": "clientSecret",
							"value": "{{printclientsecret}}",
							"type": "string"
						},
						{
							"key": "tokenName",
							"value": "printkey",
							"type": "string"
						},
						{
							"key": "addTokenTo",
							"value": "header",
							"type": "string"
						},
						{
							"key": "scope",
							"value": "",
							"type": "string"
						},
						{
							"key": "tokenType",
							"value": "Bearer",
							"type": "string"
						},
						{
							"key": "client_authentication",
							"value": "header",
							"type": "string"
						},
						{
							"key": "password",
							"value": "Guoq07@5",
							"type": "string"
						},
						{
							"key": "username",
							"value": "[email protected]",
							"type": "string"
						},
						{
							"key": "headerPrefix",
							"value": "Bearer",
							"type": "string"
						}
					]
				},
				"method": "GET",
				"header": [],
				"url": {
					"raw": "{{printserviceurl}}/qm/api/v1/rest/queues",
					"host": [
						"{{printserviceurl}}"
					],
					"path": [
						"qm",
						"api",
						"v1",
						"rest",
						"queues"
					]
				}
			},
			"response": []
		},
		{
			"name": "Step3 Upload Document",
			"event": [
				{
					"listen": "test",
					"script": {
						"exec": [
							"if(pm.response.to.have.status(201)){",
							"   pm.collectionVariables.set(\"dmId\",pm.response.text());",
							"   pm.collectionVariables.set(\"fileName\",pm.request.body.formdata.all()[0].src.split(\"/\").last());",
							"}",
							"    "
						],
						"type": "text/javascript"
					}
				}
			],
			"protocolProfileBehavior": {
				"disabledSystemHeaders": {
					"accept-encoding": true,
					"connection": true,
					"accept": true
				}
			},
			"request": {
				"auth": {
					"type": "oauth2",
					"oauth2": [
						{
							"key": "grant_type",
							"value": "client_credentials",
							"type": "string"
						},
						{
							"key": "client_authentication",
							"value": "header",
							"type": "string"
						},
						{
							"key": "clientId",
							"value": "{{printclientkey}}",
							"type": "string"
						},
						{
							"key": "accessTokenUrl",
							"value": "{{printtokenurl}}",
							"type": "string"
						},
						{
							"key": "clientSecret",
							"value": "{{printclientsecret}}",
							"type": "string"
						},
						{
							"key": "tokenName",
							"value": "printkey",
							"type": "string"
						},
						{
							"key": "addTokenTo",
							"value": "header",
							"type": "string"
						},
						{
							"key": "scope",
							"value": "",
							"type": "string"
						},
						{
							"key": "tokenType",
							"value": "Bearer",
							"type": "string"
						},
						{
							"key": "password",
							"value": "Guoq07@5",
							"type": "string"
						},
						{
							"key": "username",
							"value": "[email protected]",
							"type": "string"
						},
						{
							"key": "headerPrefix",
							"value": "Bearer",
							"type": "string"
						}
					]
				},
				"method": "POST",
				"header": [
					{
						"key": "If-None-Match",
						"value": "*",
						"type": "default"
					},
					{
						"key": "scan",
						"value": "true",
						"type": "default"
					},
					{
						"key": "Accept",
						"value": "application/json",
						"type": "default"
					},
					{
						"key": "DataServiceVersion",
						"value": "2.0",
						"type": "default"
					}
				],
				"body": {
					"mode": "formdata",
					"formdata": [
						{
							"key": "file",
							"type": "file",
							"src": "/Users/I501000/Downloads/q4.jpg"
						}
					]
				},
				"url": {
					"raw": "{{printserviceurl}}/dm/api/v1/rest/print-documents",
					"host": [
						"{{printserviceurl}}"
					],
					"path": [
						"dm",
						"api",
						"v1",
						"rest",
						"print-documents"
					]
				}
			},
			"response": []
		},
		{
			"name": "Step4 Create Print Task",
			"event": [
				{
					"listen": "test",
					"script": {
						"exec": [
							""
						],
						"type": "text/javascript"
					}
				},
				{
					"listen": "prerequest",
					"script": {
						"exec": [
							"pm.collectionVariables.get(\"dmId\");"
						],
						"type": "text/javascript"
					}
				}
			],
			"request": {
				"auth": {
					"type": "oauth2",
					"oauth2": [
						{
							"key": "grant_type",
							"value": "client_credentials",
							"type": "string"
						},
						{
							"key": "client_authentication",
							"value": "header",
							"type": "string"
						},
						{
							"key": "clientId",
							"value": "{{printclientkey}}",
							"type": "string"
						},
						{
							"key": "accessTokenUrl",
							"value": "{{printtokenurl}}",
							"type": "string"
						},
						{
							"key": "clientSecret",
							"value": "{{printclientsecret}}",
							"type": "string"
						},
						{
							"key": "tokenName",
							"value": "printkey",
							"type": "string"
						},
						{
							"key": "addTokenTo",
							"value": "header",
							"type": "string"
						},
						{
							"key": "scope",
							"value": "",
							"type": "string"
						},
						{
							"key": "tokenType",
							"value": "Bearer",
							"type": "string"
						},
						{
							"key": "password",
							"value": "Guoq07@5",
							"type": "string"
						},
						{
							"key": "username",
							"value": "[email protected]",
							"type": "string"
						},
						{
							"key": "headerPrefix",
							"value": "Bearer",
							"type": "string"
						}
					]
				},
				"method": "PUT",
				"header": [
					{
						"key": "If-None-Match",
						"value": "*",
						"type": "default"
					}
				],
				"body": {
					"mode": "raw",
					"raw": "{\n   \"numberOfCopies\": 1,\n   \"username\": \"test user\",\n   \"qname\": \"{{printQ}}\",\n   \"metadata\": {\n      \"business_metadata\": {\n         \"business_user\": \"user1\",\n         \"object_node_type\": \"object_node_1\"\n      },\n      \"version\": 1.2\n   },\n   \"printContents\": [\n      {\n         \"objectKey\": \"{{dmId}}\",\n         \"documentName\": \"{{fileName}}\"\n      }\n   ]\n}",
					"options": {
						"raw": {
							"language": "json"
						}
					}
				},
				"url": {
					"raw": "{{printserviceurl}}/qm/api/v1/rest/print-tasks/{{dmId}}",
					"host": [
						"{{printserviceurl}}"
					],
					"path": [
						"qm",
						"api",
						"v1",
						"rest",
						"print-tasks",
						"{{dmId}}"
					],
					"query": [
						{
							"key": "itemId",
							"value": "2349add8-d48e-46ee-91d6-e7bb98cfdbff",
							"disabled": true
						}
					]
				}
			},
			"response": []
		}
	],
	"event": [
		{
			"listen": "prerequest",
			"script": {
				"type": "text/javascript",
				"exec": [
					""
				]
			}
		},
		{
			"listen": "test",
			"script": {
				"type": "text/javascript",
				"exec": [
					""
				]
			}
		}
	],
	"variable": [
		{
			"key": "printtokenurl",
			"value": "https://apjcoe-x66183k9.authentication.eu10.hana.ondemand.com/oauth/token",
			"type": "default"
		},
		{
			"key": "printclientkey",
			"value": "sb-6aa6869e-be85-4b6f-b2345|print-app!b46384",
			"type": "default"
		},
		{
			"key": "printclientsecret",
			"value": "Dwsj8LMo=",
			"type": "default"
		},
		{
			"key": "printserviceurl",
			"value": "https://api.eu10.print.services.sap",
			"type": "default"
		},
		{
			"key": "dmId",
			"value": "",
			"type": "default"
		},
		{
			"key": "fileName",
			"value": ""
		},
		{
			"key": "printQ",
			"value": ""
		},
		{
			"key": "bearToken",
			"value": ""
		}
	]
}

Step 6, Import collection from step 5’s json file in postman.

p10-1-scaled.jpg

Step 7, Maintain collection variable base on service key information from step 4.

only maintain the variables in the red frame.

p11-1-scaled.jpg

Step 8, Execute the request steps from step 1 to step 4 sequently in postman.

Note: In step 3, please select a picture or a pdf document in request body .

p12-1-scaled.jpg
p13-1-scaled.jpg

Step 9, Check the result in print Q

We can find the created print task in the following screenshot .

P14-1-scaled.jpg

Thanks for your time!

Best Regards!

Jacky Liu


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK