Created
May 17, 2012 15:17
-
-
Save qmacro/2719589 to your computer and use it in GitHub Desktop.
ABAP wrapper for http://requestb.in in a 'release early release often' stylee
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class YREQUESTBIN definition | |
public | |
final | |
create public . | |
public section. | |
*"* public components of class YREQUESTBIN | |
*"* do not include other source files here!!! | |
data NAME type STRING . | |
constants BASEURL type STRING value 'http://requestb.in/'. "#EC NOTEXT | |
methods CONSTRUCTOR | |
importing | |
!NAME type STRING optional . | |
methods LOG | |
importing | |
!STRING type STRING . | |
PROTECTED SECTION. | |
*"* protected components of class YREQUESTBIN | |
*"* do not include other source files here!!! | |
private section. | |
*"* private components of class YREQUESTBIN | |
*"* do not include other source files here!!! | |
types: | |
BEGIN OF s_parms | |
, private TYPE string | |
, END OF s_parms . | |
data CLIENT type ref to IF_HTTP_CLIENT . | |
type-pools ABAP . | |
class-methods CREATE | |
importing | |
!PRIVATE type ABAP_BOOL default ABAP_TRUE | |
returning | |
value(NAME) type STRING . | |
ENDCLASS. | |
CLASS YREQUESTBIN IMPLEMENTATION. | |
* <SIGNATURE>---------------------------------------------------------------------------------------+ | |
* | Instance Public Method YREQUESTBIN->CONSTRUCTOR | |
* +-------------------------------------------------------------------------------------------------+ | |
* | [--->] NAME TYPE STRING(optional) | |
* +--------------------------------------------------------------------------------------</SIGNATURE> | |
METHOD constructor. | |
* Create a new bin if required | |
me->name = name. | |
IF me->name IS INITIAL. | |
me->name = create( ). | |
ENDIF. | |
CALL METHOD cl_http_client=>create_by_url | |
EXPORTING | |
url = baseurl && me->name | |
IMPORTING | |
client = client. | |
client->request->set_method( 'POST' ). | |
ENDMETHOD. "CONSTRUCTOR | |
* <SIGNATURE>---------------------------------------------------------------------------------------+ | |
* | Static Private Method YREQUESTBIN=>CREATE | |
* +-------------------------------------------------------------------------------------------------+ | |
* | [--->] PRIVATE TYPE ABAP_BOOL (default =ABAP_TRUE) | |
* | [<-()] NAME TYPE STRING | |
* +--------------------------------------------------------------------------------------</SIGNATURE> | |
METHOD create. | |
DATA client TYPE REF TO if_http_client. | |
DATA parms TYPE s_parms. | |
DATA jsondoc TYPE REF TO zcl_json_document. | |
DATA payload TYPE string. | |
* Private flag? | |
IF private EQ abap_true. | |
parms-private = 'true'. | |
ELSE. | |
parms-private = 'false'. | |
ENDIF. | |
CALL METHOD cl_http_client=>create_by_url | |
EXPORTING | |
url = baseurl && 'api/v1/bins' | |
IMPORTING | |
client = client. | |
client->request->set_cdata( 'private=' && parms-private ). | |
client->request->set_method( 'POST' ). | |
client->send( ). | |
client->receive( ). | |
jsondoc = zcl_json_document=>create_with_json( client->response->get_cdata( ) ). | |
name = jsondoc->get_value( 'name' ). | |
ENDMETHOD. "CREATE | |
* <SIGNATURE>---------------------------------------------------------------------------------------+ | |
* | Instance Public Method YREQUESTBIN->LOG | |
* +-------------------------------------------------------------------------------------------------+ | |
* | [--->] STRING TYPE STRING | |
* +--------------------------------------------------------------------------------------</SIGNATURE> | |
METHOD log. | |
client->request->set_cdata( string ). | |
client->send( ). | |
client->receive( ). | |
ENDMETHOD. "LOG | |
ENDCLASS. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
Alternative creation of wrapper instance for an already existing bin:
You can of course use the fab ZJSON library to dump any amount of structured data to the request bin.