Skip to content

Instantly share code, notes, and snippets.

@qmacro
Created May 17, 2012 15:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qmacro/2719589 to your computer and use it in GitHub Desktop.
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
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.
@qmacro
Copy link
Author

qmacro commented May 17, 2012

Usage:

data rb type ref to yrequestbin.
create object rb.
write: / 'Bin identifier is', rb->name.
rb->log( 'Hello, world!' ).
rb->log( 'Something else' ).

Alternative creation of wrapper instance for an already existing bin:

create object rb exporting name = 'abdefghi'.

You can of course use the fab ZJSON library to dump any amount of structured data to the request bin.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment