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

Update of pycharts #15

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
31 changes: 8 additions & 23 deletions pycharts/base.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
import datetime
import json
from pycharts import exceptions
try:
# Python 3
from urllib.parse import urlencode
from urllib.error import HTTPError
from urllib.request import Request, urlopen
except ImportError:
# Python2
from urllib import urlencode
from urllib2 import HTTPError, Request, urlopen

import requests

class BaseSecurityClient(object):
"""
Expand All @@ -20,7 +12,7 @@ class BaseSecurityClient(object):
"""

API_VERSION = 'v3'
BASE_URL = 'https://ycharts.com/api'
BASE_URL = 'https://api.ycharts.com/'
SECURITY_TYPE_PATH = None
VALID_SECURITY_FILTERS = None

Expand Down Expand Up @@ -151,20 +143,10 @@ def get_info(self, security_symbols, info_field_codes):
# Private Helper Methods
def _get_data(self, url_path, params=None):
url = '{0}/{1}/{2}'.format(self.BASE_URL, self.API_VERSION, url_path)
if params:
encoded_params = urlencode(params)
url = '{0}?{1}'.format(url, encoded_params)

url = url.replace(' ', '')
req = Request(url, headers=self.header)
response = self._parse_response(req)

return response

def _parse_response(self, req):
try:
response = urlopen(req).read().decode('utf-8')
except HTTPError as http_error:
response = requests.get(url, params=params, headers=self.header)
except requests.HTTPError as http_error:
if http_error.code == 404:
raise exceptions.PyChartsRequestUrlNotFoundException()
elif http_error.code == 401:
Expand All @@ -174,7 +156,10 @@ def _parse_response(self, req):
else:
raise

parsed_rsp = json.loads(response)
if response.status_code == 403:
raise exceptions.PyChartsRequestForbiddenException()
parsed_rsp = response.json()

# raise any payload level errors
if parsed_rsp['meta']['status'] == 'error':
error_code = parsed_rsp['meta']['error_code']
Expand Down
6 changes: 6 additions & 0 deletions pycharts/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,9 @@ class PyChartsRequestTooLongException(PyChartsRequestException):
def __init__(self, error_message=None):
self.error_message = error_message
self.error_code = 414

class PyChartsRequestForbiddenException(PyChartsRequestException):

def __init__(self, error_message=None):
self.error_message = "The server refuses to authorize the request."
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
self.error_message = "The server refuses to authorize the request."
self.error_message = 'The server refuses to authorize the request.'

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for pointing this out, this is due to the fact that we used requests instead of urllib (which we have done for caching purposes). I would have to rewrite the tests file in order to mock requests rather than mocking urlib's urlopen function, are you okay with that?

Copy link
Contributor

Choose a reason for hiding this comment

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

@VivienCabannes Yeah that would be much appreciated as we no longer need to support 2.7

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for your quick answers, I have looked a little bit about it, but that would take me too much time to implement the changes (about 90 minutes I believe), and as I am busy those days, I am afraid I will not be able to work on it at the moment.
I suggests two solutions:

  • either you only merge my commit "57385c5", and you update the url in the test file accordingly.
  • either you find someone to reimplement the unit test by mocking requests rather than urllib.
    I hope this help, sorry I do not have some much time to work on it.
    I might come back on it later if no one has fixed it.

Best regards
Vivien

self.error_code = 403