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

POST request to an API #111

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@
help="Make a GET request to an API",
action='store_true')

parser.add_argument("-POST",
help="Make a POST request to an API",
action='store_true')

parser.add_argument("-DELETE",
help = "Make a DELETE request to an API",
action = 'store_true')
Expand Down
48 changes: 47 additions & 1 deletion src/arguments/api_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,39 @@ def fetch_input_url(cls):
"request_url" : request_url,
"request_headers" : request_headers,
}

@classmethod
def read_data_from_file(cls):
filename = input("Enter a filename (response_data.json)")
if filename == '':
print("filename empty, so default file (response_data.json) is used ")
lainq marked this conversation as resolved.
Show resolved Hide resolved
lainq marked this conversation as resolved.
Show resolved Hide resolved
with open(filename, "r") as reader:
file_content = reader.read()
try:
json_data = json.loads(file_content)
data = json_data.get("data")
return data
# Make sure the data is not None and send
except json.JSONDecodeError:
print("Unable to parse the file, Please try again")
cls.read_data_from_file()
@classmethod
def fetch_payload_data(cls):
store = int(input("Please choose the below options? (1/2/3)"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a option here ?
Do you want to send payload Y/N
Then we can display option like send a payload from terminal or file

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

then there would be multiple conditions

   if yes :
       enter 1 to send from terminal
       enter 2 to read from terminal
       any other number #then again we have to repeat from starting line, if you want to send payload
   elif no 
       return 
   else
          recur the function     

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we also send bearer token in this payload ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah if the user sends it

print("Option 1: For sending data payload from terminal\n")
print("Option 2: For sending data payload by reading from json file\n")
print("Option 3: For not sending any data to POST request")
data = None
if(store == 1):
data = input("Enter data as key value pairs")
data = json.loads(data)
lainq marked this conversation as resolved.
Show resolved Hide resolved
elif(store == 2):
data = cls.read_data_from_file()
elif(store == 3):
print(f"you have entered {store}, so sending POST request without any data")
else:
print(f"You have entered {store}, please enter from the above options")
cls.fetch_payload_data()
return data
#saves the json response into a file
@classmethod
def save_response_data(cls,response_data):
Expand Down Expand Up @@ -84,6 +116,20 @@ def get_request(cls):
print(cls.invalid_schema_message)
except Exception as exception_obj:
print(exception_obj)
#Make a POST request
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add 2 space above. Follow pep8 formatting please

@classmethod
def post_request(cls):
request_data = cls.fetch_input_url()
data = cls.fetch_payload_data()
try:
response= requests.post(url = request_data["request_url"], headers= request_data["request_headers"], data = data)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a space before = (equals)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

everything can be done, using black formatter I guess, shall we try adding it

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, you can use the black formatter locally on your system before pushing it 😃

cls.print_response_json(response)
response_data = json.loads(response.content)
cls.save_response_data(response_data)
except requests.exceptions.InvalidSchema:
print(cls.invalid_schema_message)
except Exception as exception_obj:
print(exception_obj)
# Make a delete request
@classmethod
def delete_request(cls):
Expand Down
2 changes: 2 additions & 0 deletions src/arguments/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ def search_args(self):
update.check_for_updates()
elif self.arguments.GET:
self.api_test_object.get_request()
elif self.arguments.POST:
self.api_test_object.post_request()
elif self.arguments.DELETE:
self.api_test_object.delete_request()
elif self.arguments.notion:
Expand Down