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 patent.py, test_patent.py and config.py #17

Open
wants to merge 9 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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,6 @@ ENV/

# Rope project settings
.ropeproject

#OSX stuff
.DS_Store
33 changes: 33 additions & 0 deletions pybdot.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
[Global]
api_key = aApglMV0QO74TpuZACRnSUvGel8ayxf7Z067Nnsa

[PATENT]
url = https://api.nasa.gov/patents/content

[APOD]
url = https://api.nasa.gov/planetary/apod

[NEO]
url = https://api.nasa.gov/neo/rest/v1

[EPIC]
url = https://api.nasa.gov/EPIC/api

[IMAGERY]
url = https://api.nasa.gov/planetary/earth/imagery

[ASSETS]
url = https://api.nasa.gov/planetary/earth/assets

[IMAGES]
url = https://images-api.nasa.gov

[ROVERS]
url = https://api.nasa.gov/mars-photos/api/v1/rovers

[SOUNDS]
url = https://api.nasa.gov/planetary/sounds

[TECHPORT]
url = https://api.nasa.gov/planetary/sounds

1 change: 1 addition & 0 deletions pybluedot/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .api.patent import Patent
Empty file added pybluedot/api/__init__.py
Empty file.
38 changes: 38 additions & 0 deletions pybluedot/api/apod.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
class Apod(object):
"""A API to return NASA's astronomy picture of the day.

Args:
date (str, optional): The date of the APOD image to retrieve.
format YYYY-MM-DD
Default is today's date.
dh (bool, optional): Retrieve url for for high res image.
api_key (str, required): api.nasa.gov key for expanded usage.
Default is DEMO_KEY in config.py.
"""

def __init__(self, date=None, api_key=config.API_KEY, hd=False):
self.date = date
self.api_key = api_key
self.hd = hd

def get(self):
"""Builds parameter dict that requests library
will use for url query.
"""
self.patent_params = {'date': self.date,
'api_key': self.api_key,
'hd': self.hd}

response = requests.get(config.APOD_URL, params=self.patent_params)

try:
response.raise_for_status()

except HTTPError as e:
raise HTTPError(str(e))

body = response.json()

if 'error' in body:
raise ValueError(body['error'])
return body
45 changes: 45 additions & 0 deletions pybluedot/api/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from .. import pybdotconfig
import json
import requests

class NasaApiException(Exception):
"""Raised for any exception caused by a call to the Nasa API"""

def get_api_key():
config = pybdotconfig.init_config()
config.read('pybdot.ini')
api_key = config['Global']['API_KEY']
return api_key

def get_url(api):
"""Grabs URL from pybdot.ini for the corresponding API."""
config = pybdotconfig.init_config()
config.read('pybdot.ini')
url = config[api]['url']
return url

def api_get(url, payload):
payload = dict((k, v) for k, v in payload.items() if v)
payload['api_key'] = get_api_key()
response = requests.get(url, params=payload)
response.raise_for_status()
body = response.json()

if 'error' in body:
raise NasaApiException(body['error'])

return body


class Pybdot(object):
"""Default base class, all api classes will inherit from this"""

def __init__(self, **kwargs):
pass







33 changes: 33 additions & 0 deletions pybluedot/api/patent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from . import base
from pprint import pprint

class Patent(object):
"""A API to search NASA's patent repository.

Args:
query (str, required): Search text to filter results.
api_key (str, required): api.nasa.gov key for expanded usage.
concept_tags (str, optional): Return an ordered dict of concepts from
the patent abstract. Default is False.
limit (int, optional): Number of patents to return. Default is None.
"""

def __init__(self, query, concept_tags=None, limit=None):
self.query = query
self.concept_tags = concept_tags
self.limit = limit
self.url = base.get_url('PATENT')

def get(self):
self.payload = {
'query' : self.query,
'concept_tags' : self.concept_tags,
'limit' : self.limit,
}
self.response = base.api_get(self.url, self.payload)
return self.response

def __repr__(self):
return pprint(self.response)


41 changes: 0 additions & 41 deletions pybluedot/commands.py

This file was deleted.

33 changes: 33 additions & 0 deletions pybluedot/pybdot.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
[Global]
api_key = aApglMV0QO74TpuZACRnSUvGel8ayxf7Z067Nnsa

[PATENT]
url = https://api.nasa.gov/patents/content

[APOD]
url = https://api.nasa.gov/planetary/apod

[NEO]
url = https://api.nasa.gov/neo/rest/v1

