Clean up API a bit, add deprecation warning for shorten endpoint

This commit is contained in:
grym 2022-05-27 13:04:33 -04:00
parent 49d427a72c
commit 36d6a2a87b
4 changed files with 94 additions and 78 deletions

View File

@ -1,77 +0,0 @@
import typing as t
from pathlib import Path
import httpx
import typer
from oxo import __version__
app = typer.Typer()
base_url = typer.Option("http://0x0.st", envvar="OXO_BASE_URL")
def version_callback(value: bool):
if value:
typer.echo(f"{__version__}")
raise typer.Exit()
@app.callback()
def main(
version: t.Optional[bool] = typer.Option(
None,
"--version",
callback=version_callback,
is_eager=True,
help="Show the version and exit.",
),
):
"""A command line utility for 0x0.st compliant pastebins.
Set `OXO_BASE_URL` if the base URL is not http://0x0.st.
"""
@app.command()
def files(
files: t.List[Path] = typer.Argument(
..., min=1, exists=True, file_okay=True, dir_okay=False, resolve_path=True
),
base_url: str = base_url,
):
"""Upload one or more files."""
urls = []
with httpx.Client() as client:
for f in files:
payload = {"file": f.open("rb")}
res = client.post(base_url, files=payload)
if res.status_code == httpx.codes.OK:
urls.append(res.text.strip())
print(" ".join(urls), end="")
@app.command()
def repost(urls: t.List[str], base_url=base_url):
"""Repost one or more urls."""
reposted = []
with httpx.Client() as client:
for u in urls:
data = {"url": u.strip()}
res = client.post(base_url, data=data)
if res.status_code == httpx.codes.OK:
reposted.append(res.text.strip())
print(" ".join(reposted), end="")
@app.command()
def shorten(urls: t.List[str], base_url=base_url):
"""Shorten one or more urls."""
shortened = []
with httpx.Client() as client:
for u in urls:
data = {"shorten": u.strip()}
res = client.post(base_url, data=data)
if res.status_code == httpx.codes.OK:
shortened.append(res.text.strip())
print(" ".join(shortened), end="")

30
oxo/api.py Normal file
View File

@ -0,0 +1,30 @@
import io
import typing as t
from pathlib import Path
import httpx
def post_to(base_url: str, *, data: t.Iterable[t.Dict]) -> str:
retval = []
with httpx.Client() as client:
for d in data:
if isinstance(d.get("file"), io.BufferedReader):
res = client.post(base_url, files=d)
else:
res = client.post(base_url, data=d)
res.raise_for_status()
retval.append(res.text.strip())
return " ".join(retval)
def files(base_url, files: t.List[Path]):
return post_to(base_url, data=({"file": f.open("rb")} for f in files))
def repost(base_url, urls: t.List[str]):
return post_to(base_url, data=({"url": u.strip()} for u in urls))
def shorten(base_url, urls: t.List[str]):
return post_to(base_url, data=({"shorten": u.strip()} for u in urls))

62
oxo/cli.py Normal file
View File

@ -0,0 +1,62 @@
import typing as t
from pathlib import Path
import httpx
import typer
from oxo import __version__, api
app = typer.Typer()
BASE_URL = typer.Option("https://0x0.st", envvar="OXO_BASE_URL")
def version_callback(value: bool):
if value:
typer.echo(f"{__version__}")
raise typer.Exit()
@app.callback()
def main(
version: t.Optional[bool] = typer.Option(
None,
"--version",
callback=version_callback,
is_eager=True,
help="Show the version and exit.",
),
):
"""A command line utility for 0x0.st compliant pastebins.
To use a different 0x0 site, set `OXO_BASE_URL` in your environment.
"""
@app.command()
def files(
files: t.List[Path] = typer.Argument(
..., min=1, exists=True, file_okay=True, dir_okay=False, resolve_path=True
),
base_url: str = BASE_URL,
):
"""Upload one or more files."""
typer.echo(api.files(base_url, files))
@app.command()
def repost(urls: t.List[str], base_url=BASE_URL):
"""Repost one or more urls."""
typer.echo(api.repost(base_url, urls))
@app.command()
def shorten(urls: t.List[str], base_url=BASE_URL):
"""Shorten one or more urls."""
if base_url == BASE_URL.default:
typer.secho(
f"Warning: shortening is often disabled "
f"for {BASE_URL.default}, command may fail",
fg=typer.colors.RED,
)
typer.echo(api.shorten(base_url, urls))

View File

@ -14,7 +14,7 @@ install_requires=
[options.entry_points]
console_scripts =
oxo = oxo.__main__:app
oxo = oxo.cli:app
[options.extras_require]
dev =
@ -23,3 +23,4 @@ dev =
ipython
isort>=4.3.21
pre-commit
pudb