Skip to content

Commit

Permalink
Add postman request types
Browse files Browse the repository at this point in the history
  • Loading branch information
ll931217 committed Sep 14, 2024
1 parent b0d4d84 commit 9bce787
Showing 1 changed file with 69 additions and 36 deletions.
105 changes: 69 additions & 36 deletions src/posting/importing/postman.py
Original file line number Diff line number Diff line change
@@ -1,76 +1,109 @@
from pathlib import Path
from typing import List
from typing import Any, List
import json
import os
from unittest.mock import Base
from pydantic import BaseModel
import yaml

from rich.console import Console

from posting.collection import APIInfo, Collection


def create_env_file(
path: Path, env_filename: str, variables: list[dict[str, str]]
) -> Path:
class Variable(BaseModel):
key: str
value: str
type: str | None
disabled: bool | None


class Body(BaseModel):
mode: str
raw: str | None
options: dict


class Url(BaseModel):
raw: str
host: List[str]
path: List[str]
query: List[Variable]


class PostmanRequest(BaseModel):
method: str
header: List[Variable]
url: str
description: str | None
body: Body | None


class RequestItem(BaseModel):
name: str
item: List["RequestItem"] | None
request: PostmanRequest | None


def create_env_file(path: Path, env_filename: str, variables: list[Variable]) -> Path:
env_content: list[str] = []

for var in variables:
key = var["key"]
value = var["value"]
env_content.append(f"{key!r}={value!r}")
env_content.append(f"{var.key!r}={var.value!r}")

env_file = path / env_filename
env_file.write_text("\n".join(env_content))
return env_file


def generate_directory_structure(
items: List[dict], current_path: str = "", base_path: Path = Path("")
items: List[RequestItem], current_path: str = "", base_path: Path = Path("")
) -> List[str]:
directories = []
for item in items:
if "item" in item:
folder_name = item["name"]
if item.item is not None:
folder_name = item.name
new_path = f"{current_path}/{folder_name}" if current_path else folder_name
full_path = Path(base_path) / new_path
os.makedirs(str(full_path), exist_ok=True)
directories.append(str(full_path))
generate_directory_structure(item["item"], new_path, base_path)
elif "request" in item:
request_name = item["name"]
generate_directory_structure(item.item, new_path, base_path)
if item.request is not None:
request_name = item.name
file_name = f"{request_name}.posting.yaml"
full_path = Path(base_path) / current_path / file_name
create_request_file(full_path, item)
return directories


def create_request_file(file_path: Path, request_data: dict):
yaml_content = {
"name": request_data["name"],
"description": request_data.get("description", ""),
"method": request_data["request"]["method"],
"url": request_data["request"]["url"]["raw"],
def create_request_file(file_path: Path, request_data: RequestItem):
yaml_content: dict[str, Any] = {
"name": request_data.name,
}

# Add body if present
if "body" in request_data["request"]:
yaml_content["body"] = {
"content": request_data["request"]["body"].get("raw", "")
}

# Add headers
if "header" in request_data["request"]:
if request_data.request is not None:
yaml_content["method"] = request_data.request.method
yaml_content["headers"] = [
{"name": header["key"], "value": header["value"]}
for header in request_data["request"]["header"]
]

# Add query params
if "query" in request_data["request"]["url"]:
yaml_content["params"] = [
{"name": param["key"], "value": param["value"]}
for param in request_data["request"]["url"]["query"]
{"name": header.key, "value": header.value}
for header in request_data.request.header
]
if isinstance(request_data.request.url, Url):
yaml_content["url"] = request_data.request.url.raw
yaml_content["params"] = [
{"name": param.key, "value": param.value}
for param in request_data.request.url.query
]
else:
yaml_content["url"] = request_data.request.url

if request_data.request.description is not None:
yaml_content["description"] = request_data.request.description

if (
request_data.request.body is not None
and request_data.request.body.raw is not None
):
yaml_content["body"] = {"content": request_data.request.body.raw}

# Write YAML file
with open(file_path, "w") as f:
Expand Down

0 comments on commit 9bce787

Please sign in to comment.