Skip to content

Commit

Permalink
Merge pull request #15 from fgregg/main
Browse files Browse the repository at this point in the history
add organization argument and better error handling
  • Loading branch information
palewire committed Dec 15, 2023
2 parents 9406c6f + c42bc69 commit 84c932d
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions muckrock/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ def _get_request(self, url, params=None, headers=None):
params = {}
headers.update({"User-Agent": self.USER_AGENT})
response = requests.get(url, params=params, headers=headers)
if response.status_code != 200:
if response.json() == {"detail": "Invalid token."}:
raise CredentialsWrongError(response.json()["detail"])
else:
raise ValueError(
f"Muckrock API returned with this error: {response.json()}"
)
return response.json()

def _post_request(self, url, data=None, headers=None):
Expand All @@ -58,9 +65,14 @@ def _post_request(self, url, data=None, headers=None):
}
)
r = requests.post(url, json=data, headers=headers)

rjson = r.json()
if rjson == {"detail": "Invalid token."}:
raise CredentialsWrongError(rjson["detail"])
if r.status_code != 201:
breakpoint()
if rjson == {"detail": "Invalid token."}:
raise CredentialsWrongError(rjson["detail"])
else:
raise ValueError(f"Muckrock API returned with this error: {r.json()}")
return rjson


Expand Down Expand Up @@ -161,6 +173,7 @@ def create(
embargo=False,
permanent_embargo=False,
attachments=None,
organization=None,
):
"""Create a new request."""
if not title:
Expand All @@ -182,6 +195,8 @@ def create(
data["full_text"] = full_text
if attachments:
data["attachments"] = attachments
if organization:
data["organization"] = organization
return self._post_request(self.BASE_URI + self.endpoint + "/", data)

def filter(
Expand Down Expand Up @@ -209,6 +224,9 @@ def filter(
if jurisdiction_id:
params["jurisdiction"] = jurisdiction_id
if agency_id:
assert isinstance(agency_id, int) or (
isinstance(agency_id, str) and agency_id.isdigit()
), "agency_id must be a string"
params["agency"] = agency_id
datetime_submitted_choices = {
None: 1,
Expand Down

0 comments on commit 84c932d

Please sign in to comment.