Skip to content
This repository has been archived by the owner on Jan 7, 2024. It is now read-only.

Commit

Permalink
simplify handling of http and rpc calls
Browse files Browse the repository at this point in the history
  • Loading branch information
heartsucker committed May 7, 2019
1 parent 578c1b6 commit 400aeb3
Showing 1 changed file with 43 additions and 29 deletions.
72 changes: 43 additions & 29 deletions sdclientapi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,37 +86,51 @@ def _send_json_request(
headers: Optional[Dict[str, str]] = None,
) -> Tuple[Any, int, Dict[str, str]]:
if self.proxy: # We are using the Qubes securedrop-proxy
if method == "POST":
data = {
"method": method,
"path_query": path_query,
"body": body,
} # type: Dict[str, Any]
if headers is not None and headers:
data["headers"] = headers
elif method == "GET" or method == "DELETE":
data = {"method": method, "path_query": path_query, "headers": headers}

data_str = json.dumps(data, sort_keys=True)
result = json.loads(json_query(self.proxy_vm_name, data_str))
return json.loads(result["body"]), result["status"], result["headers"]

func = self._send_rpc_json_request
else: # We are not using the Qubes securedrop-proxy
if method == "POST":
result = requests.post(
urljoin(self.server, path_query), headers=headers, data=body
)
elif method == "GET":
result = requests.get(urljoin(self.server, path_query), headers=headers)
elif method == "DELETE":
result = requests.delete(
urljoin(self.server, path_query), headers=headers
)
func = self._send_http_json_request

return func(method, path_query, body, headers)

def _send_http_json_request(
self,
method: str,
path_query: str,
body: Optional[str] = None,
headers: Optional[Dict[str, str]] = None,
) -> Tuple[Any, int, Dict[str, str]]:
url = urljoin(self.server, path_query)
kwargs = {"headers": headers} # type: Dict[str, Any]

if method == "POST":
kwargs["data"] = body

result = requests.request(method, url, **kwargs)

# Because when we download a file there is no JSON in the body
if path_query.endswith("/download"):
return result, result.status_code, dict(result.headers)

return result.json(), result.status_code, dict(result.headers)

def _send_rpc_json_request(
self,
method: str,
path_query: str,
body: Optional[str] = None,
headers: Optional[Dict[str, str]] = None,
) -> Tuple[Any, int, Dict[str, str]]:
data = {"method": method, "path_query": path_query} # type: Dict[str, Any]

if method == "POST":
data["body"] = body

if headers is not None and headers:
data["headers"] = headers

# Because when we download a file there is no JSON in the body
if path_query.find("/download") != -1:
return result, result.status_code, result.headers
return result.json(), result.status_code, result.headers
data_str = json.dumps(data, sort_keys=True)
result = json.loads(json_query(self.proxy_vm_name, data_str))
return json.loads(result["body"]), result["status"], result["headers"]

def authenticate(self, totp: Optional[str] = None) -> bool:
"""
Expand Down

0 comments on commit 400aeb3

Please sign in to comment.