adrian-soto / mock-request

Mock an API request and obtain a pre-saved response object
78 stars 27 forks source link

Accomodate headers #13

Open alexyarosh opened 4 years ago

alexyarosh commented 4 years ago

Hi @adrian-soto ,

Lack of header checks makes ch4ex5-12 in "Streamlined data ingestion with pandas" fail. Here's an example, based on ex 4.5:

Here's the data used in the exercise (I think you should be able to access the direct links, lmk if it doesn't work!):

exp_path = "https://assets.datacamp.com/production/repositories/4412/datasets/6960241321d7d91fbfe16f74d3216b58074427f9/nyc_cafes.pkl"
avr_path = "https://assets.datacamp.com/production/repositories/4412/datasets/67a5ee3539b2916df0e6ebc63216c6f396693137/available_requests.dat"
err_path = "https://assets.datacamp.com/production/repositories/4412/datasets/cfc89bc158082f77e30cfa230890bc60b2e68df5/404_error.pkl"
erp_path = "https://assets.datacamp.com/production/repositories/4412/datasets/aaf775650bf3386e156f27c8b0198fcc94b4d9f4/request_errors.dat"

In particular, available_requests.dat looks like this: image


Now, the pre-exercise_code + exercise pretty much goes:

requests = MockRequests("available_requests.dat", "request_errors.dat")

headers = {"Authorization": "Bearer {}".format(api_key)}
params = {"term": "cafe",  "location": "NYC"}

response = requests.get(api_url, headers=headers, params=params)

When a request is created in L94-L112 , it ignores the headers information passed to .get() and only uses the params information.

So, in our case, a request created in L94-L112 would only have the base_url, term, and location fields, and would not have the Authorization field.

But that doesn't match the format of data in the original available_requests.dat, so in the end, the check on L115 doesn't pass image

adrian-soto commented 4 years ago

Hey @alexyarosh, thanks a lot for opening this issue, it's super well documented!

I believe the best solution to this problem would be to eliminate the "Authorization" field from the dictionaries contained in available_requests.dat. Doing so should make the exercise pass.

Please make sure to rename and upload this file while testing, because if you overwrite it it will cause trouble in the live course! My suggestion is to rename it to available_requests.json, in agreement with the data format and with the package examples.

Please let me know if you still have issues after this attempt! :)