#coding=utf-8
import hashlib
import json
import random
import requests
import time
from collections import OrderedDict
from Crypto.PublicKey import RSA
from Crypto.Hash import MD5, SHA1, SHA256
from Crypto.Signature import PKCS1_v1_5 as Signature_PKC
import base64
from requests_toolbelt.multipart.encoder import MultipartEncoder

# 接口调用地址，
# 正式环境：https://oapi.acsign.cn 
# 沙箱环境域名：https://prev.acsign.cn

#构建post请求
class HttpUtils(object):
    # 构建请求头
    @staticmethod
    def doPOST(url, reqBodyData, appId, appKey, pathstr=None, file_key=None, file_name=None):
        accept = "*/*"
        # 请求的与实体对应的MIME信息
        contentType = "multipart/form-data; charset=UTF-8"
            
        # 对请求的url进行签名
        accountsApiUrl = url
        # 毫米时间戳获取
        t = time.time()
        # 毫秒级时间戳
        _timestamp = str(round(t * 1000) + 1000 * 60)

        # 按照阿拉伯字符排序
        jsonstr = json.dumps(reqBodyData, sort_keys=True,separators=(',',':'), ensure_ascii=False)
        data = dict()
        data["appId"] = appId
        data["timestamp"] = _timestamp
        data["bizData"] = jsonstr

        #计算md5值
        m = hashlib.md5()
        m.update(jsonstr.encode('utf-8'))
        # 二进制数据字符串值
        md5_str = m.hexdigest()
        sign_str = '%s%s%s%s' % (jsonstr, md5_str, appId, _timestamp)
        # 打印签名值
        private_keyBytes = base64.b64decode(appKey)
        rsakey = RSA.importKey(private_keyBytes)
        signer = Signature_PKC.new(rsakey)
        # 根据SHA256算法处理签名内容data
        sha_data = SHA1.new(sign_str.encode("utf-8"))
        # 私钥进行签名
        signature = base64.b64encode(signer.sign(sha_data))
        
        if(pathstr != None):
           m = MultipartEncoder(
	   	       fields={
                   'appId': appId,
                   'bizData': jsonstr,
                   'timestamp': _timestamp,
          	       'Content-Type': 'application/octet-stream',
                   'type': 'application/octet-stream',
                   file_key: (file_name, open(pathstr, 'rb'), 'application/octet-stream')
               },
               boundary='-----------------------------' + str(random.randint(1e28, 1e29 - 1)))
        else:
            m = MultipartEncoder(
	   	       fields={
                   'appId': appId,
                   'bizData': jsonstr,
                   'timestamp': _timestamp,
          	       'Content-Type': 'application/octet-stream',
                   'type': 'application/octet-stream'
               },
               boundary='-----------------------------' + str(random.randint(1e28, 1e29 - 1)))

        #添加文件
        # 构建请求头
        headers = {
            'Content-Type': contentType,
            'Accept': accept,
            'sign': signature
        }

        headers['Content-Type'] = m.content_type
        r = requests.post(accountsApiUrl, data=m, headers=headers)

        return r.text
