Skip to content

Commit

Permalink
Merge pull request #1 from stepanzubkov/dev
Browse files Browse the repository at this point in the history
  • Loading branch information
stepanzubkov authored Mar 28, 2023
2 parents 2e0d1ab + 4981e65 commit dd48db1
Show file tree
Hide file tree
Showing 5 changed files with 289 additions and 8 deletions.
27 changes: 26 additions & 1 deletion florgon_cc_cli/commands/url.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
Single url commands.
"""
from datetime import datetime
import re

import click

from florgon_cc_cli.services.url import build_open_url, create_url
from florgon_cc_cli.services.url import build_open_url, create_url, get_url_info_by_hash
from florgon_cc_cli import config


@click.group()
Expand Down Expand Up @@ -34,7 +36,30 @@ def create(only_url: bool, do_not_save: bool, long_url: str):
click.echo("Short url: " + click.style(short_url, fg="green"))
click.echo(f"Redirects to: {response['redirect_url']}")
click.echo(f"Expires at: {datetime.fromtimestamp(response['expires_at'])}")
click.echo(f"QR Code url: {response['_links']['qr']['href']}")
if response["stats_is_public"]:
click.echo("Stats is public")


@url.command()
@click.argument("short_url", type=str)
def info(short_url: str):
"""Prints main information about short url."""
short_url_hashes = re.findall(f"^{config.URL_OPEN_PROVIDER}" + r"/([a-zA-Z0-9]{6})$", short_url)
if not short_url_hashes:
click.secho(
f"Short url is invalid! It should be in form '{config.URL_OPEN_PROVIDER}/xxxxxx'",
err=True, fg="red",
)
return

success, response = get_url_info_by_hash(short_url_hashes[0])
if not success:
click.secho(response["message"], err=True, fg="red")
return

click.echo(f"Redirects to: " + click.style(response['redirect_url'], fg="green"))
click.echo(f"Expires at: {datetime.fromtimestamp(response['expires_at'])}")
click.echo(f"QR Code url: {response['_links']['qr']['href']}")
if response["stats_is_public"]:
click.echo("Stats is public")
1 change: 1 addition & 0 deletions florgon_cc_cli/config.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
CC_API_URL = "https://api-cc.florgon.com/v1"
URL_OPEN_PROVIDER = "https://cc.florgon.com/o"
URL_QR_PROVIDER = "https://cc.florgon.com/qr"
34 changes: 29 additions & 5 deletions florgon_cc_cli/services/url.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,44 @@
"""


from typing import Any, Dict, Tuple
from typing import Any, Dict, Tuple, Optional

from florgon_cc_cli.services.api import execute_api_method
import florgon_cc_cli.config as config
from florgon_cc_cli import config


def build_open_url(hash: str) -> str:
"""Builds url for opening short url."""
return f"{config.URL_OPEN_PROVIDER}/{hash}"


def create_url(long_url: str, auth_token: str | None = None) -> Tuple[bool, Dict[str, Any]]:
def create_url(long_url: str, auth_token: Optional[str] = None) -> Tuple[bool, Dict[str, Any]]:
"""
Creates short url from long url.
:param str long_url: url which short url will be redirect
:param Optional[str] auth_token: Florgon OAuth token that used for authentification. Defaults to None
:return: Tuple with two elements.
First is a creaton status (True if successfully).
Seconds is a response body.
:rtype: Tuple[bool, Dict[str, Any]]
"""
response = execute_api_method("POST", "urls/", data={"url": long_url})

if "success" in response:
return True, response["success"]["url"]
else:
return False, response["error"]
return False, response["error"]


def get_url_info_by_hash(hash: str) -> Tuple[bool, Dict[str, Any]]:
"""
Returns info about short url by hash.
:param str hash: short url hash
:return: Tuple with two elements.
First is a creaton status (True if successfully).
Seconds is a response body.
:rtype: Tuple[bool, Dict[str, any]]
"""
response = execute_api_method("GET", f"urls/{hash}/")
if "success" in response:
return True, response["success"]["url"]
return False, response["error"]
Loading

0 comments on commit dd48db1

Please sign in to comment.