[EPIC]
url = https://api.nasa.gov/EPIC/api

[IMAGERY]
url = https://api.nasa.gov/planetary/earth/imagery

[ASSETS]
url = https://api.nasa.gov/planetary/earth/assets

[IMAGES]
url = https://images-api.nasa.gov

[ROVERS]
url = https://api.nasa.gov/mars-photos/api/v1/rovers

[SOUNDS]
url = https://api.nasa.gov/planetary/sounds

[TECHPORT]
url = https://api.nasa.gov/planetary/sounds

53 changes: 53 additions & 0 deletions pybluedot/pybdotconfig.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import configparser
import os

def create_config(path):
"""
Create pybluedot config file
"""
config = configparser.ConfigParser()
config.add_section('Global')
config.set('Global', 'API_KEY', input('Enter your NASA API Key:'))
config.add_section('PATENT')
config.set('PATENT', 'URL', 'https://api.nasa.gov/patents/content')
config.add_section('APOD')
config.set('APOD', 'URL', 'https://api.nasa.gov/planetary/apod')
config.add_section('NEO')
config.set('NEO', 'URL', 'https://api.nasa.gov/neo/rest/v1')
config.add_section('EPIC')
config.set('EPIC', 'URL', 'https://api.nasa.gov/EPIC/api')
config.add_section('IMAGERY')
config.set('IMAGERY', 'URL', 'https://api.nasa.gov/planetary/earth/imagery')
config.add_section('ASSETS')
config.set('ASSETS', 'URL', 'https://api.nasa.gov/planetary/earth/assets')
config.add_section('IMAGES')
config.set('IMAGES', 'URL', 'https://images-api.nasa.gov')
config.add_section('ROVERS')
config.set('ROVERS', 'URL', 'https://api.nasa.gov/mars-photos/api/v1/rovers')
config.add_section('SOUNDS')
config.set('SOUNDS', 'URL', 'https://api.nasa.gov/planetary/sounds')
config.add_section('TECHPORT')
config.set('TECHPORT', 'URL', 'https://api.nasa.gov/planetary/sounds')

with open(path, 'w') as config_file:
config.write(config_file)

def get_config(path):
"""
Return config object.
"""
if not os.path.exists(path):
create_config(path)
config = configparser.ConfigParser()
return config

def init_config():
path = 'pybdot.ini'
config = get_config(path)
return config


if __name__ == '__main__':
init_config()


14 changes: 14 additions & 0 deletions pybluedot/tests/base.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# Copyright 2016 Jeorry Balasabas
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import unittest


Expand Down
57 changes: 57 additions & 0 deletions pybluedot/tests/unit/test_patent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Copyright 2016 Jeorry Balasabas
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import mock
from pybluedot.api import patent
from pybluedot.tests import base
from requests.exceptions import HTTPError


class PatentTestSuite(base.DietTestCase):
"""This test suite provides some common things relevant to the
tests in this file. All tests should inherit from this class
"""

def setUp(self):
"""Perform setup activities
"""
self.testpatent = patent.Patent(query='plastic')

@mock.patch('pybluedot.api.patent.requests.get', autospec=True)
def test_get_response_ok(self, mock_get):
mock_response = mock.MagicMock()
expected_dict = {'key': 'value'}
mock_response.json.return_value = expected_dict
mock_get.return_value = mock_response
response_dict = self.testpatent.get()
self.assertEqual(1, mock_response.json.call_count)
self.assertEqual(response_dict, expected_dict)

@mock.patch('pybluedot.api.patent.requests.get', autospec=True)
def test_get_response_raises_HTTPError(self, mock_get):
mock_response = mock.MagicMock()
mock_response.status_code.return_value = 500
mock_response.raise_for_status.side_effect = HTTPError('NASA API DOWN')
mock_get.return_value = mock_response
with self.assertRaises(HTTPError):
self.testpatent.get()

@mock.patch('pybluedot.api.patent.requests.get', autospec=True)
def test_get_response_error_in_body(self, mock_get):
mock_response = mock.MagicMock()
mock_body = {'error': 'there is something wrong'}
mock_response.json.return_value = mock_body
mock_get.return_value = mock_response
with self.assertRaises(ValueError):
self.testpatent.get()
14 changes: 14 additions & 0 deletions pybluedot/tests/unit/test_sample.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# Copyright 2016 Jeorry Balasabas
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from pybluedot.tests import base


Expand Down