Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I created functions using the requests library #246

Open
necdetterkes opened this issue Nov 18, 2019 · 3 comments
Open

I created functions using the requests library #246

necdetterkes opened this issue Nov 18, 2019 · 3 comments

Comments

@necdetterkes
Copy link

Hello all,

I created some functions using the request library. So you can directly use this facility file in your projects.

`*** Settings ***
Library RequestsLibrary
Library HttpLibrary.HTTP
Library json
Resource ../variables/commonVariables.robot

*** Keywords ***
Create Service Session
Create Session session ${baseurl} verify=True
${headers} Create Dictionary Authorization=${apitoken} Content-Type=application/json
Set Global Variable ${headers}

GET Service Request
[Arguments] ${apiurl}
${response}= Get Request session ${apiurl} headers=${headers}
Set Global Variable ${response}

DELETE Service Request
[Arguments] ${apiurl}
${response}= Delete Request session ${apiurl}
Set Global Variable ${response}

Check Return Code
[Arguments] ${check_code}
Should Be Equal As Strings ${response.status_code} ${check_code}

Get Json Object
${json}= Json.Dumps ${response.json()}
Set Global Variable ${json}

Get Parameter Value
[Arguments] ${parameter} ${value}
Get Json Object
${parameter_value}= Get Json Value ${json} ${parameter}
Set Global Variable ${parameter_value}

Check Parameter Value is Null
[Arguments] ${parameter}
Get Parameter Value ${parameter} 0
Should Not Be Empty ${parameter_value}

Check Parameter Value is Correct
[Arguments] ${parameter} ${value}
Get Parameter Value ${parameter} ${value}
Should Be Equal ${parameter_value} ${value}

`

@necdetterkes
Copy link
Author

I added the new one

*** Settings ***
Library RequestsLibrary
Library HttpLibrary.HTTP
Library Collections
Library json
Resource ../variables/commonVariables.robot

*** Keywords ***

#REQUEST FUNCTIONS
Create Service Session
Create Session session ${baseurl} verify=True
${headers} Create Dictionary Authorization=${apitoken} Content-Type=application/json
Set Global Variable ${headers}

GET Service Request
[Arguments] ${apiurl}
${response}= Get Request session ${apiurl} headers=${headers}
Set Global Variable ${response}

POST Service Request
[Arguments] ${apiurl} &{body}
${data}= Create Dictionary &{body}
${response}= Post Request session ${apiurl} headers=${headers} json=${data}
Set Global Variable ${response}

PUT Service Request
[Arguments] ${apiurl} &{body}
${data}= Create Dictionary &{body}
${response}= Put Request session ${apiurl} headers=${headers} json=${data}
Set Global Variable ${response}

PATCH Service Request
[Arguments] ${apiurl} &{body}
${data}= Create Dictionary &{body}
${response}= Patch Request session ${apiurl} headers=${headers} json=${data}
Set Global Variable ${response}

HEAD Service Request
[Arguments] ${apiurl}
${response}= Head Request session ${apiurl} headers=${headers}
Set Global Variable ${response}

DELETE Service Request
[Arguments] ${apiurl}
${response}= Delete Request session ${apiurl}
Set Global Variable ${response}

#RESPONSE FUNCTIONS
Get Json Object
${json}= Json.Dumps ${response.json()}
Set Global Variable ${json}

Get Parameter Value
[Arguments] ${parameter} ${value}
Get Json Object
${parameter_value}= Get Json Value ${json} ${parameter}
Set Global Variable ${parameter_value}

Check Parameter Value is Null
[Arguments] ${parameter}
Get Parameter Value ${parameter} 0
Should Not Be Empty ${parameter_value}

Check Parameter Value is Correct
[Arguments] ${parameter} ${value}
Get Parameter Value ${parameter} ${value}
Should Be Equal ${parameter_value} ${value}

Check Return Code
[Arguments] ${check_code}
Should Be Equal As Strings ${response.status_code} ${check_code}

@lucagiove
Copy link
Member

Look nice what about writing this as part of the library documentation?
Maybe some example how to do basic operations with this Library?

@necdetterkes
Copy link
Author

*** Settings ***
Library RequestsLibrary
Library HttpLibrary.HTTP
Library Collections
Library json
Resource ../variables/commonVariables.robot
Resource ../variables/displayVariables.robot

*** Keywords ***

#REQUEST FUNCTIONS
Create Service Session
Create Session session ${baseurl} verify=True
${headers} Create Dictionary Authorization=Bearer ${apitoken} Content-Type=application/json
Set Global Variable ${headers}

Create Service Session With Token
Create Session tokensession ${baseurl} verify=True
${tokenheaders} Create Dictionary Content-Type=application/json
${tokendata}= Create Dictionary &{token_body}
${response}= Post Request tokensession ${token_url} headers=${tokenheaders} json=${tokendata}
${json}= Json.Dumps ${response.json()}
${token_value_with_quots}= Get Json Value ${json} /token
Set Global Variable ${token_value_with_quots}
${token_value}= Remove String ${token_value_with_quots} "
#Create Service Session
Create Session session ${baseurl} verify=True
${headers} Create Dictionary Authorization=Bearer ${token_value} Content-Type=application/json
Set Global Variable ${headers}

GET Service Request
[Arguments] ${apiurl}
${response}= Get Request session ${apiurl} headers=${headers}
Set Global Variable ${response}

POST Service Request
[Arguments] ${apiurl} &{body}
${data}= Create Dictionary &{body}
${response}= Post Request session ${apiurl} headers=${headers} json=${data}
Set Global Variable ${response}

PUT Service Request
[Arguments] ${apiurl} &{body}
${data}= Create Dictionary &{body}
${response}= Put Request session ${apiurl} headers=${headers} json=${data}
Set Global Variable ${response}

PATCH Service Request
[Arguments] ${apiurl} &{body}
${data}= Create Dictionary &{body}
${response}= Patch Request session ${apiurl} headers=${headers} json=${data}
Set Global Variable ${response}

HEAD Service Request
[Arguments] ${apiurl}
${response}= Head Request session ${apiurl} headers=${headers}
Set Global Variable ${response}

DELETE Service Request
[Arguments] ${apiurl}
${response}= Delete Request session ${apiurl} headers=${headers}
Set Global Variable ${response}

#RESPONSE FUNCTIONS
Get Json Object
${json}= Json.Dumps ${response.json()}
Set Global Variable ${json}

Get Parameter Value
[Arguments] ${parameter} ${value}
Get Json Object
${parameter_value}= Get Json Value ${json} ${parameter}
Set Global Variable ${parameter_value}

Check Parameter Value is Null
[Arguments] ${parameter}
Get Parameter Value ${parameter} 0
Should Not Be Empty ${parameter_value}

Check Parameter Value is Correct
[Arguments] ${parameter} ${value}
Get Parameter Value ${parameter} ${value}
Should Be Equal ${parameter_value} ${value}

Check Return Code
[Arguments] ${check_code}
Should Be Equal As Strings ${response.status_code} ${check_code}
Log To Console Return Code Check OK (Return code = ${response.status_code})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants