Create an HTTP Service

You will learn

  • How to create an HTTP service
  • How to return system data using a (whitelisted) ABAP utility class

You can then access the ABAP system from the browser, for example to fetch a report. In this tutorial, you will simply fetch your user name and the date and time for simplicity.


Step 1: Create an HTTP service

  1. Select a package and choose New > Other Repository Object from the context menu:

    Image depicting step-1a-new-repo-object

  2. Enter the filter text HTTP and choose Next:

    Image depicting step-1b-choose-HTTP-service
  3. Enter a name and description for your service and choose Next:

    Image depicting step-1c-name-service

  4. Choose or create a transport request:

    Image depicting step-1d-transport-request

The new HTTP service is displayed on a new tab. The handler class and URL are generated automatically, in the form:https://<server:port>/sap/bc/http/sap/<service_name>?sap-client=100

Image depicting step-1e-new-service-created

完成

Log on to answer question

Step 2: Implement the handler class

Now, you will implement the handler class, starting with a simple text output.

  1. Open the handler class by clicking on it:

    Image depicting step-2a-open-handler-class
  2. The structure of the class and the interfaces statement for IF_HTTP_SERVICE_EXTENSION are generated automatically.

  3. Go to the class implementation section and insert the following statement in the method:

    response->set_text('Hello again!').

    Image depicting step-2b-insert-method

完成

Log on to answer question

Step 3: Save and activate

Save (Ctrl+S) and activate (Ctrl+F3) your class.

完成

Log on to answer question

Step 4: Test the service

  1. Test your service in the browser by clicking the URL link:

    Image depicting step-4-test-http-service
  2. If necessary, log in again. The preview open automatically in a new tab and display something like this:

    Image depicting step-4b-hello

完成

Log on to answer question

Step 5: Add system date to the method

Now you will add to the method by fetching the date from the back end first.

In the ABAP environment, you can only use whitelisted APIs. Therefore, you cannot use SY-DATUM. Instead, you call the appropriate method of the class CL_ABAP_CONTEXT_INFO.

You then cast this date variable to a string variable and output that as before.

Delete the statement response->set_text('Hello again!'). and add the following to your code:

ABAP

Copy

DATA(system_date) = CL_ABAP_CONTEXT_INFO=>get_system_date( ).
DATA: text type string.

text = system_date.
response->set_text( text ).

完成

Log on to answer question

Step 6: Test the service again

  1. Save (Ctrl+S) and activate (Ctrl+F3) your class.
  2. Test your service by clicking the URL link again. This time, the preview should display something like this:Image depicting step-6-system-date

完成

Log on to answer question

Step 7: Test yourself

Implement the method IF_HTTP_SERVICE_EXTENSION~HANDLE_REQUEST so that it returns the current user name (UNAME), not the system date.

Use the variables user_name and text and the method get_user_formatted_name.

 

×

Log on to answer question

Step 8: Copy code (optional)

Your code should look like this:

ABAP

Copy

class ZCL_JP_TEST_HTTP definition
  public
  create public .

public section.

  interfaces IF_HTTP_SERVICE_EXTENSION .
protected section.
private section.
ENDCLASS.

CLASS ZCL_JP_TEST_HTTP IMPLEMENTATION.

  method IF_HTTP_SERVICE_EXTENSION~HANDLE_REQUEST.
      DATA(system_date) = CL_ABAP_CONTEXT_INFO=>get_system_date( ).
      DATA: text type string.

      text = system_date.
      response->set_text( text ).
  endmethod.
ENDCLASS.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,您可以使用try-except语句来捕获超时异常,如下所示: ``` import google.auth from google.auth.transport.requests import Request from googleapiclient.errors import HttpError from googleapiclient.discovery import build # 创建服务对象 creds = None if creds and creds.valid: service = build('drive', 'v3', credentials=creds) else: creds = google.auth.default(scopes=['https://www.googleapis.com/auth/drive']) service = build('drive', 'v3', credentials=creds) try: file_metadata = {'name': 'test.txt'} media = MediaFileUpload('test.txt', mimetype='text/plain') create_params = {'fields': 'id'} create = service.files().create(body=file_metadata, media_body=media, **create_params) result = None while result is None: status, result = create.next_chunk() if status: print("上传进度: {}%".format(int(status.progress() * 100))) print('文件已上传,文件ID:{0}'.format(result.get('id'))) except HttpError as error: if error.resp.status == 429: print('请求频率过高,请稍后再试') elif error.resp.status == 500 or error.resp.status == 502 or error.resp.status == 503 or error.resp.status == 504: print('服务器错误,请稍后再试') else: print('An error occurred: %s' % error) except TimeoutError: print('请求超时,请稍后再试') ``` 在上面的代码中,我们使用try-except语句来包含文件上传代码,并使用HttpError来捕获其他异常,使用TimeoutError来捕获超时异常。如果发生超时异常,程序将打印出“请求超时,请稍后再试”的提示。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值