API for GET requests with automatic error handling 25/27525/11
authorJozef Behran <jbehran@cisco.com>
Mon, 28 Sep 2015 10:18:22 +0000 (12:18 +0200)
committerJozef Behran <jbehran@cisco.com>
Tue, 20 Oct 2015 13:32:43 +0000 (15:32 +0200)
The keyword "Get Data From URI" issues a GET request for the
specified URI through the specified session, FAILs if the
request returns an error (with logging that error into Robot
logs and returns the data on success. Success is currently
defined as "returned status code 200" and the method is
hardwired to GET so this keyword is not useful for making
non-data-getting requests like POST or for GET requests
that don't return any data and return an OK-like status code
(2XX where XX != 00).

Change-Id: Iee5e894687aa9576bc4968fe851d6ab72e386872
Signed-off-by: Jozef Behran <jbehran@cisco.com>
csit/libraries/Utils.robot

index 58f409046efdb312cdf9df3b54979af928e4a18d..89603cbc400055a44f17813d36f21f4cfa550e20 100644 (file)
@@ -308,3 +308,14 @@ Run Process With Logging And Status Check
     Log    ${result.stderr}
     Should Be Equal As Integers    ${result.rc}    0
     [Return]    ${result}
+
+Get Data From URI
+    [Arguments]    ${session}    ${uri}    ${headers}=${NONE}
+    [Documentation]    Issue a GET request and return the data obtained or on error log the error and fail.
+    ...    Issues a GET request for ${uri} in ${session} using headers from
+    ...    ${headers}. If the request returns a HTTP error, fails. Otherwise
+    ...    returns the data obtained by the request.
+    ${response}=    RequestsLibrary.Get    ${session}    ${uri}    ${headers}
+    Builtin.Return_From_Keyword_If    ${response.status_code} == 200    ${response.text}
+    Builtin.Log    ${response.text}
+    Builtin.Fail    The request failed with code ${response.status_code}