Spaces:
Paused
Paused
:gem: [Feature] New DataProxyAPI: support /img api for get image by url
Browse files- apis/__init__.py +0 -0
- apis/arg_parser.py +34 -0
- apis/data_proxy_api.py +63 -0
apis/__init__.py
ADDED
|
File without changes
|
apis/arg_parser.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import argparse
|
| 2 |
+
import sys
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
class ArgParser(argparse.ArgumentParser):
|
| 6 |
+
def __init__(self, app_envs={}, *args, **kwargs):
|
| 7 |
+
super().__init__(*args, **kwargs)
|
| 8 |
+
|
| 9 |
+
self.host = app_envs.get("host", "127.0.0.1")
|
| 10 |
+
self.port = app_envs.get("port", 19898)
|
| 11 |
+
self.app_name = app_envs.get("app_name", f"App on {self.host}")
|
| 12 |
+
|
| 13 |
+
self.add_argument(
|
| 14 |
+
"-s",
|
| 15 |
+
"--host",
|
| 16 |
+
type=str,
|
| 17 |
+
default=self.host,
|
| 18 |
+
help=f"Host ({self.host}) for {self.app_name}",
|
| 19 |
+
)
|
| 20 |
+
self.add_argument(
|
| 21 |
+
"-p",
|
| 22 |
+
"--port",
|
| 23 |
+
type=int,
|
| 24 |
+
default=app_envs["port"],
|
| 25 |
+
help=f"Port ({self.port}) for {self.app_name}",
|
| 26 |
+
)
|
| 27 |
+
self.add_argument(
|
| 28 |
+
"-r",
|
| 29 |
+
"--reload",
|
| 30 |
+
action="store_true",
|
| 31 |
+
help="Reload server on code change",
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
self.args, self.unknown_args = self.parse_known_args(sys.argv[1:])
|
apis/data_proxy_api.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import uvicorn
|
| 3 |
+
|
| 4 |
+
from fastapi import FastAPI, Body
|
| 5 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 6 |
+
from fastapi.responses import Response
|
| 7 |
+
from pydantic import BaseModel
|
| 8 |
+
from tclogger import logger
|
| 9 |
+
from typing import Optional
|
| 10 |
+
|
| 11 |
+
from apis.arg_parser import ArgParser
|
| 12 |
+
from configs.envs import DATA_PROXY_APP_ENVS
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
class DataProxyAPI:
|
| 16 |
+
REQUESTS_HEADERS = {
|
| 17 |
+
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36"
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
def __init__(self, app_envs: dict = {}):
|
| 21 |
+
self.title = app_envs.get("app_name")
|
| 22 |
+
self.version = app_envs.get("version")
|
| 23 |
+
self.app = FastAPI(
|
| 24 |
+
docs_url="/",
|
| 25 |
+
title=self.title,
|
| 26 |
+
version=self.version,
|
| 27 |
+
swagger_ui_parameters={"defaultModelsExpandDepth": -1},
|
| 28 |
+
)
|
| 29 |
+
# self.allow_cors()
|
| 30 |
+
self.setup_routes()
|
| 31 |
+
logger.success(f"> {self.title} - v{self.version}")
|
| 32 |
+
|
| 33 |
+
def allow_cors(self):
|
| 34 |
+
self.app.add_middleware(
|
| 35 |
+
CORSMiddleware,
|
| 36 |
+
allow_origins=["*"],
|
| 37 |
+
allow_credentials=True,
|
| 38 |
+
allow_methods=["*"],
|
| 39 |
+
allow_headers=["*"],
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
def get_img(self, url: str):
|
| 43 |
+
response = requests.get(url, headers=self.REQUESTS_HEADERS)
|
| 44 |
+
image_bytes = response.content
|
| 45 |
+
return Response(content=image_bytes, media_type="image/jpeg")
|
| 46 |
+
|
| 47 |
+
def setup_routes(self):
|
| 48 |
+
self.app.get(
|
| 49 |
+
"/img",
|
| 50 |
+
summary="Get image by url",
|
| 51 |
+
)(self.get_img)
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
if __name__ == "__main__":
|
| 55 |
+
app_envs = DATA_PROXY_APP_ENVS
|
| 56 |
+
app = DataProxyAPI(app_envs).app
|
| 57 |
+
app_args = ArgParser(app_envs).args
|
| 58 |
+
if app_args.reload:
|
| 59 |
+
uvicorn.run("__main__:app", host=app_args.host, port=app_args.port, reload=True)
|
| 60 |
+
else:
|
| 61 |
+
uvicorn.run("__main__:app", host=app_args.host, port=app_args.port)
|
| 62 |
+
|
| 63 |
+
# python -m apps.data_proxy_api
|