File size: 969 Bytes
cbc00ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# -*- coding: utf-8 -*-
# Copyright (c) Alibaba, Inc. and its affiliates.

import os
from abc import ABC, abstractmethod

from dashscope.common.constants import DASHSCOPE_DISABLE_DATA_INSPECTION_ENV
from dashscope.common.utils import get_user_agent


class BaseRequest(ABC):
    def __init__(self, user_agent: str = "") -> None:
        ua = get_user_agent()

        # Append user_agent if provided and not empty
        if user_agent:
            ua += "; " + user_agent

        self.headers = {"user-agent": ua}
        disable_data_inspection = os.environ.get(
            DASHSCOPE_DISABLE_DATA_INSPECTION_ENV,
            "true",
        )

        if disable_data_inspection.lower() == "false":
            self.headers["X-DashScope-DataInspection"] = "enable"

    @abstractmethod
    def call(self):
        raise NotImplementedError()


class AioBaseRequest(BaseRequest):
    @abstractmethod
    async def aio_call(self):
        raise NotImplementedError()