Skip to content

unbundle requests and use requests.get instead of urllib.urlretrieve#5138

Merged
jamalex merged 3 commits intolearningequality:0.16.xfrom
benjaoming:m2crypto-dl
Jun 7, 2016
Merged

unbundle requests and use requests.get instead of urllib.urlretrieve#5138
jamalex merged 3 commits intolearningequality:0.16.xfrom
benjaoming:m2crypto-dl

Conversation

@benjaoming
Copy link
Contributor

@benjaoming benjaoming commented Jun 6, 2016

Summary

Fixes issues with m2crypto not able to handle https connections.

TODO

If not all TODOs are marked, this PR is considered WIP (work in progress)

  • Have tests been written for the new code? If you're fixing a bug, write a regression test (or have a really good reason for not writing one... and I mean really good!)
  • Has documentation been written/updated?
  • New dependencies (if any) added to requirements file

Reviewer guidance

If there's any concern about including this PR in 0.16.6, please state it in the discussion, but I would caution that the old download method was probably less reliable than using the latest version of requests @jamalex

Issues addressed

#5126

@benjaoming benjaoming added the bug label Jun 6, 2016
@benjaoming benjaoming added this to the 0.16.6 milestone Jun 6, 2016
# (this is done in this file because putting it in __init__.py causes circular imports,
# and putting it in urls.py doesn't get loaded by management commands)
base_headers = requests.defaults.defaults["base_headers"]
base_headers["User-Agent"] = ("ka-lite/%s " % VERSION) + base_headers["User-Agent"]

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

@benjaoming
Copy link
Contributor Author

This is where the requests library is used currently, I don't see any reason of concern, but should we change the User-Agent on any of these requests @jamalex ?

kalite/distributed/static/js/distributed/perseus/ke/build/update_title_tags.py
9:import requests
13:    r = requests.get("http://www.khanacademy.org/api/internal/exercises")

kalite/distributed/static/js/distributed/perseus/ke/build/close-old-issues.py
10:import requests
39:        r = requests.get(url, auth=github_auth)
52:    r = requests.get("https://api.github.com/repos/Khan/khan-exercises/issues/"
65:    r = requests.post("https://api.github.com/repos/Khan/khan-exercises/"
73:    except requests.HTTPError:
83:    r = requests.post("https://api.github.com/repos/Khan/khan-exercises/"
91:    except requests.HTTPError:

kalite/i18n/management/commands/create_dummy_language_pack.py
13:import requests
57:        resp = requests.get(url)
59:    except requests.ConnectionError as e:

kalite/i18n/management/commands/setup_in_context_translations.py
5:import requests
40:        resp = requests.get(url)

kalite/i18n/tests/create_dummy_language_pack_tests.py
1:import requests
17:    @patch.object(requests, "get", autospec=True)

kalite/static/js/distributed/perseus/ke/build/update_title_tags.py
9:import requests
13:    r = requests.get("http://www.khanacademy.org/api/internal/exercises")

kalite/static/js/distributed/perseus/ke/build/close-old-issues.py
10:import requests
39:        r = requests.get(url, auth=github_auth)
52:    r = requests.get("https://api.github.com/repos/Khan/khan-exercises/issues/"
65:    r = requests.post("https://api.github.com/repos/Khan/khan-exercises/"
73:    except requests.HTTPError:
83:    r = requests.post("https://api.github.com/repos/Khan/khan-exercises/"
91:    except requests.HTTPError:

kalite/contentload/management/commands/unpack_assessment_zip.py
1:import requests
47:            r = requests.get(ziplocation, prefetch=False)

kalite/contentload/tests/unpack_assessment_zip.py
4:import requests
52:    @patch.object(requests, "get")
60:        self.assertEqual(get_method.call_count, 0, "requests.get was called even if we should've skipped!")
66:    @patch.object(requests, "get", autospec=True)

@benjaoming
Copy link
Contributor Author

Confirmed working on Raspberry Pi with m2crypto 0.21.1 installed as a dependency of ka-lite-raspberry-pi.


response = None
last_error = None
for retries in range(1, 1 + max_retries):

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

@jamalex
Copy link
Member

jamalex commented Jun 6, 2016

This is where the requests library is used currently, I don't see any reason of concern, but should we change the User-Agent on any of these requests @jamalex ?

Most of the ones you list there don't seem important (as they're not hitting our central server, so we won't be getting logs with the user agent in it for those), but that doesn't seem to be an exhaustive list. Here are some that are more important:
https://github.com/learningequality/ka-lite/blob/develop/python-packages/fle_utils/internet/functions.py#L22
https://github.com/learningequality/ka-lite/blob/develop/python-packages/securesync/api_client.py#L14
https://github.com/learningequality/ka-lite/blob/develop/python-packages/securesync/devices/views.py#L100
(I know these are under python-packages, but these aren't actually external python packages; that code is only used in the context of KA Lite)

Might be good to pull out this header calculation logic into a reusable function:
https://github.com/learningequality/ka-lite/pull/5138/files#diff-5e841025f58a948893e991da20eeebaaR64
(though not sure the best way to avoid having securesync and fle_utils depend on kalite...)

"Content-Length" not in response or
not str(len(open(thumb_filepath, "rb").read())) == response["Content-Length"]):
"content-length" not in response.headers or
not len(open(thumb_filepath, "rb").read()) == int(response.headers['content-length'])):

This comment was marked as spam.

This comment was marked as spam.

@benjaoming
Copy link
Contributor Author

(though not sure the best way to avoid having securesync and fle_utils depend on kalite...)

They already did when kalite was monkey-patching requests :)

@benjaoming
Copy link
Contributor Author

Thanks for searching through fle_utils btw, adding that change as well.

@jamalex
Copy link
Member

jamalex commented Jun 6, 2016

They already did when kalite was monkey-patching requests :)

Kind of soft dependency; implicit, and optional. They would still function fine without kalite. More like a "hook" style "dependency". :D

@jamalex
Copy link
Member

jamalex commented Jun 6, 2016

Thanks for searching through fle_utils btw, adding that change as well.

Cool -- there were a bunch of other uses of requests in there, but they didn't seem so critical. The ones I listed are the places that hit the central server in ways we may want to examine the logs for.

@benjaoming
Copy link
Contributor Author

They would still function fine without kalite.

If the intention was to have correct user agent stats, you would be failing but much more severely than a circular import, you'd have wrong stats and think they were right! So all this functionality really depends on the monkey patch firing.

@benjaoming
Copy link
Contributor Author

@jamalex done in 5884508

@jamalex
Copy link
Member

jamalex commented Jun 7, 2016

Ok, looks good to me! Merging now, but please make sure data syncing also still works (with a basic check -- registering and creating a local record and syncing it up, with no errors), in addition to the video downloading, as both depend on requests.

@jamalex jamalex merged commit 1164dc6 into learningequality:0.16.x Jun 7, 2016
@jamalex jamalex removed the has PR label Jun 7, 2016
@benjaoming
Copy link
Contributor Author

@jamalex

Tested:

  • One-click Device registration OK
  • Sync'ing OK
  • Video download OK

screenshot from 2016-06-08 13 43 24

@jamalex
Copy link
Member

jamalex commented Jun 8, 2016

💥 👍 ⚡

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants