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

Adding bindings for the "Patents" API #13

Closed
wants to merge 6 commits into from
Closed
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
40 changes: 40 additions & 0 deletions patent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import argparse
import requests
import json


def patent(**kwargs):
Copy link
Contributor

Choose a reason for hiding this comment

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

The function name should indicate what the function does, not what it is. I would recommend you create a class called Patents, and have this function inside (rename the function to get or something like that)

Also, I cheated and read ahead a little bit, and you likely don't need to capture **kwargs here. Try to limit your input to what you'll actually use. See a future comment for more info here.

patent_url = 'https://api.nasa.gov/patents/content'
Copy link
Contributor

Choose a reason for hiding this comment

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

I would remove this static URL and instead make it a parameter to this function. We'll want to provide the URL for this API via a configuration file, most likely. We generally want to stay away from statically declaring things in a function.

get_params = {}
Copy link
Contributor

Choose a reason for hiding this comment

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

I am not understanding this and the next two lines. You essentially create a dict get_params then add all of the key/value pairs from kwargs, making it identical. Then you never use get_params. Was there a reason for this?

Copy link
Author

Choose a reason for hiding this comment

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

That is me not checking my work. I left kwargs in the get_response line to validate it would work and when it did I didn't go back and remove the get_params dict.

for key, value in kwargs.iteritems():
get_params[key] = value
get_response = requests.get(patent_url, params = kwargs)
Copy link
Contributor

Choose a reason for hiding this comment

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

Kind of what I was talking about before - there doesn't seem to be a reason to pass kwargs in here - can you elaborate on why you did this?

Copy link
Author

Choose a reason for hiding this comment

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

See my previous comment about not removing the get_params dict after testing kwargs.

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, get_response kind of sounds like a function name. How about just response?


if get_response.status_code == 429:
Copy link
Contributor

Choose a reason for hiding this comment

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

Great idea, but let's make sure we capture other error codes. A common approach is to throw Exceptions for anything above 2XX, but I'll leave that up to you.

raise Exception('You have exceeded your rate limit')

body = get_response.json()

if 'error' in body:
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this how the Patents API reports errors? Genuinely curious, I haven't poked around it yet.

raise Exception(body['error'])
# Current default is to print out json, need to add ability for file output
Copy link
Contributor

Choose a reason for hiding this comment

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

Remember, these functions will be consumed by other Python modules. We really aren't looking to interact with the user in any way here, but to return useful data that other Python code can consume. How about a dictionary?

pretty_print = json.dumps(body, indent=2)
return pretty_print


def main():
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a good idea, but not the right place for this. This file won't be run directly by the user, but by other Python modules, so anything related to arguments should go elsewhere. I'd remove this function, and everything below it, as this module needs to focus exclusively on functions for the Patents API.

ap = argparse.ArgumentParser(description = 'Search NASA patent portfolio')
ap.add_argument('-a', '--api_key',
help = 'Your NASA API key', required=True)
ap.add_argument('-q', '--query', help = 'Text to search on', required=True)
ap.add_argument('-c', '--concept_tags',
help = 'Return ordered dict of concepts from the abstract')
ap.add_argument('-l', '--limit', help = 'Number of patents to return',
type=int)
args = ap.parse_args()

#need to add option to output json file
print(patent(**vars(args)))

if __name__ == '__main__':
main()