import { ApiRoute } from "@/utils/type"; import classNames from "classnames"; import { useState } from "react"; import Highlight from "react-highlight"; import { BiLogoPython, BiSolidCopy } from "react-icons/bi"; import { Options } from "redaxios"; export const PythonSnippet = ({ endpoint, headers, parameters, body, onCopyToClipboard, }: { endpoint: ApiRoute; parameters?: Record; headers?: Record; body?: Options | undefined; onCopyToClipboard: (e: string) => void; }) => { const [isCopied, setIsCopied] = useState(false); const generatePythonRequestFromEndpoint = () => { const { method, path } = endpoint; const fullpath = `${process.env.NEXT_PUBLIC_APP_APIURL}${path}`; const removeEmptyValues = (data: Record) => { const formattedData = { ...data }; Object.entries(formattedData).forEach(([key, value]) => { if (!value) { delete formattedData[key]; } if (typeof value === "boolean") { formattedData[key] = value ? "True" : "False"; } }); return formattedData; }; const Dict: Record = { GET: () => { const filteredEmptyParameters = removeEmptyValues(parameters ?? {}); return `import requests response = requests.get( "${fullpath}", params=${JSON.stringify(filteredEmptyParameters)}, headers=${JSON.stringify(headers)} )`; }, DELETE: () => { const formattedBody = removeEmptyValues(body ?? {}); return `import requests response = requests.delete( "${fullpath}", data=${JSON.stringify(formattedBody)}, headers=${JSON.stringify(headers)} )`; }, DEFAULT: () => { const formattedBody = removeEmptyValues(body ?? {}); return `import requests response = requests.${method.toLocaleLowerCase()}( "${fullpath}", json=${JSON.stringify(formattedBody)}, headers=${JSON.stringify(headers)} )`; }, }; return Dict[method] ? Dict[method]() : Dict["DEFAULT"](); }; const handleCopy = () => { onCopyToClipboard(generatePythonRequestFromEndpoint()); setIsCopied(true); setTimeout(() => { setIsCopied(false); }, 1000); }; return (

Python

{generatePythonRequestFromEndpoint()}
Copied!
); };