Skip to content

Commit d79033b

Browse files
author
Douglas
authored
Merge pull request #1 from SocketDev/initial-code
Initial code
2 parents 2d2af07 + 7ff7237 commit d79033b

File tree

16 files changed

+681
-0
lines changed

16 files changed

+681
-0
lines changed

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
*.json
2+
main.py
3+
venv
4+
.idea
5+
.DS_Store
6+
*.zip
7+
*.pyc

README.rst

Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
2+
socketdev-python-sdk
3+
###############
4+
5+
Purpose
6+
-------
7+
8+
The Socket.dev Python SDK provides a wrapper around the Socket.dev REST API to simplify making calls to the API from Python.
9+
10+
Initializing the module
11+
-----------------------
12+
13+
.. code-block::
14+
15+
from socketdev import SocketDev
16+
socket = SocketDev("REPLACE_ME")
17+
18+
Supported Functions
19+
-------------------
20+
21+
npm.issues(package, version)
22+
""""""""""""""""""""""""""""
23+
Retrieve the Issues associated with a package and version.
24+
25+
**Usage:**
26+
27+
.. code-block::
28+
29+
from socketdev import SocketDev
30+
socket = SocketDev("REPLACE_ME")
31+
print(socket.npm.issues("hardhat-gas-report", "1.1.25"))
32+
33+
**PARAMETERS:**
34+
35+
- **package (str)** - The name of the NPM package.
36+
- **version (str)** - The version of the NPM Package.
37+
38+
npm.score(package, version)
39+
"""""""""""""""""""""""""""
40+
Retrieve the Issues associated with a package and version.
41+
42+
**Usage:**
43+
44+
.. code-block::
45+
46+
from socketdev import SocketDev
47+
socket = SocketDev("REPLACE_ME")
48+
print(socket.npm.score("hardhat-gas-report", "1.1.25"))
49+
50+
**PARAMETERS:**
51+
52+
- **package (str)** - The name of the NPM package.
53+
- **version (str)** - The version of the NPM Package.
54+
55+
dependencies.get(limit, offset)
56+
""""""""""""""""""
57+
Retrieve the dependencies for the organization associated with the API Key
58+
59+
**Usage:**
60+
61+
.. code-block::
62+
63+
from socketdev import SocketDev
64+
socket = SocketDev("REPLACE_ME")
65+
print(socket.dependencies.get(10, 0))
66+
67+
**PARAMETERS:**
68+
69+
- **limit (int)** - The maximum number of dependencies to return
70+
- **offset (int)** - The index to start from for pulling the dependencies
71+
72+
dependencies.post(files, params)
73+
""""""""""""""""""""""""""""""""
74+
Retrieve the dependencies for the organization associated with the API Key
75+
76+
**Usage:**
77+
78+
.. code-block::
79+
80+
from socketdev import SocketDev
81+
socket = SocketDev("REPLACE_ME")
82+
file_names = [
83+
"path/to/package.json"
84+
]
85+
params = {
86+
"repository": "username/repo-name",
87+
"branch": "dependency-branch
88+
}
89+
print(socket.dependencies.post(file_names, params))
90+
91+
**PARAMETERS:**
92+
93+
- **files (list)** - The file paths of the manifest files to import into the Dependency API.
94+
- **params (dict)** - A dictionary of the `repository` and `branch` options for the API
95+
96+
org.get()
97+
"""""""""
98+
Retrieve the Socket.dev org information
99+
100+
**Usage:**
101+
102+
.. code-block::
103+
104+
from socketdev import SocketDev
105+
socket = SocketDev("REPLACE_ME")
106+
print(socket.org.get())
107+
108+
quota.get()
109+
"""""""""""
110+
Retrieve the the current quota available for your API Key
111+
112+
**Usage:**
113+
114+
.. code-block::
115+
116+
from socketdev import SocketDev
117+
socket = SocketDev("REPLACE_ME")
118+
print(socket.quota.get())
119+
120+
report.list()
121+
"""""""""""""
122+
Retrieve the list of all reports for the organization
123+
124+
**Usage:**
125+
126+
.. code-block::
127+
128+
from socketdev import SocketDev
129+
socket = SocketDev("REPLACE_ME")
130+
print(socket.report.list())
131+
132+
report.delete(report_id)
133+
""""""""""""""""""""""""
134+
Delete the specified report
135+
136+
**Usage:**
137+
138+
.. code-block::
139+
140+
from socketdev import SocketDev
141+
socket = SocketDev("REPLACE_ME")
142+
print(socket.report.delete("report-id"))
143+
144+
**PARAMETERS:**
145+
146+
- **report_id (str)** - The report ID of the report to delete
147+
148+
report.view(report_id)
149+
""""""""""""""""""""""
150+
Retrieve the information for a Project Health Report
151+
152+
**Usage:**
153+
154+
.. code-block::
155+
156+
from socketdev import SocketDev
157+
socket = SocketDev("REPLACE_ME")
158+
print(socket.report.view("report_id"))
159+
160+
**PARAMETERS:**
161+
162+
- **report_id (str)** - The report ID of the report to view
163+
164+
report.supported()
165+
""""""""""""""""""
166+
Retrieve the supported types of manifest files for creating a report
167+
168+
**Usage:**
169+
170+
.. code-block::
171+
172+
from socketdev import SocketDev
173+
socket = SocketDev("REPLACE_ME")
174+
print(socket.report.supported())
175+
176+
report.create(files)
177+
""""""""""""""""""""
178+
Create a new project health report with the provided files
179+
180+
**Usage:**
181+
182+
.. code-block::
183+
184+
from socketdev import SocketDev
185+
socket = SocketDev("REPLACE_ME")
186+
files = [
187+
"/path/to/manifest/package.json"
188+
]
189+
print(socket.report.create(files))
190+
191+
**PARAMETERS:**
192+
193+
- **files (list)** - List of file paths of manifest files
194+
195+
repositories.get()
196+
""""""""""""""""""
197+
Get a list of information about the tracked repositores
198+
199+
**Usage:**
200+
201+
.. code-block::
202+
203+
from socketdev import SocketDev
204+
socket = SocketDev("REPLACE_ME")
205+
print(socket.repositories.get())
206+
207+
settings.get()
208+
""""""""""""""
209+
Retrieve the Socket Organization Settings
210+
211+
**Usage:**
212+
213+
.. code-block::
214+
215+
from socketdev import SocketDev
216+
socket = SocketDev("REPLACE_ME")
217+
print(socket.settings.get())

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[build-system]
2+
requires = ["setuptools"]
3+
build-backend = "setuptools.build_meta"

setup.cfg

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[metadata]
2+
3+
name = socketdev-sdk
4+
version = 0.0.1
5+
author = Douglas Coburn
6+
author_email = [email protected]
7+
description = A python SDK for the socket.dev API
8+
long_description = file: README.rst
9+
long_description_content_type: text/x-rst
10+
license = MIT
11+
classifiers =
12+
Programming Language :: Python :: 3
13+
14+
[options]
15+
python_requires = >=3.6
16+
install_requires =
17+
requests

socketdev/__init__.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import logging
2+
import requests
3+
import base64
4+
from socketdev.dependencies import Dependencies
5+
from socketdev.npm import NPM
6+
from socketdev.openapi import OpenAPI
7+
from socketdev.org import Orgs
8+
from socketdev.quota import Quota
9+
from socketdev.report import Report
10+
from socketdev.repositories import Repositories
11+
from socketdev.settings import Settings
12+
from socketdev.socket_classes import Dependency, Org, Response
13+
from socketdev.exceptions import APIKeyMissing, APIFailure, APIAccessDenied, APIInsufficientQuota, APIResourceNotFound
14+
15+
16+
__author__ = 'socket.dev'
17+
__version__ = '0.0.1'
18+
__all__ = [
19+
"SocketDev",
20+
]
21+
22+
23+
global encoded_key
24+
api_url = "https://api.socket.dev/v0"
25+
log = logging.getLogger("socketdev")
26+
log.addHandler(logging.NullHandler())
27+
28+
29+
def encode_key(token: str):
30+
global encoded_key
31+
encoded_key = base64.b64encode(token.encode()).decode('ascii')
32+
33+
34+
def do_request(
35+
path: str,
36+
headers: dict = None,
37+
payload: [dict, str] = None,
38+
files: list = None,
39+
method: str = "GET",
40+
):
41+
if encoded_key is None or encoded_key == "":
42+
raise APIKeyMissing
43+
44+
if headers is None:
45+
headers = {
46+
'Authorization': f"Basic {encoded_key}",
47+
'User-Agent': 'SocketPythonScript/0.0.1',
48+
"accept": "application/json"
49+
}
50+
url = f"{api_url}/{path}"
51+
try:
52+
response = requests.request(
53+
method.upper(),
54+
url,
55+
headers=headers,
56+
data=payload,
57+
files=files
58+
)
59+
if response.status_code >= 400:
60+
raise APIFailure("Bad Request")
61+
elif response.status_code == 401:
62+
raise APIAccessDenied("Unauthorized")
63+
elif response.status_code == 403:
64+
raise APIInsufficientQuota("Insufficient max_quota for API method")
65+
elif response.status_code == 404:
66+
raise APIResourceNotFound(f"Path not found {path}")
67+
elif response.status_code == 429:
68+
raise APIInsufficientQuota("Insufficient quota for API route")
69+
except Exception as error:
70+
response = Response(
71+
text=f"{error}",
72+
error=True,
73+
status_code=500
74+
)
75+
raise APIFailure(response)
76+
return response
77+
78+
79+
class SocketDev:
80+
def __init__(self, token: str):
81+
self.token = token + ":"
82+
encode_key(self.token)
83+
self.dependencies = Dependencies()
84+
self.npm = NPM()
85+
self.openapi = OpenAPI()
86+
self.org = Orgs()
87+
self.quota = Quota()
88+
self.report = Report()
89+
self.repositories = Repositories()
90+
self.settings = Settings()

socketdev/dependencies/__init__.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import socketdev
2+
from socketdev.tools import load_files
3+
from urllib.parse import urlencode
4+
import json
5+
6+
7+
class Dependencies:
8+
@staticmethod
9+
def post(files: list, params: dict) -> dict:
10+
loaded_files = []
11+
loaded_files = load_files(files, loaded_files)
12+
path = "dependencies/upload?" + urlencode(params)
13+
response = socketdev.do_request(
14+
path=path,
15+
files=loaded_files,
16+
method="POST"
17+
)
18+
if response.status_code == 200:
19+
result = response.json()
20+
else:
21+
result = {}
22+
print(f"Error posting {files} to the Dependency API")
23+
print(response.text)
24+
return result
25+
26+
@staticmethod
27+
def get(
28+
limit: int = 50,
29+
offset: int = 0,
30+
) -> dict:
31+
path = "dependencies/search"
32+
payload = {
33+
"limit": limit,
34+
"offset": offset
35+
}
36+
payload_str = json.dumps(payload)
37+
response = socketdev.do_request(
38+
path=path,
39+
method="POST",
40+
payload=payload_str
41+
)
42+
if response.status_code == 200:
43+
result = response.json()
44+
else:
45+
result = {}
46+
print("Unable to retrieve Dependencies")
47+
print(response.text)
48+
return result

0 commit comments

Comments
 (0)