Skip to content

Commit 669cf90

Browse files
committed
Add API Endpoints, clean up documentation.
1 parent 1b38144 commit 669cf90

10 files changed

Lines changed: 130761 additions & 451 deletions

edgar/client.py

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
from edgar.current_events import CurrentEvents
1010
from edgar.issuers import Issuers
1111
from edgar.ownership_filings import OwnershipFilings
12+
from edgar.submissions import Submissions
13+
from edgar.xbrl import Xbrl
1214

1315

1416
class EdgarClient():
@@ -36,7 +38,7 @@ def archives(self) -> Archives:
3638
3739
### Returns
3840
---
39-
Users:
41+
Archives:
4042
The `Archives` services Object.
4143
"""
4244

@@ -50,7 +52,7 @@ def companies(self) -> Companies:
5052
5153
### Returns
5254
---
53-
Users:
55+
Companies:
5456
The `Companies` services Object.
5557
"""
5658

@@ -64,7 +66,7 @@ def series(self) -> Series:
6466
6567
### Returns
6668
---
67-
Users:
69+
Series:
6870
The `Series` services Object.
6971
"""
7072

@@ -78,7 +80,7 @@ def mutual_funds(self) -> MutualFunds:
7880
7981
### Returns
8082
---
81-
Users:
83+
MutualFunds:
8284
The `MutualFunds` services Object.
8385
"""
8486

@@ -92,7 +94,7 @@ def variable_insurance_products(self) -> VariableInsuranceProducts:
9294
9395
### Returns
9496
---
95-
Users:
97+
VariableInsuranceProducts:
9698
The `VariableInsuranceProducts` services Object.
9799
"""
98100

@@ -106,7 +108,7 @@ def datasets(self) -> Datasets:
106108
107109
### Returns
108110
---
109-
Users:
111+
Datasets:
110112
The `Datasets` services Object.
111113
"""
112114

@@ -120,7 +122,7 @@ def filings(self) -> Filings:
120122
121123
### Returns
122124
---
123-
Users:
125+
Filings:
124126
The `Filings` services Object.
125127
"""
126128

@@ -134,7 +136,7 @@ def current_events(self) -> CurrentEvents:
134136
135137
### Returns
136138
---
137-
Users:
139+
CurrentEvents:
138140
The `CurrentEvents` services Object.
139141
"""
140142

@@ -148,7 +150,7 @@ def issuers(self) -> Issuers:
148150
149151
### Returns
150152
---
151-
Users:
153+
Issuers:
152154
The `Issuers` services Object.
153155
"""
154156

@@ -162,11 +164,39 @@ def ownership_filings(self) -> OwnershipFilings:
162164
163165
### Returns
164166
---
165-
Users:
167+
OwnershipFilings:
166168
The `OwnershipFilings` services Object.
167169
"""
168170

169171
# Grab the `OwnershipFilings` object.
170172
object = OwnershipFilings(session=self.edgar_session)
171173

172174
return object
175+
176+
def submissions(self) -> Submissions:
177+
"""Used to access the `Submissions` services.
178+
179+
### Returns
180+
---
181+
Submissions:
182+
The `Submissions` services Object.
183+
"""
184+
185+
# Grab the `Submissions` object.
186+
object = Submissions(session=self.edgar_session)
187+
188+
return object
189+
190+
def xbrl(self) -> Xbrl:
191+
"""Used to access the `Xbrl` services.
192+
193+
### Returns
194+
---
195+
Xbrl:
196+
The `Xbrl` services Object.
197+
"""
198+
199+
# Grab the `Xbrl` object.
200+
object = Xbrl(session=self.edgar_session)
201+
202+
return object

edgar/session.py

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
import json
22
import time
3-
from pprint import pprint
43
import requests
54
import logging
65
import pathlib
76

87
from typing import Dict
9-
from datetime import datetime
10-
from datetime import date
118

129

1310
class EdgarSession():
@@ -40,6 +37,8 @@ def __init__(self, client: object) -> None:
4037

4138
self.client: EdgarClient = client
4239
self.resource = 'https://www.sec.gov'
40+
self.api_resource = 'https://data.sec.gov'
41+
self.total_requests = 0
4342

4443
if not pathlib.Path('logs').exists():
4544
pathlib.Path('logs').mkdir()
@@ -60,21 +59,28 @@ def __repr__(self) -> str:
6059

6160
return str_representation
6261

