Sample (Python)
It will show how to generate signature by using Python, and show a sample to request API.
1. Sample for Signature
#coding=utf-8
import hashlib
import hmac
import time
from random import randint
###Configure APP SERVER###
api_server = "https://cdn-api-dev.swiftfederation.com"
def signature():
###Paste your access key id!!!###
access_key_id="V265i4K31j991E19"
###Paste your access key secret!!!###
access_key_secret="06427n2vLExuYH3cn7v5GG1MeYQ7n6S3"
request_method = "POST"
uri = "/v1.0/report/bandwidth"
request_timestamp = time.strftime("%Y%m%dT%H%M%SZ", time.localtime(time.time() + time.timezone))
nonce = str(randint(10000, 99999))
request_body = '{"domains":["cycdnml.yuanzhanapp.com"],"startTime":"2018-12-16T00:00:00Z","endTime":"2018-12-16T00:20:00Z"}'
'''Caculate signature'''
signing_string = "%s\n%s\n%s\n%s\n%s\n%s" % (request_method.upper(), uri, request_timestamp, nonce, access_key_id, request_body)
signature = hex_hmac_sha256(access_key_secret, signing_string)
'''Assemble Authroization value'''
authorization_val = "HMAC-SHA256 " + access_key_id + ":" + signature
print "================ Signature ==================="
print("signature: %s" % signature)
print("")
'''hex hamc sha256 algorithm'''
def hex_hmac_sha256(sign_key, signing_string):
signed_bytes = hmac.new(sign_key, signing_string, digestmod=hashlib.sha256).digest()
return signed_bytes.encode('hex')
signature()
2. Sample for API Request
refer to Get Bandwidth
2.1. Get Bandwidth
- API URL:
https://cdn-api.swiftfederation.com/v1.1/report/bandwidth
- Request Method:
POST
- Request Header:
Authorization: HMAC-SHA256 V265i4K31j991E19:016fc06ec61ce4ba2faf34f3c35c85b6fffe5c6811f9829d2d32a241c5e7f391
Content-Type: application/json; charset=utf-8
x-sfd-date: 20181218T021551Z
x-sfd-nonce: 64593
- Request Body Example
{
"domains": [
"www.example1.com",
"www.example2.com"
],
"startTime": "2018-03-29T17:35:00Z",
"endTime": "2018-03-29T17:45:00Z",
"fillFixedTime": "false",
"interval": "minute"
}
2.2. Request Get Bandwidth API
Please replace 3 parts commented by '###' as below:
- api_server
- access_key_id
- access_key_secret
#coding=utf-8
import json
import traceback
import urllib2
import hashlib
import hmac
import datetime
import time
from random import randint
import requests
###Configure APP SERVER###
api_server = "https://cdn-api-dev.swiftfederation.com"
def get_bandwidth():
###Paste your access key id!!!###
access_key_id="V265i4K31j991E19"
###Paste your access key secret!!!###
access_key_secret="06427n2vLExuYH3cn7v5GG1MeYQ7n6S3"
request_method = "POST"
uri = "/v1.0/report/bandwidth"
request_timestamp = time.strftime("%Y%m%dT%H%M%SZ", time.localtime(time.time() + time.timezone))
nonce = str(randint(10000, 99999))
request_body = '{"domains":["cycdnml.yuanzhanapp.com"],"startTime":"2018-12-16T00:00:00Z","endTime":"2018-12-16T00:20:00Z"}'
'''Caculate signature'''
signing_string = "%s\n%s\n%s\n%s\n%s\n%s" % (request_method.upper(), uri, request_timestamp, nonce, access_key_id, request_body)
signature = hex_hmac_sha256(access_key_secret, signing_string)
'''Assemble Authroization value'''
authorization_val = "HMAC-SHA256 " + access_key_id + ":" + signature
print "================ Signature ==================="
print("signature: %s" % signature)
print("")
print "================ Request ==================="
print("Authorization: %s" % authorization_val)
print("Content-Type: application/json; charset=utf-8")
print("x-sfd-date: %s" % request_timestamp)
print("x-sfd-nonce: %s" % nonce)
print("\r\n")
print(request_body)
print("\r\n")
url = "%s%s" % (api_server, uri)
headers = dict()
headers['Authorization'] = authorization_val
headers['Content-Type'] = "application/json; charset=utf-8"
headers['x-sfd-date'] = request_timestamp
headers['x-sfd-nonce'] = nonce
try:
rs = requests.post(url, data=request_body, headers=headers)
rs.raise_for_status()
except requests.RequestException as e:
traceback.print_exc()
else:
return rs
'''hex hamc sha256 algorithm'''
def hex_hmac_sha256(sign_key, signing_string):
signed_bytes = hmac.new(sign_key, signing_string, digestmod=hashlib.sha256).digest()
return signed_bytes.encode('hex')
ret = get_bandwidth()
print "================ Response ==================="
parsed_output = json.loads(ret.text)
print(parsed_output)