POST log_download_url の呼び出しサンプル

サンプルコード

POST log_download_url を呼び出すサンプルコードをご紹介します。

下記のコードをep-api_logdownload.pyというファイル名で保存します。

import base64
import contextlib
import hashlib
import hmac
import json
import ssl
import time
import urllib.request
import urllib.parse
import subprocess


ENDPOINT = "ep.api.iij.jp"
API_VERSION = "20190625"
API_NAME = "log_download_url"
HTTP_METHOD = "POST"
ACCESS_KEY = b"YOUR ACCESS KEY HERE"
SECRET_KEY = b"YOUR SECRET KEY HERE"
SIGNATURE_VERSION = "2"
SIGNATURE_METHOD = "HmacSHA256"
CONTENT_TYPE = "application/json"
EXPIRE = time.strftime("%FT%H:%M:%SZ", time.gmtime(time.time() + 12 * 60 * 60))

SERVICE_CODE = "YEMxxxxxxxx"
CHILD_SERVICE_CODE = "yexxxxxxxx"
MASTER_ID = "xxxxxxxx"
LOG_DATE = "YYYYMMDD"
FILENAME = './log.zip'


# make signature
path = "/r/{}/{}/{}/{}.json".format(
    API_VERSION, SERVICE_CODE, CHILD_SERVICE_CODE, API_NAME
)

header_strings = CONTENT_TYPE + "\n"
header_strings += "x-iijapi-expire" + ":" + EXPIRE + "\n"
header_strings += "x-iijapi-signaturemethod" + ":" + SIGNATURE_METHOD + "\n"
header_strings += "x-iijapi-signatureversion" + ":" + SIGNATURE_VERSION

string_to_sign = "\n".join([HTTP_METHOD.upper(), "", header_strings, path])
signature = hmac.new(
    SECRET_KEY, string_to_sign.encode("utf-8"), hashlib.sha256
).digest()
signature = base64.b64encode(signature)

# setup SSL context
context = ssl.create_default_context()
https_handler = urllib.request.HTTPSHandler(context=context)
opener = urllib.request.build_opener(https_handler)
urllib.request.install_opener(opener)

# make request
headers = {
    "Authorization": b"IIJAPI " + ACCESS_KEY + b":" + signature,
    "Content-Type": CONTENT_TYPE,
    "x-iijapi-expire": EXPIRE,
    "x-iijapi-SignatureMethod": SIGNATURE_METHOD,
    "x-iijapi-SignatureVersion": SIGNATURE_VERSION,
}

data = {"LogDate": LOG_DATE}

url = "https://{}{}".format(ENDPOINT, path)
request = urllib.request.Request(url, json.dumps(data).encode(), headers=headers)

# call EP-API
with contextlib.closing(urllib.request.urlopen(request)) as conn:
    response = conn.read()

resp_dict = json.loads(response.decode())

encrypt_key = resp_dict["APIResult"]["EncryptKey"]

# encode master id
cmd1 = "echo -n {}".format(MASTER_ID).split(" ")
cmd2 = "openssl enc -md md5 -aes-128-cbc -e -nosalt -base64 -pass pass:{}".format(encrypt_key).split(" ")

p1 = subprocess.Popen(cmd1, stdout=subprocess.PIPE)
p2 = subprocess.Popen(cmd2, stdin=p1.stdout, stdout=subprocess.PIPE)

master_id_enc = p2.communicate()[0].decode()[:-1]
download_url = resp_dict["APIResult"]["LogDownloadURL"]

url = "{}?MasterID={}".format(download_url, urllib.parse.quote(master_id_enc, safe=''))
request = urllib.request.Request(url, headers=headers)

data = urllib.request.urlopen(request).read()
with open(FILENAME, mode="wb") as f:
    f.write(data)

注意点

ファイル内の以下の行を適宜変更します。

ACCESS_KEY = b"YOUR ACCESS KEY HERE"
SECRET_KEY = b"YOUR SECRET KEY HERE"
SERVICE_CODE = "YEMxxxxxxxx"
CHILD_SERVICE_CODE = "yexxxxxxxx"
MASTER_ID = "xxxxxxxx"
LOG_DATE = "YYYYMMDD"

実行例

下記の実行例は python 3.10.12 環境で実行しています。

$ python ep-api_logdownload.py

正常に終了すると、log.zip というファイル名でログがダウンロードされます。

$ ls -l  log.zip
-rw-r--r-- 1 user group 1406139 Nov 13 17:46 log.zip