forked from Alexey/DAS_2024_1
125 lines
4.1 KiB
Python
125 lines
4.1 KiB
Python
|
import uuid
|
||
|
import json
|
||
|
from flask import Flask, request, jsonify
|
||
|
from flask_cors import CORS
|
||
|
import requests
|
||
|
from requests.adapters import HTTPAdapter
|
||
|
from requests.packages.urllib3.util.retry import Retry
|
||
|
|
||
|
class Vacancy:
|
||
|
def __init__(self, uuid, title, company, description, salary, resume_uuid):
|
||
|
self.uuid = uuid
|
||
|
self.title = title
|
||
|
self.company = company
|
||
|
self.description = description
|
||
|
self.salary = salary
|
||
|
self.resume_uuid = resume_uuid
|
||
|
self.resume_info = None
|
||
|
|
||
|
def to_dict(self):
|
||
|
if self.resume_info:
|
||
|
return {
|
||
|
"uuid": self.uuid,
|
||
|
"title": self.title,
|
||
|
"company": self.company,
|
||
|
"description": self.description,
|
||
|
"salary": self.salary,
|
||
|
"resume_uuid": self.resume_uuid,
|
||
|
"resume_info": self.resume_info
|
||
|
}
|
||
|
else:
|
||
|
return {
|
||
|
"uuid": self.uuid,
|
||
|
"title": self.title,
|
||
|
"company": self.company,
|
||
|
"description": self.description,
|
||
|
"salary": self.salary,
|
||
|
"resume_uuid": self.resume_uuid
|
||
|
}
|
||
|
|
||
|
app = Flask(__name__)
|
||
|
CORS(app)
|
||
|
|
||
|
vacancies = {}
|
||
|
|
||
|
@app.route("/vacancies", methods=["GET"])
|
||
|
def get_vacancies():
|
||
|
return jsonify([vacancy.to_dict() for vacancy in vacancies.values()])
|
||
|
|
||
|
@app.route("/vacancies/<vacancy_uuid>", methods=["GET"])
|
||
|
def get_vacancy(vacancy_uuid):
|
||
|
vacancy = vacancies.get(vacancy_uuid)
|
||
|
if vacancy:
|
||
|
if not vacancy.resume_info:
|
||
|
vacancy.resume_info = get_resume_info(vacancy.resume_uuid)
|
||
|
return jsonify(vacancy.to_dict())
|
||
|
else:
|
||
|
return jsonify({"error": "Vacancy not found"}), 404
|
||
|
|
||
|
@app.route("/vacancies", methods=["POST"])
|
||
|
def create_vacancy():
|
||
|
data = request.get_json()
|
||
|
vacancy_uuid = str(uuid.uuid4())
|
||
|
vacancy = Vacancy(
|
||
|
vacancy_uuid,
|
||
|
data["title"],
|
||
|
data["company"],
|
||
|
data["description"],
|
||
|
data["salary"],
|
||
|
data["resume_uuid"],
|
||
|
)
|
||
|
vacancies[vacancy_uuid] = vacancy
|
||
|
vacancy.resume_info = get_resume_info(vacancy.resume_uuid)
|
||
|
return jsonify(vacancy.to_dict()), 201
|
||
|
|
||
|
@app.route("/vacancies/<vacancy_uuid>", methods=["PUT"])
|
||
|
def update_vacancy(vacancy_uuid):
|
||
|
vacancy = vacancies.get(vacancy_uuid)
|
||
|
if vacancy:
|
||
|
data = request.get_json()
|
||
|
vacancy.title = data.get("title", vacancy.title)
|
||
|
vacancy.company = data.get("company", vacancy.company)
|
||
|
vacancy.description = data.get("description", vacancy.description)
|
||
|
vacancy.salary = data.get("salary", vacancy.salary)
|
||
|
vacancy.resume_uuid = data.get("resume_uuid", vacancy.resume_uuid)
|
||
|
vacancy.resume_info = get_resume_info(vacancy.resume_uuid)
|
||
|
return jsonify(vacancy.to_dict())
|
||
|
else:
|
||
|
return jsonify({"error": "Vacancy not found"}), 404
|
||
|
|
||
|
@app.route("/vacancies/<vacancy_uuid>", methods=["DELETE"])
|
||
|
def delete_vacancy(vacancy_uuid):
|
||
|
vacancy = vacancies.get(vacancy_uuid)
|
||
|
if vacancy:
|
||
|
del vacancies[vacancy_uuid]
|
||
|
return "", 200
|
||
|
else:
|
||
|
return jsonify({"error": "Vacancy not found"}), 404
|
||
|
|
||
|
def get_resume_info(resume_uuid):
|
||
|
url = f'http://resumes-service:5001/resumes/{resume_uuid}'
|
||
|
|
||
|
# Настройка retry механизма
|
||
|
retries = Retry(
|
||
|
total=3, # Максимальное количество повторов
|
||
|
status_forcelist=[429, 500, 502, 503, 504], # Коды статуса, для которых нужно повторить запрос
|
||
|
backoff_factor=0.3, # Время ожидания перед повторной попыткой
|
||
|
)
|
||
|
adapter = HTTPAdapter(max_retries=retries)
|
||
|
http = requests.Session()
|
||
|
http.mount("https://", adapter)
|
||
|
http.mount("http://", adapter)
|
||
|
|
||
|
try:
|
||
|
response = http.get(url)
|
||
|
if response.status_code == 200:
|
||
|
return response.json()
|
||
|
else:
|
||
|
return None
|
||
|
except requests.exceptions.RequestException as e:
|
||
|
print(f"Ошибка при запросе к resumes-service: {e}")
|
||
|
return None
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
app.run(debug=True, host="0.0.0.0", port=5000)
|