diff --git a/.gitignore b/.gitignore index 7bbc71c..4956e1a 100644 --- a/.gitignore +++ b/.gitignore @@ -99,3 +99,6 @@ ENV/ # mypy .mypy_cache/ + +.idea/ +*.iml \ No newline at end of file diff --git a/README.md b/README.md index 2841b71..16d4a3d 100644 --- a/README.md +++ b/README.md @@ -5,3 +5,29 @@ Coming soon... this is where our officially supported SDK for Python is going to ## Developers... we need you! We need some help fleshing out this repo. If you're a Python dev with experience building PyPI-compatible libraries and web API wrappers, we're offering a reward of $250 to help us get started. For more info please email support@newsapi.org, or dive right in and send us a pull request. + + +# Usage +``` +import newsapi +newsapi.API_KEY = '' + +# list of all sources +newsapi.Sources().list() + +# list of American sources +newsapi.Sources().list(country='us') + +# list of American Gaming sources +newsapi.Sources().list(**{'country': 'us', 'category': 'gaming'}) + +# Same pattern for TopHeadlines and Everything + +newsapi.TopHeadlines().list() +newsapi.TopHeadlines().list(arg1=..., arg2=...) +newsapi.TopHeadlines().list(**{'arg1':..., 'arg2':...}) + +newsapi.Everything().list() +newsapi.Everything().list(arg1=..., arg2=...) +newsapi.Everything().list(**{'arg1':..., 'arg2':...}) +``` diff --git a/newsapi/__init__.py b/newsapi/__init__.py new file mode 100644 index 0000000..7e63e4a --- /dev/null +++ b/newsapi/__init__.py @@ -0,0 +1,4 @@ +API_KEY = None +API_URL_BASE = 'https://newsapi.org/v2/' + +from . resources import TopHeadlines, Everything, Sources \ No newline at end of file diff --git a/newsapi/client.py b/newsapi/client.py new file mode 100644 index 0000000..e0c8187 --- /dev/null +++ b/newsapi/client.py @@ -0,0 +1,24 @@ +import requests + +from .errors import NewsAPIError + + +class Client: + + def __init__(self): + self._session = requests.Session() + + from newsapi import API_KEY + self._api_key = API_KEY + self._session.headers.update({'Authorization': self._api_key}) + + from newsapi import API_URL_BASE + self._api_url_base = API_URL_BASE + + def get(self, endpoint, params): + resp = self._session.get(self._api_url_base + endpoint, params=params) + try: + resp.raise_for_status() + return resp.json() + except requests.exceptions.HTTPError: + raise NewsAPIError(resp.json()) \ No newline at end of file diff --git a/newsapi/errors.py b/newsapi/errors.py new file mode 100644 index 0000000..d87fb74 --- /dev/null +++ b/newsapi/errors.py @@ -0,0 +1,5 @@ +class NewsAPIError(Exception): + + def __init__(self, response_json, *args, **kwargs): + message = '{code} : {message}'.format(**response_json) + super().__init__(message, *args, **kwargs) \ No newline at end of file diff --git a/newsapi/resources.py b/newsapi/resources.py new file mode 100644 index 0000000..71bfefc --- /dev/null +++ b/newsapi/resources.py @@ -0,0 +1,31 @@ +from .client import Client + + +class BaseResource: + + endpoint = None + item = None + + def __init__(self): + self.client = Client() + + def list(self, **params): + return self.client.get(self.endpoint, params)[self.item] + + +class TopHeadlines(BaseResource): + + endpoint = 'top-headlines' + item = 'articles' + + +class Everything(BaseResource): + + endpoint = 'everything' + item = 'articles' + + +class Sources(BaseResource): + + endpoint = 'sources' + item = 'sources' diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..a3740f1 --- /dev/null +++ b/setup.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python +from setuptools import setup, find_packages + +install_reqs = [ + 'requests', +] + +setup( + name='newsapi', + version='0.0.0', + description='Python client for the News-API REST API', + author='James Christopher', + author_email='jcahall@washington.edu', + packages=find_packages(), + classifiers=[ + 'Development Status :: 3 - Alpha', + 'Intended Audience :: Developers', + 'License :: OSI Approved :: Apache Software License', + 'Natural Language :: English', + 'Operating System :: OS Independent', + 'Programming Language :: Python :: 3.4', + 'Programming Language :: Python :: 3.5', + 'Programming Language :: Python :: 3.6', + 'Programming Language :: Python', + 'Topic :: Internet :: WWW/HTTP', + ], + url='https://github.com/News-API-gh/News-API-python', + install_requires=install_reqs, +) \ No newline at end of file