Skip to content

Commit

Permalink
fix: 添加实例化参数 #7626
Browse files Browse the repository at this point in the history
  • Loading branch information
guohelu committed Dec 17, 2024
1 parent 5a9ad5d commit b4fe30b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 35 deletions.
42 changes: 17 additions & 25 deletions gcloud/contrib/template_market/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@


class MarketAPIClient:
def __init__(self):
def __init__(self, username):
self.base_url = settings.TEMPLATE_MARKET_API_URL
self.headers = {
"X-Bkapi-Authorization": json.dumps(
{"bk_app_code": settings.APP_CODE, "bk_app_secret": settings.SECRET_KEY}
{"bk_app_code": settings.APP_CODE, "bk_app_secret": settings.SECRET_KEY, "bk_username": username}
),
"Content-Type": "application/json",
}
Expand All @@ -31,34 +31,26 @@ def _get_url(self, endpoint):

def _make_request(self, method, endpoint, **kwargs):
url = self._get_url(endpoint)

headers = {**self.headers}
bk_username = kwargs.get("username")
if bk_username:
headers["bk_username"] = bk_username

response = requests.request(method, url, headers=headers, json=kwargs.get("data"))
response = requests.request(method, url, headers=self.headers, json=kwargs.get("data"))
return response.json()

def get_service_category(self, username):
return self._make_request("GET", "/category/get_service_category/", username=username)
def get_service_category(self):
return self._make_request("GET", "/category/get_service_category/")

def get_scene_label(self, username):
return self._make_request("GET", "/sre_property/scene_label/", username=username)
def get_scene_label(self):
return self._make_request("GET", "/sre_property/scene_label/")

def get_risk_level(self, username):
return self._make_request("GET", "/sre_scene/risk_level/", username=username)
def get_risk_level(self):
return self._make_request("GET", "/sre_scene/risk_level/")

def get_template_scene_detail(self, market_record_id, username):
return self._make_request("GET", f"/sre_scene/flow_template_scene/{market_record_id}/", username=username)
def get_template_scene_detail(self, market_record_id):
return self._make_request("GET", f"/sre_scene/flow_template_scene/{market_record_id}/")

def get_template_scene_list(self, username):
return self._make_request("GET", "/sre_scene/flow_template_scene/?is_all=true", username=username)
def get_template_scene_list(self):
return self._make_request("GET", "/sre_scene/flow_template_scene/?is_all=true")

def create_template_scene(self, data, username):
return self._make_request("POST", "/sre_scene/flow_template_scene/", username=username, data=data)
def create_template_scene(self, data):
return self._make_request("POST", "/sre_scene/flow_template_scene/", data=data)

def patch_template_scene(self, data, market_record_id, username):
return self._make_request(
"PATCH", f"/sre_scene/flow_template_scene/{market_record_id}/", username=username, data=data
)
def patch_template_scene(self, data, market_record_id):
return self._make_request("PATCH", f"/sre_scene/flow_template_scene/{market_record_id}/", data=data)
24 changes: 14 additions & 10 deletions gcloud/contrib/template_market/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class TemplateSceneViewSet(viewsets.ViewSet):
serializer_class = TemplateSharedRecordSerializer
permission_classes = [permissions.IsAuthenticated, SharedTemplateRecordPermission]

market_client = MarketAPIClient()
market_client = MarketAPIClient

def _build_template_data(self, serializer, **kwargs):
templates = TaskTemplate.objects.filter(id__in=serializer.validated_data["template_ids"], is_deleted=False)
Expand All @@ -81,30 +81,34 @@ def _handle_response(self, response_data, error_message):

@action(detail=False, methods=["get"])
def get_service_category(self, request, *args, **kwargs):
response_data = self.market_client.get_service_category(username=request.user.username)
client = self.market_client(username=request.user.username)
response_data = client.get_service_category()
error_response = self._handle_response(response_data, "Failed to obtain scene category")
if error_response:
return error_response
return Response({"result": True, "data": response_data["data"], "code": err_code.SUCCESS.code})

@action(detail=False, methods=["get"])
def get_scene_label(self, request, *args, **kwargs):
response_data = self.market_client.get_scene_label(username=request.user.username)
client = self.market_client(username=request.user.username)
response_data = client.get_scene_label()
error_response = self._handle_response(response_data, "Failed to obtain scene tag list")
if error_response:
return error_response
return Response({"result": True, "data": response_data["data"], "code": err_code.SUCCESS.code})

@action(detail=False, methods=["get"])
def get_risk_level(self, request, *args, **kwargs):
response_data = self.market_client.get_risk_level(username=request.user.username)
client = self.market_client(username=request.user.username)
response_data = client.get_risk_level()
error_response = self._handle_response(response_data, "Failed to obtain the market risk level list")
if error_response:
return error_response
return Response({"result": True, "data": response_data["data"], "code": err_code.SUCCESS.code})

def list(self, request, *args, **kwargs):
response_data = self.market_client.get_template_scene_list(username=request.user.username)
client = self.market_client(username=request.user.username)
response_data = client.get_template_scene_list()
error_response = self._handle_response(response_data, "Failed to obtain the market template list")
if error_response:
return error_response
Expand All @@ -116,7 +120,8 @@ def create(self, request, *args, **kwargs):
serializer.is_valid(raise_exception=True)

data = self._build_template_data(serializer)
response_data = self.market_client.create_template_scene(data, username=request.user.username)
client = self.market_client(username=request.user.username)
response_data = client.create_template_scene(data)
error_response = self._handle_response(response_data, "Failed to create market template record")
if error_response:
return error_response
Expand All @@ -135,15 +140,14 @@ def partial_update(self, request, *args, **kwargs):
serializer = self.serializer_class(data=request.data, partial=True)
serializer.is_valid(raise_exception=True)

existing_records = self.market_client.get_template_scene_detail(
market_record_id, username=request.user.username
)
client = self.market_client(username=request.user.username)
existing_records = client.get_template_scene_detail(market_record_id)
existing_market_template_ids = set(
[template["id"] for template in json.loads(existing_records["data"]["templates"])]
)

data = self._build_template_data(serializer, market_record_id=market_record_id)
response_data = self.market_client.patch_template_scene(data, market_record_id, username=request.user.username)
response_data = client.patch_template_scene(data, market_record_id)
error_response = self._handle_response(response_data, "Failed to update market template record")
if error_response:
return error_response
Expand Down

0 comments on commit b4fe30b

Please sign in to comment.