Skip to main content

The CDN Rule Engine SDK for Python

Project description

CDN Rule Engine Python SDK

Installation

require python version >= 3.5

    pip3 install --user byteplus-cdn-rule-engine-sdk

Upgrade sdk version from old one

    pip3 install --upgrade byteplus-cdn-rule-engine-sdk

Examples

示例1

设置规则,任意条件下,设置缓存时间为0秒。

from rule_engine.Const import Const
from rule_engine.Rule import Rule, Condition, Action


def get_rule_example_1():
    rule = Rule()
    condition = Condition({
        "IsGroup": False,
        "Connective": Const.ConnectiveAnd,
        "Condition": {
            "Object": Const.ConditionAlways,
        }
    })
    action = Action({
        "Action": Const.ActionCacheTime,
        "Groups": [{
            "Dimension": Const.ActionCacheTime,
            "GroupParameters": [{
                "Parameters": [
                    {
                        "Name":"ttl",
                        "Values": ["0"]
                    },
                    {
                        "Name":"ttl_unit",
                        "Values": ["sec"]
                    },
                    {
                        "Name":"cache_policy",
                        "Values": [Const.CacheDefault]
                    },
                    {
                        "Name":"force_cache",
                        "Values": ["true"]
                    }
                ]
            }]
        }]
    })
    rule.if_block.condition = condition
    rule.if_block.actions.append(action)
    return {
        "Switch": True,
        "Rules": [
            {
                "Name": "test",
                "Rule": rule.encode_to_string()
            }
        ]
    }

示例2

设置规则,当客户端请求协议为http时,添加客户端响应头X-Rsp-1=123

from rule_engine.Const import Const
from rule_engine.Rule import Rule, Condition, Action

def get_rule_example_2():
    rule = Rule()
    condition = Condition({
        "IsGroup": False,
        "Connective": Const.ConnectiveAnd,
        "Condition": {
            "Object": Const.ConditionHTTPProtocol,
            "Operator": Const.OperatorEqual,
            "Value": [Const.HTTP]
        }
    })
    action = Action({
        "Action": Const.ActionResponseHeader,
        "Groups": [{
            "Dimension": Const.ActionResponseHeader,
            "GroupParameters": [{
                "Parameters": [
                    {
                        "Name": "action",
                        "Values": [Const.Set]
                    },
                    {
                        "Name": "header_name",
                        "Values": ["X-Rsp-1"]
                    },
                    {
                        "Name": "header_value",
                        "Values": ["123"]
                    }
                ]
            }]
        }]
    })
    rule.if_block.condition = condition
    rule.if_block.actions.append(action)
    return {
        "Switch": True,
        "Rules": [
            {
                "Name": "test",
                "Rule": rule.encode_to_string()
            }
        ]
    }

示例3

查询当前配置(假设是示例2),并将头部值修改为111;同时添加一个动作:开启视频拖拽。

from rule_engine.Const import Const
from rule_engine.Rule import Rule, Condition, Action

def get_rule_example_3():
    re_config = get_rule_example_2()
    rule = Rule()
    rule.decode_from_string(re_config['Rules'][0]['Rule'])
    for act in rule.if_block.actions:
        if act.action == Const.ActionResponseHeader:
            act.groups[0].group_parameters[0].set("header_value", ["111"])
    rule.if_block.actions.append(Action({
        "Action": Const.ActionVideoDrag,
        "Groups": [{
            "Dimension": Const.ActionVideoDrag,
            "GroupParameters": [{
                "Parameters": [
                    {
                        "Name": "switch",
                        "Values": [Const.SwitchTrue]
                    }
                ]
            }]
        }]
    }))
    re_config['Rules'][0]['Rule'] = rule.encode_to_string()
    return re_config

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cdn_rule_engine_sdk-0.0.1.tar.gz (12.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cdn_rule_engine_sdk-0.0.1-py3-none-any.whl (12.6 kB view details)

Uploaded Python 3

File details

Details for the file cdn_rule_engine_sdk-0.0.1.tar.gz.

File metadata

  • Download URL: cdn_rule_engine_sdk-0.0.1.tar.gz
  • Upload date:
  • Size: 12.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.6

File hashes

Hashes for cdn_rule_engine_sdk-0.0.1.tar.gz
Algorithm Hash digest
SHA256 047012a93a122c1b7e1c6786154bac649df08ae81caeae9c089f76d569b3168f
MD5 7bcd43fcb82ecfa99460d585647260c4
BLAKE2b-256 8f4e6cb819d6478347f6e49ab74a4ff2a88bcdf3d85c9b6a4c7dc0ae853d1073

See more details on using hashes here.

File details

Details for the file cdn_rule_engine_sdk-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for cdn_rule_engine_sdk-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 db7074e61d084d834526fd4b9de209eda14770bf4d169e9dce50a05f4f7040de
MD5 d193878d8c8b41ebaaf3dfa7253f79c7
BLAKE2b-256 99f154890f94fd52d9bc423e0ba2bad9f998a9add967d109b9563c024cbf4882

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page