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

Add multilanguage support. #17

Merged
merged 1 commit into from
Jun 29, 2019
Merged
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
9 changes: 8 additions & 1 deletion apixu/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ def __init__(self, message, code):


class ApixuClient:
def __init__(self, api_key=None, api_url=API_URL):
def __init__(self, api_key=None, api_url=API_URL, lang=None):
self.api_key = api_key
self.api_url = api_url.rstrip('/')
self.lang = lang

def _get(self, url, args=None):
new_args = {}
Expand Down Expand Up @@ -48,6 +49,8 @@ def current(self, q=None):
args = {}
if q:
args['q'] = q
if self.lang:
args['lang'] = self.lang

return self._get(url, args)

Expand All @@ -68,6 +71,8 @@ def forecast(self, q=None, days=None, hour=None):
args['days'] = days
if hour:
args['hour'] = hour
if self.lang:
args['lang'] = self.lang

return self._get(url, args)

Expand All @@ -84,5 +89,7 @@ def history(self, q=None, since=None, until=None):
if not isinstance(until, datetime.date):
raise ApixuException(message='"until" must be a date', code=0)
args['end_dt'] = until.strftime('%Y-%m-%d')
if self.lang:
args['lang'] = self.lang

return self._get(url, args)
3 changes: 2 additions & 1 deletion examples/current.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
from apixu.client import ApixuClient

api_key = os.environ['APIXUKEY']
client = ApixuClient(api_key)
client = ApixuClient(api_key=api_key, lang="fr")

current = client.current(q='London')

print(current['location']['name'])
print(current['location']['region'])

print(current['current']['last_updated_epoch'])
print(current['current']['condition']['text'])

'''
{
Expand Down
3 changes: 2 additions & 1 deletion examples/forecast.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
from apixu.client import ApixuClient

api_key = os.environ['APIXUKEY']
client = ApixuClient(api_key)
client = ApixuClient(api_key=api_key, lang="es")

forecast = client.forecast(q='London', days=2)

print(forecast['location']['name'])

print(forecast['current']['last_updated_epoch'])
print(forecast['current']['condition']['text'])

for day in forecast['forecast']['forecastday']:
print(day['date'])
Expand Down
1 change: 1 addition & 0 deletions examples/history.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
for day in history['forecast']['forecastday']:
print(day['date'])
print(day['day']['maxtemp_c'])
print(day['day']['condition']['text'])

'''
{
Expand Down