-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlibrariesio.py
39 lines (30 loc) · 1.21 KB
/
librariesio.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/usr/bin/python3.6
import json
import logging
import requests
class LibraryInfoGetter():
"""
Class responsible for consuming the Libraries.io API.
"""
def __init__(self, api_key, platform, name):
"""
The class requires an API key, the libraries platform and the library name.
"""
self.__url = "https://libraries.io/api/%s/%s?api_key=%s" % (platform, name, api_key)
def get(self):
"""
Returns the current library info, or None if an error occurs.
"""
library_info = None
try:
response = requests.get(self.__url)
if response.status_code == 200:
library_info = json.loads(response.content.decode("utf-8"))
logging.info("Success on getting the current library info.")
else:
logging.warning("Failure on getting the current library info. "
"Status code: %s. Libraries.io API response: %s",
response.status_code, response.text)
except requests.exceptions.ConnectionError:
logging.exception("Error on getting the current library info. Stack trace:")
return library_info