Part1 – Get ready for Form Service by Adobe API in BTP |
Part2 – Wrapping Form Service API into your own consumable API <- This blog |
Part3 – Create No-Code PDF generation app in SAP Build Apps(AppGyver App) |
CLASS zcl_http_adobe_pdf DEFINITION
PUBLIC
CREATE PUBLIC .
PUBLIC SECTION.
TYPES:BEGIN OF typ_w_input,
company TYPE string,
address TYPE string,
state TYPE string,
phone TYPE string,
item TYPE string,
description TYPE string,
quantity TYPE string,
unit_price TYPE string,
amount TYPE string,
END OF typ_w_input.
DATA:gw_input_data TYPE typ_w_input.
INTERFACES if_http_service_extension .
PROTECTED SECTION.
PRIVATE SECTION.
ENDCLASS.
CLASS zcl_http_adobe_pdf IMPLEMENTATION.
METHOD if_http_service_extension~handle_request.
DATA: lo_json TYPE REF TO data,
lv_rendered_pdf type string.
FIELD-SYMBOLS:
<data> TYPE data,
<field> TYPE any,
<value> TYPE any.
"Get inbound data
DATA(lv_request_body) = request->get_text( ).
"Get each fields from request body(Json)
lo_json = /ui2/cl_json=>generate( json = lv_request_body ).
IF lo_json IS BOUND.
ASSIGN lo_json->* TO <data>.
ASSIGN COMPONENT `company` OF STRUCTURE <data> TO <field>.
IF sy-subrc EQ 0.
ASSIGN <field>->* TO <value>.
gw_input_data-company = <value>.
UNASSIGN <value>.
ENDIF.
ASSIGN COMPONENT `address` OF STRUCTURE <data> TO <field>.
IF sy-subrc EQ 0.
ASSIGN <field>->* TO <value>.
gw_input_data-address = <value>.
UNASSIGN <value>.
ENDIF.
ASSIGN COMPONENT `state` OF STRUCTURE <data> TO <field>.
IF sy-subrc EQ 0.
ASSIGN <field>->* TO <value>.
gw_input_data-state = <value>.
UNASSIGN <value>.
ENDIF.
ASSIGN COMPONENT `phone` OF STRUCTURE <data> TO <field>.
IF sy-subrc EQ 0.
ASSIGN <field>->* TO <value>.
gw_input_data-phone = <value>.
UNASSIGN <value>.
ENDIF.
ASSIGN COMPONENT `item` OF STRUCTURE <data> TO <field>.
IF sy-subrc EQ 0.
ASSIGN <field>->* TO <value>.
gw_input_data-item = <value>.
UNASSIGN <value>.
ENDIF.
ASSIGN COMPONENT `description` OF STRUCTURE <data> TO <field>.
IF sy-subrc EQ 0.
ASSIGN <field>->* TO <value>.
gw_input_data-description = <value>.
UNASSIGN <value>.
ENDIF.
ASSIGN COMPONENT `quantity` OF STRUCTURE <data> TO <field>.
IF sy-subrc EQ 0.
ASSIGN <field>->* TO <value>.
gw_input_data-quantity = <value>.
UNASSIGN <value>.
ENDIF.
ASSIGN COMPONENT `unitprice` OF STRUCTURE <data> TO <field>.
IF sy-subrc EQ 0.
ASSIGN <field>->* TO <value>.
gw_input_data-unit_price = <value>.
UNASSIGN <value>.
ENDIF.
ASSIGN COMPONENT `amount` OF STRUCTURE <data> TO <field>.
IF sy-subrc EQ 0.
ASSIGN <field>->* TO <value>.
gw_input_data-amount = <value>.
UNASSIGN <value>.
ENDIF.
ENDIF.
TRY.
"Initialize Template Store Client
DATA(lo_client) = NEW zcl_fp_client(
iv_name = 'ADS_SRV'
).
"create xml string
DATA(lv_xml_raw) = |<form1>| &&
|<InvoiceNumber>Ego ille</InvoiceNumber>| &&
|<InvoiceDate>20040606T101010</InvoiceDate>| &&
|<OrderNumber>Si manu vacuas</OrderNumber>| &&
|<Terms>Apros tres et quidem</Terms>| &&
|<Company>| && gw_input_data-company && |</Company>| &&
|<Address>| && gw_input_data-address && |</Address>| &&
|<StateProvince>| && gw_input_data-state && |</StateProvince>| &&
|<ZipCode>Am undique</ZipCode>| &&
|<Phone>| && gw_input_data-phone && |</Phone>| &&
|<Fax>Vale</Fax>| &&
|<ContactName>Ego ille</ContactName>| &&
|<Item>| && gw_input_data-item && |</Item>| &&
|<Description>| && gw_input_data-description && |</Description>| &&
|<Quantity>| && gw_input_data-quantity && |</Quantity>| &&
|<UnitPrice>| && gw_input_data-unit_price && |</UnitPrice>| &&
|<Amount>| && gw_input_data-amount && |</Amount>| &&
|<Item></Item>| && |<Description></Description>| && |<Quantity></Quantity>| && |<UnitPrice></UnitPrice>| && |<Amount></Amount>| &&
|<Item></Item>| && |<Description></Description>| && |<Quantity></Quantity>| && |<UnitPrice></UnitPrice>| && |<Amount></Amount>| &&
|<Item></Item>| && |<Description></Description>| && |<Quantity></Quantity>| && |<UnitPrice></UnitPrice>| && |<Amount></Amount>| &&
|<Item></Item>| && |<Description></Description>| && |<Quantity></Quantity>| && |<UnitPrice></UnitPrice>| && |<Amount></Amount>| &&
|<Item></Item>| && |<Description></Description>| && |<Quantity></Quantity>| && |<UnitPrice></UnitPrice>| && |<Amount></Amount>| &&
|<Item></Item>| && |<Description></Description>| && |<Quantity></Quantity>| && |<UnitPrice></UnitPrice>| && |<Amount></Amount>| &&
|<Item></Item>| && |<Description></Description>| && |<Quantity></Quantity>| && |<UnitPrice></UnitPrice>| && |<Amount></Amount>| &&
|<Item></Item>| && |<Description></Description>| && |<Quantity></Quantity>| && |<UnitPrice></UnitPrice>| && |<Amount></Amount>| &&
|<Item></Item>| && |<Description></Description>| && |<Quantity></Quantity>| && |<UnitPrice></UnitPrice>| && |<Amount></Amount>| &&
|<Item></Item>| && |<Description></Description>| && |<Quantity></Quantity>| && |<UnitPrice></UnitPrice>| && |<Amount></Amount>| &&
|<Item></Item>| && |<Description></Description>| && |<Quantity></Quantity>| && |<UnitPrice></UnitPrice>| && |<Amount></Amount>| &&
|<Item></Item>| && |<Description></Description>| && |<Quantity></Quantity>| && |<UnitPrice></UnitPrice>| && |<Amount></Amount>| &&
|<Item></Item>| && |<Description></Description>| && |<Quantity></Quantity>| && |<UnitPrice></UnitPrice>| && |<Amount></Amount>| &&
|<Item></Item>| && |<Description></Description>| && |<Quantity></Quantity>| && |<UnitPrice></UnitPrice>| && |<Amount></Amount>| &&
|<Subtotal></Subtotal>| &&
|<StateTaxRate></StateTaxRate>| &&
|<StateTax></StateTax>| &&
|<FederalTaxRate></FederalTaxRate>| &&
|<FederalTaxRate></FederalTaxRate>| &&
|<FederalTax></FederalTax>| &&
|<ShippingCharge></ShippingCharge>| &&
|<GrandTotal></GrandTotal>| &&
|<Comments></Comments>| &&
|<AmountPaid></AmountPaid>| &&
|<DateReceived></DateReceived>| &&
|</form1>|.
DATA(lv_xml) = cl_web_http_utility=>encode_base64( lv_xml_raw ).
"Render PDF by caling REST API
lv_rendered_pdf = lo_client->reder_pdf( iv_xml = lv_xml ).
ENDTRY.
response->set_text(
EXPORTING
i_text = lv_rendered_pdf ).
ENDMETHOD.
ENDCLASS.
Add the communication user as the user of this communication system.
Type | Internet |
Host | Your ABAP environment System URL |
Port | 443 |
Use SSL | X |
Service Collection URL: | /sap/bc/http/sap/zhttp_adobe_pdf |
Authentication type | Basic |
Username | Your communication user in ABAP environment |
Password | Password for the communication user |
Select | API Provider |
API Provider | Your API Provider |
URL | /sap/bc/http/sap/zhttp_adobe_pdf |
Name | xxxxxx |
Title | xxxxxx |
API State | Active |
Host Alias: | Your defualt Host Alias of your API management service |
API Base Path: | /zhttp_adobe_pdf(Or you can decide your own) |
Service Type | REST |
Policy allows you to add customized behaviors to your API, in order to control traffic, add/reduce security measures, message transformation, and so on so forth. In this tutorial, we add two behaviors so that it's easy to consume the API from SAP Build Apps(AppGyver app):
- Allow Cross Origin CORS when consumed from SAP Build Apps:
<!-- Key/value pairs can be stored, retrieved, and deleted from named existing maps by configuring this policy by specifying PUT, GET, or DELETE operations -->
<!-- mapIdentifier refers to the name of the key value map -->
<!-- Don't use Key Value Maps to store your logs as this can impact API Proxy runtime flow -->
<KeyValueMapOperations mapIdentifier="ABAP_Environment" continueOnError="false" enabled="true" xmlns="http://www.sap.com/apimgmt">
<Get assignTo="private.BasicAuthUsername" index='1'>
<Key><Parameter>username</Parameter></Key>
</Get>
<Get assignTo="private.BasicAuthPassword" index='1'>
<Key><Parameter>password</Parameter></Key>
</Get>
<Scope>environment</Scope>
</KeyValueMapOperations>
<BasicAuthentication continueOnError='false' enabled='true' xmlns='http://www.sap.com/apimgmt'>
<Operation>Encode</Operation>
<IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
<User ref='private.BasicAuthUsername'></User>
<Password ref='private.BasicAuthPassword'></Password>
<AssignTo createNew="true">request.header.Authorization</AssignTo>
</BasicAuthentication>
<!-- This policy can be used to create or modify the standard HTTP request and response messages -->
<AssignMessage async="false" continueOnError="false" enabled="true" xmlns='http://www.sap.com/apimgmt'>
<Add>
<Headers>
<Header name="Access-Control-Allow-Origin">*</Header>
<Header name="Access-Control-Allow-Headers">set-cookie, origin, accept, maxdataserviceversion, x-csrf-token, authorization, dataserviceversion, accept-language, x-http-method, content-type, X-Requested-With</Header>
<Header name="Access-Control-Max-Age">3628800</Header>
<Header name="Access-Control-Allow-Methods">GET, PUT, POST, DELETE</Header>
<Header name="Access-Control-Expose-Headers">set-cookie, x-csrf-token, x-http-method</Header>
</Headers>
</Add>
<IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
<AssignTo createNew="false" type="response">response</AssignTo>
</AssignMessage>
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
13 | |
10 | |
7 | |
7 | |
6 | |
5 | |
5 | |
5 | |
4 | |
4 |