Feb-11-2023, 05:39 PM
Hi guys! Currently trying to code my first program to automate an API, but got stuck at the moment where I transfer 2 arguments from earlier functions to the 3-rd function.
BTW, feel free to leave any suggestions for the whole code, apart for the error.
Here I'm authenticating and extract the cookie token:
Then I create a booking and return its id in the str form:
Finally, I wanna pass the return values from the 2 previous functions into the function below. To do that, I saved the return values in the variables (booker_auth_method_var, booker_create_booking_method_var) and pass them as arguments into my next function.
And that's where code goes crazy and I get the following output:
/Users/.../python /Users/evgeniy/PycharmProjects/booker_automation/workfiles/booker_http_methods.py
https://restful-booker.herokuapp.com/auth
{'Cookie': 'token=366401ff5eabf91'}
https://restful-booker.herokuapp.com/booking
1820 <Response [200]>
My another python file "general_http_methods" which I import to the file with the error contains the following:
Please, help. This mistake kind freaks me out cos I don't understand neither its reasons nor the way to fix. I've tried playing with accessing the key of the dictionary from the 2nd method in different ways, with converting the booking id into str differently, but all in all I get this mistake.
And I simply see no workaround since this code is already as simple as possible, I do everything step by step...
BTW, feel free to leave any suggestions for the whole code, apart for the error.
Here I'm authenticating and extract the cookie token:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
"""HTTP-methods for booker API""" import requests import json from workfiles import general_http_methods """Authentication""" def booker_auth_method(): auth_resource = '/auth' auth_url = base_url + auth_resource print (auth_url) auth_body = { 'username' : 'admin' , 'password' : 'password123' } result_auth_token = general_http_methods.general_post_method(auth_url, auth_body) json_result_auth_token = result_auth_token.json() value_of_the_cookie_json_result_auth_token = str (json_result_auth_token.get( 'token' )) adding_token_to_auth_token_value = 'token=' + value_of_the_cookie_json_result_auth_token result_auth_cookie_token = str ({ 'Cookie' : adding_token_to_auth_token_value}) print (result_auth_cookie_token) return result_auth_cookie_token booker_auth_method_var = booker_auth_method() |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
"""Creating a booking""" def booker_create_booking_method(): create_booking_resource = '/booking' create_booking_body = { "firstname" : 'Python' , "lastname" : "Tester" , "totalprice" : 111 , "depositpaid" : True , "bookingdates" : { "checkin" : "2023-01-01" , "checkout" : "2023-01-02" }, "additionalneeds" : "Breakfast" } create_booking_url = base_url + create_booking_resource print (create_booking_url) create_booking_result = general_http_methods.general_post_method(create_booking_url, create_booking_body) str_json_create_booking_result = str (create_booking_result.json()[ 'bookingid' ]) print (str_json_create_booking_result) return str_json_create_booking_result booker_create_booking_method_var = booker_create_booking_method() |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
"Updating the WHOLE booking" def booker_update_booking_method(booker_auth_method_var, booker_create_booking_method_var): update_booking_resource = '/booking/' update_booking_url = base_url + update_booking_resource + booker_create_booking_method_var update_booking_body = { "firstname" : "Python" , "lastname" : "QA Engineer" , "totalprice" : 777 , "depositpaid" : False , "bookingdates" : { "checkin" : "2023-07-08" , "checkout" : "2023-07-15" }, "additionalneeds" : "ALL INCLUSIVE" } update_booking_result = general_http_methods.general_put_method(update_booking_url, booker_auth_method_var, update_booking_body) print (booker_auth_method_var, update_booking_url, update_booking_result.text) return update_booking_result booker_update_booking_method(booker_auth_method_var, booker_create_booking_method_var) |
/Users/.../python /Users/evgeniy/PycharmProjects/booker_automation/workfiles/booker_http_methods.py
https://restful-booker.herokuapp.com/auth
{'Cookie': 'token=366401ff5eabf91'}
https://restful-booker.herokuapp.com/booking
1820 <Response [200]>
Error:Traceback (most recent call last):
File "/Users/.../booker_http_methods.py", line 73, in <module>
booker_update_booking_method(booker_auth_method_var, booker_create_booking_method_var)
File "/Users/.../booker_http_methods.py", line 68, in booker_update_booking_method
update_booking_result = general_http_methods.general_put_method(update_booking_url, booker_auth_method_var, update_booking_body)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/.../general_http_methods.py", line 20, in general_put_method
result = requests.put(url, headers=headers, cookies=cookies, json=body)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/.../api.py", line 130, in put
return request("put", url, data=data, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/.../api.py", line 59, in request
return session.request(method=method, url=url, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/.../sessions.py", line 573, in request
prep = self.prepare_request(req)
^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/.../sessions.py", line 471, in prepare_request
cookies = cookiejar_from_dict(cookies)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/.../cookies.py", line 537, in cookiejar_from_dict
cookiejar.set_cookie(create_cookie(name, cookie_dict[name]))
~~~~~~~~~~~^^^^^^
TypeError: string indices must be integers, not 'str'
Process finished with exit code 1My another python file "general_http_methods" which I import to the file with the error contains the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
"""General HTTP-methods""" import requests headers = { 'Content-Type' : 'application/json' , 'Accept' : 'application/json' } def general_get_meothod(url): result = requests.get(url) return result def general_post_method(url, body): result = requests.post(url, headers = headers, json = body) return result def general_put_method(url, cookies, body): result = requests.put(url, headers = headers, cookies = cookies, json = body) return result def general_patch_method(url, cookies, body): result = requests.patch(url, headers = headers, cookies = cookies, json = body) return result def general_delete_method(url, cookies, body): result = requests.patch(url, headers = headers, cookies = cookies, json = body) return result |
And I simply see no workaround since this code is already as simple as possible, I do everything step by step...