请把Python翻译成Java,谢谢

李海华 李海华 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
import requests  # pip install requests
import json
import base64
import hashlib
import hmac
import os
import time #for nonce
 
class BitfinexClient(object):
    BASE_URL = "https://api.bitfinex.com/"
    KEY=os.environ["BFX_KEY"]
    SECRET=os.environ["BFX_SECRET"]
 
    def _nonce(self):
        """
        Returns a nonce
        Used in authentication
        """
        return str(int(round(time.time() * 10000)))
 
    def _headers(self, path, nonce, body):
        signature = "/api/" + path + nonce + body
        h = hmac.new(self.SECRET, signature, hashlib.sha384)
        signature = h.hexdigest()
        return {
            "bfx-nonce": nonce,
            "bfx-apikey": self.KEY,
            "bfx-signature": signature,
            "content-type": "application/json"
        }
 
    def req(self, path, params = {}):
        nonce = self._nonce()
        body = params
        rawBody = json.dumps(body)
        headers = self._headers(path, nonce, rawBody)
        url = self.BASE_URL + path
        resp = requests.post(url, headers=headers, data=rawBody, verify=True)
        return resp
         
    def active_orders(self):
        """
        Fetch active orders
        """
        response = self.req("v2/auth/r/orders")
        if response.status_code == 200:
          return response.json()
        else:
          print response.status_code
          print response
          return ''
 
client = BitfinexClient()
print client.active_orders()
...全文

相关推荐

评论 6
收藏 已收藏
分享
返回顶部
李海华
非技术区 23,404 成员 70,505 内容