Skip to content

Commit

Permalink
feat: dynamic headers
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-Destructive committed Nov 27, 2022
1 parent ee17dae commit c0d70f5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
10 changes: 9 additions & 1 deletion src/aptui_styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,12 @@ Button {
*/

.header{
layout: horizontal;
layout: vertical;
max-height: 5;
}
#headers{
max-height: 5;
width: 1fr;
}

#key, #value {
Expand Down Expand Up @@ -68,3 +72,7 @@ Button {
color: white;
border: solid white;
}

RequestHeader {
layout: horizontal;
}
29 changes: 20 additions & 9 deletions src/main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import json
import curl
import uncurl
from typing import List
import pyperclip
import requests
from textual.app import App, ComposeResult
Expand All @@ -28,7 +27,7 @@ def compose(self) -> ComposeResult:
yield Container(
Input(placeholder="key", id="key"),
Input(placeholder="value", id="value"),
classes="header",
id="header",
)


Expand All @@ -54,6 +53,16 @@ class RequestContainer(Static):
"""A APTUI widget."""

method_choice = "GET"
headers_dict = {}

def get_headers(self) -> dict:
headers = self.query("#header")
for header in headers:
key = header.query_one("#key").value
value = header.query_one("#value").value
if key and value:
self.headers_dict[key] = value
return self.headers_dict

def catch_response(self, resp: requests.Response) -> dict:
try:
Expand All @@ -70,15 +79,10 @@ def get_request(self, url: str) -> dict:
"response": resp.content.decode("ascii"),
}

def get_headers(self) -> dict:
headers = self.query(".headers")
for header in headers:
kv_pair = header

return {}

def post_request(self, url: str, body: dict, headers: dict) -> dict:
# resp = requests.post(url, data=body, headers=headers)
headers = self.get_headers() or {}
resp = requests.request("POST", url, headers=headers, json=body)
if resp.status_code not in range(200, 227):
return self.catch_response(resp)
Expand All @@ -98,6 +102,7 @@ def delete_request(self, url: str) -> dict:

def put_request(self, url: str, body: dict, headers: dict) -> dict:
# resp = requests.post(url, data=body, headers=headers)
headers = self.get_headers() or {}
resp = requests.request("PUT", url, headers=headers, json=body)
if resp.status_code not in range(200, 227):
return self.catch_response(resp)
Expand All @@ -108,6 +113,7 @@ def put_request(self, url: str, body: dict, headers: dict) -> dict:

def patch_request(self, url: str, body: dict, headers: dict) -> dict:
# resp = requests.post(url, data=body, headers=headers)
headers = self.get_headers() or {}
resp = requests.request("PATCH", url, headers=headers, json=body)
if resp.status_code not in range(200, 227):
return self.catch_response(resp)
Expand Down Expand Up @@ -220,12 +226,17 @@ def on_button_pressed(self, event: Button.Pressed) -> None:
self.query_one("#reqheaders").mount(headers)
headers.scroll_visible()

elif button_id == "add_head":
header = RequestHeader(id="header")
self.query_one("#headers").mount(header)

def compose(self) -> ComposeResult:
"""Create child widgets of a API Request."""
yield Input(placeholder="URL", id="url")
yield Button("Send", id="send", variant="success")
yield RequestMethods("Rquest Methods", id="methods", classes="body")
yield RequestHeader()
yield Container(RequestHeader(), id="headers")
yield Button("Add Header", id="add_head")
yield Body(expand=True, id="body")
yield Response("Response", expand=True, id="response")
yield Button("Copy Curl", id="tocurl")
Expand Down

0 comments on commit c0d70f5

Please sign in to comment.