CDN Rule Engine Python SDK
Installation
require python version >= 3.5
pip3 install cdn-rule-engine-sdk
Upgrade sdk version from old one
pip3 install --upgrade cdn-rule-engine-sdk
Examples
示例1
设置规则,任意条件下,设置缓存时间为0秒。
from cdn_rule_engine_sdk.rule_engine.Const import Const
from cdn_rule_engine_sdk.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 cdn_rule_engine_sdk.rule_engine.Const import Const
from cdn_rule_engine_sdk.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 cdn_rule_engine_sdk.rule_engine.Const import Const
from cdn_rule_engine_sdk.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
Release files for cdn-rule-engine-sdk 0.0.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| cdn_rule_engine_sdk-0.0.4.tar.gz | 12.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| cdn_rule_engine_sdk-0.0.4-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 24.8 kB
Release files / cdn_rule_engine_sdk-0.0.4.tar.gz
| Download URL | cdn_rule_engine_sdk-0.0.4.tar.gz |
|---|---|
| Size | 12.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d69d0e9bf59e77478c1ab03eb2ecf87562e9093bd91ea5c94d79d0852ff01cf6
|
|
BLAKE2b-256 checksum How to use checksums |
d06695e3024acd7e8e0b80858b52abc13160fe2004d5650e85a53d794963ccd9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.9.6
|
Release files / cdn_rule_engine_sdk-0.0.4-py3-none-any.whl
| Download URL | cdn_rule_engine_sdk-0.0.4-py3-none-any.whl |
|---|---|
| Size | 12.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d1645faa077040803cb0325d91c0cdeb0adbeec083c048bcccb682884e7c7576
|
|
BLAKE2b-256 checksum How to use checksums |
9561fe3abd50784ec1aef9f27596314e7276f11dd69be89c6756fdd4c5ec3a15
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.9.6
|