Skip to content

Commit

Permalink
Merge pull request #577 from jacebrowning/slash-for-blank
Browse files Browse the repository at this point in the history
Support slashes as placeholders for blank lines
  • Loading branch information
jacebrowning committed Feb 13, 2021
2 parents af89998 + 184efac commit 34a9f80
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Revision History

## 8.1

- Updated APIs that accept `text_lines[]` to support `/` as a placeholder for blank lines.

## 8.0

- Renamed `template_key` to `template_id` in APIs.
Expand Down
10 changes: 5 additions & 5 deletions app/api/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
@doc.response(200, str, description="Your API key is valid")
@doc.response(401, str, description="Your API key is invalid")
async def validate(request):
return (
response.json({"message": "Your API key is valid."}, status=200)
if utils.meta.authenticated(request)
else response.json({"message": "Your API key is invalid."}, status=401)
)
valid = utils.meta.authenticated(request, allow_email=True)
status = 200 if valid else 401
state = "valid" if valid else "invalid"
message = f"Your API key is {state}."
return response.json({"message": message}, status=status)


@blueprint.get("/images/preview.jpg")
Expand Down
2 changes: 1 addition & 1 deletion app/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def configure(app):
app.config.API_HOST = app.config.SERVER_NAME = settings.SERVER_NAME
app.config.API_BASEPATH = "/"
app.config.API_SCHEMES = [settings.SCHEME]
app.config.API_VERSION = "8.0"
app.config.API_VERSION = "8.1"
app.config.API_TITLE = "Memegen.link"
app.config.API_CONTACT_EMAIL = "support@maketested.com"
app.config.API_LICENSE_NAME = "View the license"
Expand Down
14 changes: 14 additions & 0 deletions app/tests/test_apis_clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,20 @@ def it_returns_401_when_unauthenticated(expect, client):
expect(response.status) == 401
expect(response.json) == {"message": "Your API key is invalid."}

def it_accepts_email_addresses(expect, client):
request, response = client.get(
"/auth", headers={"X-API-KEY": "user@example.com"}
)
expect(response.status) == 200
expect(response.json) == {"message": "Your API key is valid."}

def it_rejects_invalid_email_addresses(expect, client):
request, response = client.get(
"/auth", headers={"X-API-KEY": "user@example"}
)
expect(response.status) == 401
expect(response.json) == {"message": "Your API key is invalid."}


def describe_image_preview():
@pytest.fixture
Expand Down
6 changes: 6 additions & 0 deletions app/tests/test_apis_memes.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ def it_drops_trailing_blank_lines(expect, client):
expect(response.status) == 201
expect(response.json) == {"url": "http://localhost:5000/images/iw.png"}

def it_supports_slashes_to_indicate_blank_lines(expect, client):
data = {"template_id": "iw", "text_lines": ["/", "2", "/", ""]}
request, response = client.post("/images", data=data)
expect(response.status) == 201
expect(response.json) == {"url": "http://localhost:5000/images/iw/_/2.png"}


def describe_detail():
@pytest.mark.parametrize(
Expand Down
7 changes: 6 additions & 1 deletion app/utils/meta.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from email.utils import parseaddr
from urllib.parse import unquote, urlparse

import aiohttp
Expand All @@ -6,13 +7,17 @@
from .. import settings


def authenticated(request) -> bool:
def authenticated(request, *, allow_email: bool = False) -> bool:
api_key = _get_api_key(request)
if api_key:
api_mask = api_key[:2] + "***" + api_key[-2:]
logger.info(f"Authenticated with {api_mask}")
if api_key in settings.API_KEYS:
return True
if allow_email:
name, email = parseaddr(api_key)
if "@" in email and "." in email:
return True
return False


Expand Down
4 changes: 3 additions & 1 deletion app/utils/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ def encode(lines: list[str]) -> str:
encoded_lines = []

for line in lines:
if line:
if line == "/":
encoded_lines.append("_")
elif line:
encoded = line
for before, after in [
("_", "__"),
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[tool.poetry]

name = "memegen"
version = "8.0"
version = "8.1"
description = "The free and open source API to generate memes."
authors = ["Jace Browning <support@maketested.com>"]
license = "MIT"
Expand Down

0 comments on commit 34a9f80

Please sign in to comment.