|
| 1 | +module Huobi |
| 2 | + |
| 3 | +export HuobiCommonQuery, |
| 4 | + HuobiPublicQuery, |
| 5 | + HuobiAccessQuery, |
| 6 | + HuobiPrivateQuery, |
| 7 | + HuobiAPIError, |
| 8 | + HuobiClient, |
| 9 | + HuobiData |
| 10 | + |
| 11 | +using Serde |
| 12 | +using Dates, NanoDates, TimeZones, Base64, Nettle |
| 13 | + |
| 14 | +using ..CryptoAPIs |
| 15 | +import ..CryptoAPIs: Maybe, AbstractAPIsError, AbstractAPIsData, AbstractAPIsQuery, AbstractAPIsClient |
| 16 | + |
| 17 | +abstract type HuobiData <: AbstractAPIsData end |
| 18 | +abstract type HuobiCommonQuery <: AbstractAPIsQuery end |
| 19 | +abstract type HuobiPublicQuery <: HuobiCommonQuery end |
| 20 | +abstract type HuobiAccessQuery <: HuobiCommonQuery end |
| 21 | +abstract type HuobiPrivateQuery <: HuobiCommonQuery end |
| 22 | + |
| 23 | +@enum Status begin |
| 24 | + ok |
| 25 | + error |
| 26 | +end |
| 27 | + |
| 28 | +""" |
| 29 | + Data{D} <: AbstractAPIsData |
| 30 | +
|
| 31 | +## Required fields |
| 32 | +- `data::D`: The body data in response. |
| 33 | +
|
| 34 | +## Optional fields |
| 35 | +- `status::Status`: Status of API response. |
| 36 | +- `ch::String`: The data stream. It may be empty as some API doesn't have data stream. |
| 37 | +- `ts::NanoDate`: The UTC timestamp when API respond, the unit is millisecond. |
| 38 | +- `code::Int64`: Response code. |
| 39 | +""" |
| 40 | +struct Data{D} <: AbstractAPIsData |
| 41 | + status::Maybe{Status} |
| 42 | + ch::Maybe{String} |
| 43 | + ts::Maybe{NanoDate} |
| 44 | + code::Maybe{Int64} |
| 45 | + data::D |
| 46 | +end |
| 47 | + |
| 48 | +""" |
| 49 | + DataTick{D} <: AbstractAPIsData |
| 50 | +
|
| 51 | +## Required fields |
| 52 | +- `tick::D`: The body tick in response. |
| 53 | +- `status::Status`: Status of API response. |
| 54 | +- `ch::String`: The data stream. It may be empty as some API doesn't have data stream. |
| 55 | +- `ts::NanoDate`: The UTC timestamp when API respond. |
| 56 | +""" |
| 57 | +struct DataTick{D} <: AbstractAPIsData |
| 58 | + status::Status |
| 59 | + ch::String |
| 60 | + ts::NanoDate |
| 61 | + tick::D |
| 62 | +end |
| 63 | + |
| 64 | +""" |
| 65 | + HuobiClient <: AbstractAPIsClient |
| 66 | +
|
| 67 | +Client info. |
| 68 | +
|
| 69 | +## Required fields |
| 70 | +- `base_url::String`: Base URL for the client. |
| 71 | +
|
| 72 | +## Optional fields |
| 73 | +- `public_key::String`: Public key for authentication. |
| 74 | +- `secret_key::String`: Secret key for authentication. |
| 75 | +- `interface::String`: Interface for the client. |
| 76 | +- `proxy::String`: Proxy information for the client. |
| 77 | +- `account_name::String`: Account name associated with the client. |
| 78 | +- `description::String`: Description of the client. |
| 79 | +""" |
| 80 | +Base.@kwdef struct HuobiClient <: AbstractAPIsClient |
| 81 | + base_url::String |
| 82 | + public_key::Maybe{String} = nothing |
| 83 | + secret_key::Maybe{String} = nothing |
| 84 | + interface::Maybe{String} = nothing |
| 85 | + proxy::Maybe{String} = nothing |
| 86 | + account_name::Maybe{String} = nothing |
| 87 | + description::Maybe{String} = nothing |
| 88 | +end |
| 89 | + |
| 90 | +""" |
| 91 | + HuobiAPIError{T} <: AbstractAPIsError |
| 92 | +
|
| 93 | +Exception thrown when an API method fails with code `T`. |
| 94 | +
|
| 95 | +## Required fields |
| 96 | +- `err_code::String`: Error code. |
| 97 | +- `err_msg::String`: Error message. |
| 98 | +
|
| 99 | +## Optional fields |
| 100 | +- `ts::NanoDate`: he UTC timestamp when API respond. |
| 101 | +- `status::String`: Status of API response. |
| 102 | +- `code::Int64`: Response code. |
| 103 | +""" |
| 104 | +struct HuobiAPIError{T} <: AbstractAPIsError |
| 105 | + err_code::String |
| 106 | + err_msg::String |
| 107 | + ts::Maybe{NanoDate} |
| 108 | + status::Maybe{String} |
| 109 | + data::Nothing |
| 110 | + code::Maybe{Int64} |
| 111 | + |
| 112 | + function HuobiAPIError(err_code::String, err_msg::String, x...) |
| 113 | + return new{Symbol(err_code)}(err_code, err_msg, x...) |
| 114 | + end |
| 115 | +end |
| 116 | + |
| 117 | +function Base.show(io::IO, e::HuobiAPIError) |
| 118 | + return print(io, "code = ", "\"", e.err_code, "\"", ", ", "msg = ", "\"", e.err_msg, "\"") |
| 119 | +end |
| 120 | + |
| 121 | +CryptoAPIs.error_type(::HuobiClient) = HuobiAPIError |
| 122 | + |
| 123 | +function CryptoAPIs.request_sign!(::HuobiClient, query::Q, ::String)::Q where {Q<:HuobiPublicQuery} |
| 124 | + return query |
| 125 | +end |
| 126 | + |
| 127 | +function CryptoAPIs.request_sign!(client::HuobiClient, query::Q, endpoint::String)::Nothing where {Q<:HuobiPrivateQuery} |
| 128 | + query.AccessKeyId = client.public_key |
| 129 | + query.Timestamp = now(UTC) |
| 130 | + query.SignatureMethod = "HmacSHA256" |
| 131 | + query.SignatureVersion = "2" |
| 132 | + query.Signature = nothing |
| 133 | + body::String = Serde.to_query(query) |
| 134 | + endpoint = string("/", endpoint) |
| 135 | + host = last(split(client.base_url, "//")) |
| 136 | + salt = join(["GET", host, endpoint, body], "\n") |
| 137 | + query.Signature = Base64.base64encode(digest("sha256", client.secret_key, salt)) |
| 138 | + return nothing |
| 139 | +end |
| 140 | + |
| 141 | +function CryptoAPIs.request_body(::Q)::String where {Q<:HuobiPublicQuery} |
| 142 | + return "" |
| 143 | +end |
| 144 | + |
| 145 | +function CryptoAPIs.request_body(query::Q)::String where {Q<:HuobiPrivateQuery} |
| 146 | + return Serde.to_query(query) |
| 147 | +end |
| 148 | + |
| 149 | +function CryptoAPIs.request_query(query::Q)::String where {Q<:HuobiPublicQuery} |
| 150 | + return Serde.to_query(query) |
| 151 | +end |
| 152 | + |
| 153 | +function CryptoAPIs.request_query(::Q)::String where {Q<:HuobiPrivateQuery} |
| 154 | + return "" |
| 155 | +end |
| 156 | + |
| 157 | +function CryptoAPIs.request_headers(client::HuobiClient, ::HuobiCommonQuery)::Vector{Pair{String,String}} |
| 158 | + return Pair{String,String}[ |
| 159 | + "Content-Type" => "application/json", |
| 160 | + ] |
| 161 | +end |
| 162 | + |
| 163 | +include("Utils.jl") |
| 164 | +include("Errors.jl") |
| 165 | + |
| 166 | +include("Spot/Spot.jl") |
| 167 | +using .Spot |
| 168 | + |
| 169 | +end |
0 commit comments