Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 14 additions & 19 deletions passboltapi/__init__.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -158,9 +158,7 @@ def get(self, url, return_response_object=False, **kwargs):
r = self.requests_session.get(self.server_url + url, headers=self.get_headers(), **kwargs)
try:
r.raise_for_status()
if return_response_object:
return r
return r.json()
return r if return_response_object else r.json()

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function APIClient.get refactored with the following changes:

except requests.exceptions.HTTPError as e:
logging.error(r.text)
raise e
Expand All@@ -169,9 +167,7 @@ def put(self, url, data, return_response_object=False, **kwargs):
r = self.requests_session.put(self.server_url + url, json=data, headers=self.get_headers(), **kwargs)
try:
r.raise_for_status()
if return_response_object:
return r
return r.json()
return r if return_response_object else r.json()
Comment on lines -172 to +170

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function APIClient.put refactored with the following changes:

except requests.exceptions.HTTPError as e:
logging.error(r.text)
raise e
Expand All@@ -180,9 +176,7 @@ def post(self, url, data, return_response_object=False, **kwargs):
r = self.requests_session.post(self.server_url + url, json=data, headers=self.get_headers(), **kwargs)
try:
r.raise_for_status()
if return_response_object:
return r
return r.json()
return r if return_response_object else r.json()
Comment on lines -183 to +179

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function APIClient.post refactored with the following changes:

except requests.exceptions.HTTPError as e:
logging.error(r.text)
raise e
Expand DownExpand Up@@ -247,12 +241,10 @@ def iterate_resources(self, params: Optional[dict] = None):
params = params or {}
url_params = urllib.parse.urlencode(params)
if url_params:
url_params = "?" + url_params
response = self.get("/resources.json" + url_params)
url_params = f"?{url_params}"
response = self.get(f"/resources.json{url_params}")
assert "body" in response.keys(), f"Key 'body' not found in response keys: {response.keys()}"
resources = response["body"]
for resource in resources:
yield resource
yield from response["body"]
Comment on lines -250 to +247

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function PassboltAPI.iterate_resources refactored with the following changes:


def list_resources(self, folder_id: Optional[PassboltFolderIdType] = None):
params = {
Expand All@@ -261,8 +253,8 @@ def list_resources(self, folder_id: Optional[PassboltFolderIdType] = None):
}
url_params = urllib.parse.urlencode(params)
if url_params:
url_params = "?" + url_params
response = self.get("/folders.json" + url_params)
url_params = f"?{url_params}"
response = self.get(f"/folders.json{url_params}")
Comment on lines -264 to +257

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function PassboltAPI.list_resources refactored with the following changes:

assert "body" in response.keys(), f"Key 'body' not found in response keys: {response.keys()}"
response = response["body"][0]
assert "children_resources" in response.keys(), (
Expand DownExpand Up@@ -292,7 +284,7 @@ def list_users(
else:
params = {"filter[has-access]": resource_or_folder_id, "contain[user]": 1}
params["contain[permission]"] = True
response = self.get(f"/users.json", params=params)
response = self.get("/users.json", params=params)
Comment on lines -295 to +287

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function PassboltAPI.list_users refactored with the following changes:

assert "body" in response.keys(), f"Key 'body' not found in response keys: {response.keys()}"
response = response["body"]
users = constructor(
Expand DownExpand Up@@ -465,8 +457,11 @@ def update_resource(
payload["secrets"] = self._encrypt_secrets(secret_text=secret_text, recipients=recipients)

if payload:
r = self.put(f"/resources/{resource_id}.json", payload, return_response_object=True)
return r
return self.put(
f"/resources/{resource_id}.json",
payload,
return_response_object=True,
)
Comment on lines -468 to +464

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function PassboltAPI.update_resource refactored with the following changes:


def describe_group(self, group_id: PassboltGroupIdType):
response = self.get(f"/groups/{group_id}.json", params={"contain[groups_users]": 1})
Expand Down
10 changes: 4 additions & 6 deletions test.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -4,7 +4,7 @@


def get_my_passwords(passbolt_obj):
result = list()
result = []

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function get_my_passwords refactored with the following changes:

for i in passbolt_obj.get(url="/resources.json?api-version=v2")["body"]:
result.append({
"id": i["id"],
Expand All@@ -14,8 +14,7 @@ def get_my_passwords(passbolt_obj):
})
print(i)
for i in result:
resource = passbolt_obj.get(
"/secrets/resource/{}.json?api-version=v2".format(i["id"]))
resource = passbolt_obj.get(f'/secrets/resource/{i["id"]}.json?api-version=v2')
i["password"] = passbolt_obj.decrypt(resource["body"]["data"])
print(result)

Expand All@@ -24,7 +23,7 @@ def get_passwords_basic():
# A simple example to show how to retrieve passwords of a user.
# Note the config file is placed in the project directory.
passbolt_obj = passboltapi.PassboltAPI(config_path="config.ini")
result = list()
result = []
Comment on lines -27 to +26

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function get_passwords_basic refactored with the following changes:

for i in passbolt_obj.get(url="/resources.json?api-version=v2")["body"]:
result.append({
"id": i["id"],
Expand All@@ -34,8 +33,7 @@ def get_passwords_basic():
})
print(i)
for i in result:
resource = passbolt_obj.get(
"/secrets/resource/{}.json?api-version=v2".format(i["id"]))
resource = passbolt_obj.get(f'/secrets/resource/{i["id"]}.json?api-version=v2')
i["password"] = passbolt_obj.decrypt(resource["body"]["data"])
print(result)
passbolt_obj.close_session()
Expand Down