Skip to content

Commit

Permalink
requrl
Browse files Browse the repository at this point in the history
  • Loading branch information
LoicGrobol committed Dec 19, 2023
1 parent 2ca7c3e commit 4bf3057
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 13 deletions.
53 changes: 49 additions & 4 deletions slides/04-requests/requrl_argparse.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,64 @@
import json

import argparse

import requests


def main(argv=None):
parser = argparse.ArgumentParser(description="Make http request")
parser.add_argument("url", help='The url to request')
parser = argparse.ArgumentParser(description="Make http requests")
parser.add_argument("url", help="The url to request")
parser.add_argument(
"--header",
help="Headers to add to the request, formatted as JSON.",
default=dict(),
type=json.loads,
)
parser.add_argument(
"-X",
"--request",
help="The type of request to send.",
choices=["GET", "PUT", "POST"],
default="GET",
)
parser.add_argument(
"-o", "--output", help="A file to write the output to. Defaults to stdin."
)
parser.add_argument(
"-d",
"--data",
help="Data to pass to a POST request, formatted as JSON.",
default=dict(),
type=json.loads,
)

args = parser.parse_args(argv)
if args.request == "GET":
response = requests.get(
args.url,
headers=args.header,
)
elif args.request == "PUT":
response = requests.put(
args.url,
headers=args.header,
)
elif args.request == "POST":
response = requests.post(
args.url,
headers=args.header,
data=args.data,
)
response = requests.get(args.url)
if response:
print(response.text)
if args.output is None:
print(response.text)
else:
with open(args.output, "w") as out_stream:
out_stream.write(response.text)
else:
print(f"Error (status code {response.status_code})")


if __name__ == "__main__":
main()
main()
File renamed without changes.
26 changes: 17 additions & 9 deletions slides/04-requests/requrl_click.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
import requests


@click.command(help="Download a text resource and print it")
@click.command(help="Mate http requests.")
@click.argument("url")
@click.option(
"--header", "-H", help="Headers to add to the request, formatted as JSON."
"--header",
"-H",
help="Headers to add to the request, formatted as JSON.",
default="{}",
)
@click.option(
"--output",
Expand All @@ -24,22 +27,27 @@
show_default=True,
help="The type of request to send.",
)
@click.option("--data", "-d", help="Data to pass to a POST request, formatted as JSON.")
@click.option(
"--data",
"-d",
help="Data to pass to a POST request, formatted as JSON.",
default="{}",
)
def main(url, header, output, request, data):
if request.lower() == "GET":
if request.upper() == "GET":
response = requests.get(
url,
header=json.loads(header),
headers=json.loads(header),
)
elif request.lower() == "PUT":
elif request.upper() == "PUT":
response = requests.put(
url,
header=json.loads(header),
headers=json.loads(header),
)
elif request.lower() == "POST":
elif request.upper() == "POST":
response = requests.post(
url,
header=json.loads(header),
headers=json.loads(header),
data=json.loads(data),
)
if response:
Expand Down

0 comments on commit 4bf3057

Please sign in to comment.