Add files via upload
This commit is contained in:
13
proxy/Dockerfile
Normal file
13
proxy/Dockerfile
Normal file
@@ -0,0 +1,13 @@
|
||||
FROM python:3.11-slim-bookworm
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
COPY requirements.txt .
|
||||
|
||||
RUN pip install -r requirements.txt
|
||||
|
||||
EXPOSE 5009
|
||||
|
||||
COPY . .
|
||||
|
||||
CMD [ "python", "app.py" ]
|
||||
57
proxy/app.py
Normal file
57
proxy/app.py
Normal file
@@ -0,0 +1,57 @@
|
||||
import json
|
||||
import time
|
||||
|
||||
import cherrypy
|
||||
import requests
|
||||
|
||||
from mock_data import get_mock_data
|
||||
|
||||
# тестовый режим с возможностью заказа питсы с гравием(!)
|
||||
MOCK = False
|
||||
|
||||
API_URL = 'https://api.papajohns.ru'
|
||||
|
||||
|
||||
class PapaJohnsProxy(object):
|
||||
@cherrypy.expose
|
||||
def default(self, *args, **kwargs):
|
||||
path = cherrypy.request.path_info
|
||||
method = cherrypy.request.method
|
||||
headers = dict(cherrypy.request.headers)
|
||||
query_string = cherrypy.request.query_string
|
||||
data = cherrypy.request.body.read() if cherrypy.request.body.length else None
|
||||
|
||||
if MOCK:
|
||||
time.sleep(1)
|
||||
return json.dumps(get_mock_data(path))
|
||||
|
||||
url = f'{API_URL}{path}'
|
||||
|
||||
if query_string:
|
||||
url += f'?{query_string}'
|
||||
|
||||
headers_to_forward = {
|
||||
k: v for k, v in headers.items()
|
||||
if k.lower() not in ['host', 'content-length']
|
||||
}
|
||||
|
||||
try:
|
||||
response = requests.request(
|
||||
method=method,
|
||||
url=url,
|
||||
headers=headers_to_forward,
|
||||
data=data,
|
||||
timeout=10
|
||||
)
|
||||
|
||||
cherrypy.response.status = response.status_code
|
||||
return response.text
|
||||
|
||||
except requests.exceptions.RequestException as e:
|
||||
cherrypy.response.status = 500
|
||||
return json.dumps({"error": str(e)})
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
cherrypy.config.update({'server.socket_host': '0.0.0.0', 'server.socket_port': 5009})
|
||||
cherrypy.quickstart(PapaJohnsProxy())
|
||||
7
proxy/docker-compose.yml
Normal file
7
proxy/docker-compose.yml
Normal file
@@ -0,0 +1,7 @@
|
||||
services:
|
||||
pizza-proxy:
|
||||
build: .
|
||||
container_name: pizza-proxy
|
||||
ports:
|
||||
- 5009:5009
|
||||
restart: unless-stopped
|
||||
222
proxy/mock_data.py
Normal file
222
proxy/mock_data.py
Normal file
@@ -0,0 +1,222 @@
|
||||
def get_mock_data(path: str) -> dict:
|
||||
if '/order/save' in path:
|
||||
return ORDER_SUCCESS
|
||||
return ORDER_FAIL
|
||||
if '/order/status' in path:
|
||||
if ORDER_STATUS["order_status"] < 5:
|
||||
ORDER_STATUS["order_status"] += 1
|
||||
return ORDER_STATUS
|
||||
if '/cart/add' in path:
|
||||
ORDER_STATUS["order_status"] = -1
|
||||
return CART_ADD
|
||||
if '/catalog/category-goods' in path:
|
||||
return PIZZAS
|
||||
|
||||
|
||||
CART_ADD = {
|
||||
"unauthorized_token": "7904e2634334760a642a169c0f7c67f0",
|
||||
"cart_id": 123456789,
|
||||
"composition": [
|
||||
{
|
||||
"item": {
|
||||
"name": "С ананасами"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
ORDER_FAIL = {
|
||||
"success": False,
|
||||
"message": {
|
||||
"composition": [
|
||||
"Минимальная стоимость заказа на доставку 99999 ₽"
|
||||
]
|
||||
},
|
||||
"status": 400
|
||||
}
|
||||
|
||||
ORDER_SUCCESS = {
|
||||
"order_id": 69420
|
||||
}
|
||||
|
||||
|
||||
ORDER_STATUS = {
|
||||
"order_status": -1
|
||||
}
|
||||
|
||||
|
||||
PIZZAS = [
|
||||
{
|
||||
"goods": [
|
||||
{
|
||||
"id": 1234,
|
||||
"name": "С ананасами",
|
||||
"variations": [
|
||||
{
|
||||
"id": 123430,
|
||||
"price": 599,
|
||||
"kind": {
|
||||
"id": 3
|
||||
},
|
||||
"stuffed_crust": "none",
|
||||
"size": {
|
||||
"value": 23
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": 123431,
|
||||
"price": 879,
|
||||
"kind": {
|
||||
"id": 3
|
||||
},
|
||||
"stuffed_crust": "none",
|
||||
"size": {
|
||||
"value": 30
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": 123432,
|
||||
"price": 1079,
|
||||
"kind": {
|
||||
"id": 3
|
||||
},
|
||||
"stuffed_crust": "none",
|
||||
"size": {
|
||||
"value": 35
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": 123433,
|
||||
"price": 1379,
|
||||
"kind": {
|
||||
"id": 3
|
||||
},
|
||||
"stuffed_crust": "none",
|
||||
"size": {
|
||||
"value": 40
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": 123410,
|
||||
"price": 879,
|
||||
"kind": {
|
||||
"id": 1
|
||||
},
|
||||
"stuffed_crust": "none",
|
||||
"size": {
|
||||
"value": 30
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": 123411,
|
||||
"price": 1079,
|
||||
"kind": {
|
||||
"id": 1
|
||||
},
|
||||
"stuffed_crust": "none",
|
||||
"size": {
|
||||
"value": 35
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": 123412,
|
||||
"price": 1379,
|
||||
"kind": {
|
||||
"id": 1
|
||||
},
|
||||
"stuffed_crust": "none",
|
||||
"size": {
|
||||
"value": 40
|
||||
}
|
||||
}
|
||||
],
|
||||
"good_type": "promotional"
|
||||
},
|
||||
{
|
||||
"id": 4321,
|
||||
"name": "С гравием",
|
||||
"variations": [
|
||||
{
|
||||
"id": 223430,
|
||||
"price": 429,
|
||||
"kind": {
|
||||
"id": 3
|
||||
},
|
||||
"stuffed_crust": "none",
|
||||
"size": {
|
||||
"value": 23
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": 223431,
|
||||
"price": 429,
|
||||
"kind": {
|
||||
"id": 3
|
||||
},
|
||||
"stuffed_crust": "none",
|
||||
"size": {
|
||||
"value": 30
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": 223432,
|
||||
"price": 4279,
|
||||
"kind": {
|
||||
"id": 3
|
||||
},
|
||||
"stuffed_crust": "none",
|
||||
"size": {
|
||||
"value": 35
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": 223433,
|
||||
"price": 4279,
|
||||
"kind": {
|
||||
"id": 3
|
||||
},
|
||||
"stuffed_crust": "none",
|
||||
"size": {
|
||||
"value": 40
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": 223410,
|
||||
"price": 429,
|
||||
"kind": {
|
||||
"id": 1
|
||||
},
|
||||
"stuffed_crust": "none",
|
||||
"size": {
|
||||
"value": 30
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": 223411,
|
||||
"price": 4279,
|
||||
"kind": {
|
||||
"id": 1
|
||||
},
|
||||
"stuffed_crust": "none",
|
||||
"size": {
|
||||
"value": 35
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": 223412,
|
||||
"price": 4279,
|
||||
"kind": {
|
||||
"id": 1
|
||||
},
|
||||
"stuffed_crust": "none",
|
||||
"size": {
|
||||
"value": 40
|
||||
}
|
||||
}
|
||||
],
|
||||
"good_type": "promotional"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
2
proxy/requirements.txt
Normal file
2
proxy/requirements.txt
Normal file
@@ -0,0 +1,2 @@
|
||||
CherryPy==18.9.0
|
||||
requests==2.31.0
|
||||
Reference in New Issue
Block a user