Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DP-5290 python-sdk/cdn: 查询域名带宽,支持type参数 #456

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion examples/cdn_bandwidth.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
查询指定域名指定时间段内的带宽
"""
import qiniu
from qiniu import CdnManager
from qiniu import CdnManager, DataType


# 账户ak,sk
Expand All @@ -31,3 +31,9 @@

print(ret)
print(info)

ret, info = cdn_manager.get_bandwidth_data(
urls, startDate, endDate, granularity, data_type=DataType.BANDWIDTH)

print(ret)
print(info)
2 changes: 1 addition & 1 deletion qiniu/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
build_batch_stat, build_batch_delete, build_batch_restoreAr, build_batch_restore_ar
from .services.storage.uploader import put_data, put_file, put_stream
from .services.storage.upload_progress_recorder import UploadProgressRecorder
from .services.cdn.manager import CdnManager, create_timestamp_anti_leech_url, DomainManager
from .services.cdn.manager import CdnManager, DataType, create_timestamp_anti_leech_url, DomainManager
from .services.processing.pfop import PersistentFop
from .services.processing.cmd import build_op, pipe_cmd, op_save
from .services.compute.app import AccountClient
Expand Down
20 changes: 17 additions & 3 deletions qiniu/services/cdn/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,17 @@

from qiniu.compat import is_py2
from qiniu.compat import is_py3
from enum import Enum

import hashlib

class DataType(Enum):
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing class docstring (missing-class-docstring)

Details

lint 解释

  • 该 lint 出现表示一个类(class)缺少文档字符串(docstring)。文档字符串是用于描述类、方法或函数的注释,它可以帮助其他开发者理解代码的用途和功能。在 Python 中,通常使用三引号(""" 或 ''')来定义文档字符串。

错误用法

class MyClass:
    def my_method(self):
        pass

正确用法

  • 方案一:添加类文档字符串
class MyClass:
    """这是一个示例类,用于演示如何添加类文档字符串。"""

    def my_method(self):
        pass
  • 方案二:如果不需要文档字符串,可以考虑是否需要该类
# 如果不需要文档字符串,可以考虑是否需要该类
class MyClass:
    def my_method(self):
        pass
  • 方案三:使用类型注解(Type Annotations)
from typing import List, Dict

class MyClass:
    """这是一个示例类,用于演示如何添加类文档字符串。"""

    def __init__(self, data: List[Dict[str, int]]):
        self.data = data

    def my_method(self) -> None:
        pass
  • 方案四:使用类型注解和文档字符串结合
from typing import List, Dict

class MyClass:
    """这是一个示例类,用于演示如何添加类文档字符串。"""

    def __init__(self, data: List[Dict[str, int]]):
        """
        初始化方法。

        :param data: 一个包含字典的列表
        :type data: List[Dict[str, int]]
        """
        self.data = data

    def my_method(self) -> None:
        """这是一个示例方法,用于演示如何添加方法文档字符串。"""
        pass

💡 以上内容由 AI 辅助生成,如有疑问欢迎反馈交流

BANDWIDTH = 'bandwidth'
X302BANDWIDTH = '302bandwidth'
X302MBANDWIDTH = '302mbandwidth'
FLOW = 'flow'
X302FLOW = '302flow'
X302MFLOW = '302mflow'

def urlencode(str):
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing function or method docstring (missing-function-docstring)

Details

lint 解释

  • 该 lint 出现表示一个函数或方法缺少文档字符串(docstring)。文档字符串是用于描述函数或方法用途、参数和返回值的注释。它有助于其他开发者理解代码的功能,提高代码的可读性和可维护性。

错误用法

def calculate_area(width, height):
    return width * height

正确用法

  • 方案一:添加文档字符串
def calculate_area(width, height):
    """
    计算矩形的面积
    
    参数:
    width (int): 矩形的宽度
    height (int): 矩形的高度
    
    返回:
    int: 矩形的面积
    """
    return width * height
  • 方案二:如果函数或方法非常简单,可以不添加文档字符串,但最好在代码中留下注释说明其用途。
def calculate_area(width, height):
    # 计算矩形的面积
    return width * height

💡 以上内容由 AI 辅助生成,如有疑问欢迎反馈交流

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redefining built-in 'str' (redefined-builtin)

Details

lint 解释

  • 该 lint 出现表示在代码中重新定义了内置的 str 变量。这通常会导致命名冲突,因为 str 是 Go 语言中的一个内置类型,重新定义它可能会导致意外的行为或编译错误。

错误用法

package main

import "fmt"

func main() {
	str := "Hello, World!" // 正确使用内置的 str 类型
	fmt.Println(str)

	str := "Another string" // 重新定义内置的 str 变量,导致命名冲突
	fmt.Println(str)
}

正确用法

package main

import "fmt"

func main() {
	myString := "Hello, World!" // 使用自定义变量名避免命名冲突
	fmt.Println(myString)

	anotherString := "Another string" // 使用另一个自定义变量名
	fmt.Println(anotherString)
}
  • 方案二:避免使用内置类型名称作为变量名,以防止命名冲突。

💡 以上内容由 AI 辅助生成,如有疑问欢迎反馈交流

if is_py2:
Expand Down Expand Up @@ -60,7 +68,7 @@ def refresh_urls_and_dirs(self, urls, dirs):
Returns:
一个dict变量和一个ResponseInfo对象
参考代码 examples/cdn_manager.py
"""
"""
req = {}
if urls is not None and len(urls) > 0:
req.update({"urls": urls})
Expand Down Expand Up @@ -89,7 +97,7 @@ def prefetch_urls(self, urls):
url = '{0}/v2/tune/prefetch'.format(self.server)
return self.__post(url, body)

def get_bandwidth_data(self, domains, start_date, end_date, granularity):
def get_bandwidth_data(self, domains, start_date, end_date, granularity, data_type=None):
"""
查询带宽数据,文档 https://developer.qiniu.com/fusion/api/traffic-bandwidth

Expand All @@ -98,6 +106,7 @@ def get_bandwidth_data(self, domains, start_date, end_date, granularity):
start_date: 起始日期
end_date: 结束日期
granularity: 数据间隔
data_type: 计量数据类型, see class DataType.XXXBANDWIDTH

Returns:
一个dict变量和一个ResponseInfo对象
Expand All @@ -108,12 +117,14 @@ def get_bandwidth_data(self, domains, start_date, end_date, granularity):
req.update({"startDate": start_date})
req.update({"endDate": end_date})
req.update({"granularity": granularity})
if data_type is not None:
req.update({'type': data_type.value}) # should be one of 'bandwidth', '302bandwidth', '302mbandwidth'

body = json.dumps(req)
url = '{0}/v2/tune/bandwidth'.format(self.server)
return self.__post(url, body)

def get_flux_data(self, domains, start_date, end_date, granularity):
def get_flux_data(self, domains, start_date, end_date, granularity, data_type=None):
"""
查询流量数据,文档 https://developer.qiniu.com/fusion/api/traffic-bandwidth

Expand All @@ -122,6 +133,7 @@ def get_flux_data(self, domains, start_date, end_date, granularity):
start_date: 起始日期
end_date: 结束日期
granularity: 数据间隔
data_type: 计量数据类型, see class DataType.XXXFLOW

Returns:
一个dict变量和一个ResponseInfo对象
Expand All @@ -132,6 +144,8 @@ def get_flux_data(self, domains, start_date, end_date, granularity):
req.update({"startDate": start_date})
req.update({"endDate": end_date})
req.update({"granularity": granularity})
if data_type is not None:
req.update({'type': data_type.value}) # should be one of 'flow', '302flow', '302mflow'

body = json.dumps(req)
url = '{0}/v2/tune/flux'.format(self.server)
Expand Down