-
Notifications
You must be signed in to change notification settings - Fork 52
Feature/astronomy #142
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
Merged
yammesicka
merged 32 commits into
PythonFreeCourse:develop
from
hadaskedar2020:feature/astronomy
Feb 6, 2021
Merged
Feature/astronomy #142
Changes from all commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
8f90c0c
feat: astronomy
hadaskedar2020 ea000db
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
hadaskedar2020 2f62fe0
feat: astronomy - async support & fixes
hadaskedar2020 e4bd164
feat: astronomy - async support & fixes
hadaskedar2020 73e68b0
feat: astronomy - async support & fixes
hadaskedar2020 41f3502
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
hadaskedar2020 ec19dd0
feat: astronomy - async support & fixes
hadaskedar2020 c62261c
feat: astronomy - async support & fixes
hadaskedar2020 0ad4344
feat: astronomy - async support & fixes
hadaskedar2020 692f871
feat: astronomy - async support & fixes
hadaskedar2020 7c07cc7
feat: astronomy - async support & fixes
hadaskedar2020 6c4077a
feat: astronomy - async support & fixes
hadaskedar2020 64e49c4
feat: astronomy - async support & fixes
hadaskedar2020 69a722b
feat: astronomy - async support & fixes
hadaskedar2020 7a7fc61
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
hadaskedar2020 d7889be
Merge branch 'develop' into feature/astronomy
hadaskedar2020 0c03aa7
feat: astronomy - async support & fixes
hadaskedar2020 b748f2e
feat: astronomy - async support & fixes
hadaskedar2020 09c110c
Merge remote-tracking branch 'origin/feature/astronomy' into feature/…
hadaskedar2020 220eab7
feat: astronomy - async support & fixes
hadaskedar2020 fe9238c
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
hadaskedar2020 506a378
Merge branch 'develop' into feature/astronomy
hadaskedar2020 588c7f9
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
hadaskedar2020 9bbce1a
feat: astronomy - async support & fixes
hadaskedar2020 c3264a1
feat: astronomy - async support & fixes
hadaskedar2020 4542974
feat: astronomy - async support & fixes
hadaskedar2020 b11d6db
feat: astronomy - async support & fixes
hadaskedar2020 56a5147
feat: astronomy - async support & fixes
hadaskedar2020 9ba6abd
Merge branch 'develop' into feature/astronomy
yammesicka dd22131
feat: astronomy - async support & fixes
hadaskedar2020 7674326
feat: astronomy - async support & fixes
hadaskedar2020 39d91e5
feat: astronomy - async support & fixes
hadaskedar2020 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import datetime | ||
import functools | ||
import httpx | ||
from typing import Dict | ||
|
||
from app import config | ||
|
||
|
||
# This feature requires an API KEY - get yours free @ www.weatherapi.com | ||
|
||
ASTRONOMY_URL = "http://api.weatherapi.com/v1/astronomy.json" | ||
NO_API_RESPONSE = "No response from server" | ||
|
||
|
||
@functools.lru_cache(maxsize=128, typed=False) | ||
async def get_data_from_api(formatted_date: str, location: str)\ | ||
-> Dict[str, int]: | ||
""" get the relevant astronomical data by calling the "weather api" API. | ||
Args: | ||
formatted_date (date) - relevant date. | ||
location (str) - location name. | ||
Returns: | ||
response_json (json dict) including: | ||
relevant part (data / error) of the JSON returned by the API. | ||
Success (bool) | ||
ErrorDescription (str) - error message. | ||
""" | ||
input_query_string = {'key': config.ASTRONOMY_API_KEY, 'q': location, | ||
'dt': formatted_date} | ||
output = {} | ||
try: | ||
async with httpx.AsyncClient() as client: | ||
response = await client.get(ASTRONOMY_URL, | ||
params=input_query_string) | ||
except httpx.HTTPError: | ||
output["Success"] = False | ||
output["ErrorDescription"] = NO_API_RESPONSE | ||
return output | ||
if response.status_code != httpx.codes.OK: | ||
output["Success"] = False | ||
output["ErrorDescription"] = NO_API_RESPONSE | ||
return output | ||
output["Success"] = True | ||
try: | ||
output.update(response.json()['location']) | ||
return output | ||
except KeyError: | ||
output["Success"] = False | ||
output["ErrorDescription"] = response.json()['error']['message'] | ||
return output | ||
|
||
|
||
async def get_astronomical_data(requested_date: datetime.datetime, | ||
location: str) -> Dict[str, int]: | ||
""" get astronomical data (Sun & Moon) for date & location - | ||
main function. | ||
Args: | ||
requested_date (date) - date requested for astronomical data. | ||
location (str) - location name. | ||
Returns: dictionary with the following entries: | ||
Status - success / failure. | ||
ErrorDescription - error description (relevant only in case of error). | ||
location - relevant location values(relevant only in case of success). | ||
name, region, country, lat, lon etc. | ||
astronomy - relevant astronomy values, all time in local time - | ||
(relevant only in case of success): | ||
sunrise, sunset, moonrise, moonset, moon_phase, moon_illumination. | ||
""" | ||
formatted_date = requested_date.strftime('%Y-%m-%d') | ||
return await get_data_from_api(formatted_date, location) |
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import datetime | ||
import httpx | ||
import pytest | ||
import requests | ||
import responses | ||
import respx | ||
|
||
|
||
from app.internal.astronomy import get_astronomical_data | ||
from app.internal.astronomy import ASTRONOMY_URL | ||
|
||
RESPONSE_FROM_MOCK = {"location": { | ||
"name": "Tel Aviv-Yafo", | ||
"region": "Tel Aviv", | ||
"country": "Israel", | ||
"lat": 32.07, | ||
"lon": 34.76, | ||
"tz_id": "Asia/Jerusalem", | ||
"localtime_epoch": 1611399607, | ||
"localtime": "2021-01-23 13:00" | ||
}, | ||
"astronomy": { | ||
"astro": { | ||
"sunrise": "05:25 AM", | ||
"sunset": "06:03 PM", | ||
"moonrise": "01:56 PM", | ||
"moonset": "03:04 AM", | ||
"moon_phase": "Waxing Gibbous", | ||
"moon_illumination": "79" | ||
} | ||
} | ||
} | ||
ERROR_RESPONSE_FROM_MOCK = {"error": {"message": "Error Text"}} | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_get_astronomical_data(httpx_mock): | ||
requested_date = datetime.datetime(day=4, month=4, year=2020) | ||
httpx_mock.add_response(method="GET", json=RESPONSE_FROM_MOCK) | ||
output = await get_astronomical_data(requested_date, "tel aviv") | ||
assert output['Success'] | ||
|
||
|
||
@respx.mock | ||
@pytest.mark.asyncio | ||
async def test_astronomical_data_error_from_api(): | ||
requested_date = datetime.datetime(day=4, month=4, year=2021) | ||
route = respx.get(ASTRONOMY_URL) | ||
route.return_value = httpx.Response(200, json=ERROR_RESPONSE_FROM_MOCK) | ||
output = await get_astronomical_data(requested_date, "123") | ||
assert not output['Success'] | ||
|
||
|
||
@respx.mock | ||
@pytest.mark.asyncio | ||
async def test_astronomical_exception_from_api(httpx_mock): | ||
requested_date = datetime.datetime.now() + datetime.timedelta(days=3) | ||
respx.get(ASTRONOMY_URL).mock(return_value=httpx.Response(500)) | ||
output = await get_astronomical_data(requested_date, "456") | ||
assert not output['Success'] | ||
|
||
|
||
@responses.activate | ||
@pytest.mark.asyncio | ||
async def test_astronomical_no_response_from_api(): | ||
requested_date = datetime.datetime(day=11, month=1, year=2020) | ||
responses.add(responses.GET, ASTRONOMY_URL, status=500) | ||
requests.get(ASTRONOMY_URL) | ||
output = await get_astronomical_data(requested_date, "789") | ||
assert not output['Success'] |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.