63-
def build_url(self, endpoint: str) -> str:
62+
def build_url(self, endpoint: str, use_api: bool = False) -> str:
6463
"""Builds the full url for the endpoint.
6564
6665
### Parameters
6766
----
6867
endpoint : str
6968
The endpoint being requested.
7069
70+
use_api : bool (optional, Default=False)
71+
If `True` use the API resource URL, `False`
72+
use the filings resource URL.
73+
7174
### Returns
7275
----
7376
str:
7477
The full URL with the endpoint needed.
7578
"""
7679

77-
url = self.resource + endpoint
80+
if use_api:
81+
url = self.api_resource + endpoint
82+
else:
83+
url = self.resource + endpoint
7884

7985
return url
8086

@@ -84,7 +90,8 @@ def make_request(
8490
endpoint: str,
8591
params: dict = None,
8692
data: dict = None,
87-
json_payload: dict = None
93+
json_payload: dict = None,
94+
use_api: bool = False
8895
) -> Dict:
8996
"""Handles all the requests in the library.
9097
@@ -112,13 +119,17 @@ def make_request(
112119
json : dict (optional, Default=None)
113120
A json data payload for a request
114121
122+
use_api : bool (optional, Default=False)
123+
If `True` use the API resource URL, `False`
124+
use the filings resource URL.
125+
115126
### Returns:
116127
----
117128
A Dictionary object containing the JSON values.
118129
"""
119130

120131
# Build the URL.
121-
url = self.build_url(endpoint=endpoint)
132+
url = self.build_url(endpoint=endpoint, use_api=use_api)
122133

123134
logging.info(
124135
"URL: {url}".format(url=url)
@@ -144,11 +155,18 @@ def make_request(
144155

145156
print(request_request.url)
146157

158+
self.total_requests += 1
159+
147160
# Send the request.
148161
response: requests.Response = request_session.send(
149162
request=request_request
150163
)
151164

165+
if self.total_requests == 9:
166+
print("sleeping for 5 seconds.")
167+
time.sleep(5)
168+
self.total_requests = 0
169+
152170
# Keep going.
153171
while response.status_code != 200:
154172

@@ -157,6 +175,7 @@ def make_request(
157175
request=request_request
158176
)
159177
except:
178+
print("Sleeping for five seconds")
160179
time.sleep(5)
161180

162181
# Close the session.
@@ -166,8 +185,6 @@ def make_request(
166185
response_headers = response.headers
167186
content_type = response_headers['Content-Type']
168187

169-
170-
171188
# If it's okay and no details.
172189
if response.ok and len(response.content) > 0:
173190

edgar/submissions.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
from edgar.session import EdgarSession
2+
from edgar.utilis import EdgarUtilities
3+
4+
5+
class Submissions():
6+
7+
"""
8+
## Overview:
9+
----
10+
Allows a user to query each entity’s current filing
11+
history using the SEC Restful API.
12+
"""
13+
14+
def __init__(self, session: EdgarSession) -> None:
15+
"""Initializes the `Submissions` object.
16+
17+
### Parameters
18+
----
19+
session : `EdgarSession`
20+
An initialized session of the `EdgarSession`.
21+
22+
### Usage
23+
----
24+
>>> edgar_client = EdgarClient()
25+
>>> submissions_service = edgar_client.submissions()
26+
"""
27+
28+
# Set the session.
29+
self.edgar_session: EdgarSession = session
30+
self.edgar_utilities: EdgarUtilities = EdgarUtilities()
31+
32+
def __repr__(self) -> str:
33+
"""String representation of the `EdgarClient.Submissions` object."""
34+
35+
# define the string representation
36+
str_representation = '<EdgarClient.Submissions(active=True, connected=True)>'
37+
38+
return str_representation
39+
40+
def get_submissions(self, cik: str) -> dict:
41+
"""Returns all the ownership filings for a given CIK number.
42+
43+
### Arguments:
44+
----
45+
cik : str
46+
The CIK number you want to query.
47+
48+
### Returns:
49+
----
50+
dict :
51+
A collection of `Submission` resource objects.
52+
53+
### Usage:
54+
----
55+
>>> edgar_client = EdgarClient()
56+
>>> submissions_service = edgar_client.submissions()
57+
>>> submissions_service.get_submissions(
58+
cik='1326801'
59+
)
60+
"""
61+
62+
if len(cik) < 10:
63+
num_of_zeros = 10 - len(cik)
64+
cik = num_of_zeros*"0" + cik
65+
66+
# Grab the Data.
67+
response = self.edgar_session.make_request(
68+
method='get',
69+
endpoint=f'/submissions/CIK{cik}.json',
70+
use_api=True
71+
)
72+
73+
return response

0 commit comments

Comments
 (0)