-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/content type enhancement (#58)
* Adding JWT, plain, zip, gzip, jpg, webp generation for content type
- Loading branch information
Showing
16 changed files
with
313 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
python_sources(name="src") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import base64 | ||
import quopri | ||
from enum import Enum | ||
from typing import Optional | ||
|
||
|
||
class ContentEncoding(str, Enum): | ||
SEVEN_BIT = "7-bit" | ||
EIGHT_BIT = "8-bit" | ||
BINARY = "binary" | ||
QUOTED_PRINTABLE = "quoted-printable" | ||
BASE16 = "base-16" | ||
BASE32 = "base-32" | ||
BASE64 = "base-64" | ||
|
||
|
||
def binary_encoder(string: str) -> str: | ||
return "".join(format(x, "b") for x in bytearray(string, "utf-8")) | ||
|
||
|
||
def bytes_str_repr(b: bytes) -> str: | ||
return repr(b)[2:-1] | ||
|
||
|
||
def seven_bit_encoder(string: str) -> str: | ||
return bytes_str_repr(string.encode("utf-7")) | ||
|
||
|
||
def eight_bit_encoder(string: str) -> str: | ||
return bytes_str_repr(string.encode("utf-8")) | ||
|
||
|
||
def quoted_printable_encoder(string: str) -> str: | ||
return bytes_str_repr(quopri.encodestring(string.encode("utf-8"))) | ||
|
||
|
||
def b16_encoder(string: str) -> str: | ||
return bytes_str_repr(base64.b16encode(string.encode("utf-8"))) | ||
|
||
|
||
def b32_encoder(string: str) -> str: | ||
return bytes_str_repr(base64.b32encode(string.encode("utf-8"))) | ||
|
||
|
||
def b64_encoder(string: str) -> str: | ||
return bytes_str_repr(base64.b64encode(string.encode("utf-8"))) | ||
|
||
|
||
Encoder = { | ||
ContentEncoding.SEVEN_BIT: seven_bit_encoder, | ||
ContentEncoding.EIGHT_BIT: eight_bit_encoder, | ||
ContentEncoding.BINARY: binary_encoder, | ||
ContentEncoding.QUOTED_PRINTABLE: quoted_printable_encoder, | ||
ContentEncoding.BASE16: b16_encoder, | ||
ContentEncoding.BASE32: b32_encoder, | ||
ContentEncoding.BASE64: b64_encoder, | ||
} | ||
|
||
|
||
def encode(string: str, encoding: Optional[ContentEncoding]) -> str: | ||
return Encoder.get(encoding, lambda s: s)(string) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
python_sources(name="src") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from jsf.schema_types.string_utils.content_type.application__gzip import create_random_gzip | ||
from jsf.schema_types.string_utils.content_type.application__jwt import create_random_jwt | ||
from jsf.schema_types.string_utils.content_type.application__zip import create_random_zip | ||
from jsf.schema_types.string_utils.content_type.image__jpeg import random_jpg | ||
from jsf.schema_types.string_utils.content_type.image__webp import random_webp | ||
from jsf.schema_types.string_utils.content_type.text__plain import random_fixed_length_sentence | ||
|
||
|
||
def not_implemented(*args, **kwargs): | ||
raise NotImplementedError() | ||
|
||
|
||
ContentTypeGenerator = { | ||
"application/jwt": create_random_jwt, | ||
# "text/html": not_implemented, | ||
# "application/xml": not_implemented, # To implement: Port code from https://onlinerandomtools.com/generate-random-xml | ||
# "image/bmp": not_implemented, # To implement: request jpg and convert to bmp | ||
# "text/css": not_implemented, | ||
# "text/csv": not_implemented, | ||
# "image/gif": not_implemented, # To implement: request jpg and convert to gif | ||
"image/jpeg": random_jpg, | ||
# "application/json": not_implemented, # To implement: Port code from https://onlinerandomtools.com/generate-random-xml | ||
# "text/javascript": not_implemented, | ||
# "image/png": not_implemented, # To implement: request jpg and convert to png | ||
# "image/tiff": not_implemented, # To implement: request jpg and convert to tiff | ||
"text/plain": random_fixed_length_sentence, | ||
"image/webp": random_webp, | ||
"application/zip": create_random_zip, | ||
"application/gzip": create_random_gzip, | ||
# "application/x-bzip": not_implemented, # To implement: create in memory random files using text/plain then zip | ||
# "application/x-bzip2": not_implemented, # To implement: create in memory random files using text/plain then zip | ||
# "application/pdf": not_implemented, # To implement: request jpg and convert to pdf and/or make pdf using python package | ||
# "text/calendar": not_implemented, | ||
} | ||
|
||
|
||
def generate(content_type: str, min_length: int, max_length: int) -> str: | ||
return ContentTypeGenerator.get(content_type, not_implemented)(min_length, max_length) |
16 changes: 16 additions & 0 deletions
16
jsf/schema_types/string_utils/content_type/application__gzip.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import gzip | ||
import io | ||
|
||
from jsf.schema_types.string_utils.content_encoding import bytes_str_repr | ||
from jsf.schema_types.string_utils.content_type.application__zip import create_random_file_name | ||
from jsf.schema_types.string_utils.content_type.text__plain import random_fixed_length_sentence | ||
|
||
|
||
def create_random_gzip(*args, **kwargs) -> str: | ||
fgz = io.BytesIO() | ||
gzip_obj = gzip.GzipFile(filename=create_random_file_name(), mode="wb", fileobj=fgz) | ||
gzip_obj.write(random_fixed_length_sentence().encode("utf-8")) | ||
gzip_obj.close() | ||
|
||
fgz.seek(0) | ||
return bytes_str_repr(fgz.getvalue()) |
48 changes: 48 additions & 0 deletions
48
jsf/schema_types/string_utils/content_type/application__jwt.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import base64 | ||
import hashlib | ||
import hmac | ||
import json | ||
import secrets | ||
from datetime import timezone | ||
|
||
from faker import Faker | ||
|
||
faker = Faker() | ||
|
||
|
||
def base64url_encode(input: bytes): | ||
return base64.urlsafe_b64encode(input).decode("utf-8").replace("=", "") | ||
|
||
|
||
def jwt(api_key, expiry, api_sec): | ||
|
||
segments = [] | ||
|
||
header = {"typ": "JWT", "alg": "HS256"} | ||
payload = {"iss": api_key, "exp": expiry} | ||
|
||
json_header = json.dumps(header, separators=(",", ":")).encode() | ||
json_payload = json.dumps(payload, separators=(",", ":")).encode() | ||
|
||
segments.append(base64url_encode(json_header)) | ||
segments.append(base64url_encode(json_payload)) | ||
|
||
signing_input = ".".join(segments).encode() | ||
key = api_sec.encode() | ||
signature = hmac.new(key, signing_input, hashlib.sha256).digest() | ||
|
||
segments.append(base64url_encode(signature)) | ||
|
||
encoded_string = ".".join(segments) | ||
|
||
return encoded_string | ||
|
||
|
||
def create_random_jwt(*args, **kwargs): | ||
|
||
api_key = secrets.token_urlsafe(16) | ||
api_sec = secrets.token_urlsafe(16) | ||
|
||
expiry = int(faker.date_time(timezone.utc).timestamp()) | ||
|
||
return jwt(api_key, expiry, api_sec) |
27 changes: 27 additions & 0 deletions
27
jsf/schema_types/string_utils/content_type/application__zip.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import io | ||
import random | ||
import zipfile | ||
from typing import Tuple | ||
|
||
import rstr | ||
|
||
from jsf.schema_types.string_utils.content_encoding import bytes_str_repr | ||
from jsf.schema_types.string_utils.content_type.text__plain import random_fixed_length_sentence | ||
|
||
|
||
def create_random_file_name() -> str: | ||
return rstr.xeger(r"[a-zA-Z0-9]+\.txt") | ||
|
||
|
||
def create_random_file() -> Tuple[str, io.BytesIO]: | ||
return (create_random_file_name(), io.BytesIO(random_fixed_length_sentence().encode("utf-8"))) | ||
|
||
|
||
def create_random_zip(*args, **kwargs) -> str: | ||
zip_buffer = io.BytesIO() | ||
|
||
with zipfile.ZipFile(zip_buffer, "a", zipfile.ZIP_DEFLATED, False) as zip_file: | ||
for file_name, data in [create_random_file() for _ in range(random.randint(1, 10))]: | ||
zip_file.writestr(file_name, data.getvalue()) | ||
|
||
return bytes_str_repr(zip_buffer.getvalue()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import random | ||
|
||
import requests | ||
|
||
from jsf.schema_types.string_utils.content_encoding import bytes_str_repr | ||
|
||
|
||
def random_jpg(*args, **kwargs) -> str: | ||
return bytes_str_repr( | ||
requests.get( | ||
f"https://picsum.photos/{random.randint(1,50)*10}/{random.randint(1,50)*10}.jpg" | ||
).content | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import random | ||
|
||
import requests | ||
|
||
from jsf.schema_types.string_utils.content_encoding import bytes_str_repr | ||
|
||
|
||
def random_webp(*args, **kwargs) -> str: | ||
return bytes_str_repr( | ||
requests.get( | ||
f"https://picsum.photos/{random.randint(1,50)*10}/{random.randint(1,50)*10}.webp" | ||
).content | ||
) |
Oops, something went wrong.