このエントリーをはてなブックマークに追加


PowerShellでCoinExchange.ioで扱う通貨の情報を取得する方法・getcurrency

getcurrency APIは、getcurrencies APIにより返却されたCurrencyID, TickerCodeを指定することにより
指定したID(CurrencyID), コード(TickerCode)の通貨情報を返却します。
以下、PowerShellによる、TickerCode, CurrencyIDを指定して、指定した通貨情報を取得する操作例を記します。


CoinExchange.io へのリンク

CoinExchange.io

追記

  • 2018/7/5
    「サーバーによってプロトコル違反が発生しました. Section=ResponseHeader Detail=CR の後には LF を指定しなければなりません。」
    が出力されるようであれば、以下リンク記事を参考にしてください。
  • Section=ResponseHeader Detail=CR の後には LF...の対処方法

関連資料・記事

CurrencyID, TickerCodeは、getcurrencies APIにより一覧を取得することができます。~

getcurrencies APIについては、以下のリンク先の記事を参照してください。

メジャーな仮想通貨のCurrencyID, TickerCodeを取得してみます。

PS C:\> [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
PS C:\> $currencies = Invoke-RestMethod -UseBasicParsing "https://www.coinexchange.io/api/v1/getcurrencies"
PS C:\> $target = @("bitcoin", "bitcoin cash", "litecoin", "ethereum")
PS C:\> $currencies.result | % { if ($target -contains $_.Name) { $_} } | Format-Table

CurrencyID Name         TickerCode WalletStatus Type
---------- ----         ---------- ------------ ----
344        Bitcoin Cash BCH        online       currency
1          Bitcoin      BTC        online       currency
70         Ethereum     ETH        online       currency
2          Litecoin     LTC        online       currency

上記の出力をみると、CurrencyID, TickerCode が取得できているのが確認できます。
以下、このCurrencyID, TickerCodeを利用します。

getcurrency APIの仕様

CoinExchange.io API v1 Referenceの抜粋です。

Get Currency
 
 This endpoint retrieves information about a single currency / asset.
HTTP Request

GET https://www.coinexchange.io/api/v1/getcurrency?currency_id=1

or

GET https://www.coinexchange.io/api/v1/getcurrency?ticker_code=BTC
Query Parameters
Parameter    Type     Description
currency_id  integer  Determines the currency to be returned by id
ticker_code  string   Dtermines the currency to be returned by ticker code
'getcurrency’ returns JSON structured like this:

{
    "success":"1",
    "request":"\/api\/v1\/getcurrency",
    "message":"",
    "result":{
        "CurrencyID":"1",
        "Name":"Bitcoin",
        "TickerCode":"BTC",
        "WalletStatus":"online",
        "Type":"currency"
    }
}

CurrencyID, TickerCodeを指定して通貨情報を取得

以下、CurrencyID, TickerCodeを指定して通貨情報を取得します。

  • currency_id = 1 (=BTC)
    PS C:\> [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
    PS C:\> $currency = Invoke-RestMethod -UseBasicParsing "https://www.coinexchange.io/api/v1/getcurrency?currency_id=1"
    PS C:\> $currency
    
    success request             message result
    ------- -------             ------- ------
    1       /api/v1/getcurrency         @{CurrencyID=1; Name=Bitcoin; TickerCode=BTC; WalletStatus=online; Type=currency}
    
    PS C:\> $currency.result
    
    
    CurrencyID   : 1
    Name         : Bitcoin
    TickerCode   : BTC
    WalletStatus : online
    Type         : currency
  • ticker_code=ETH
    PS C:\> $currency = Invoke-RestMethod -UseBasicParsing "https://www.coinexchange.io/api/v1/getcurrency?ticker_code=ETH"
    PS C:\> $currency
    
    success request             message result
    ------- -------             ------- ------
    1       /api/v1/getcurrency         @{CurrencyID=70; Name=Ethereum; TickerCode=ETH; WalletStatus=online; Type=currency}
    
    PS C:\> $currency.result
    
    
    CurrencyID   : 70
    Name         : Ethereum
    TickerCode   : ETH
    WalletStatus : online
    Type         : currency

上記の出力の通り、BTCとETHの通貨情報が取得できました。

HTTPステータスなどを一緒に取得したい場合はInvoke-WebRequestが便利

上記では、Invoke-RestMethodにてJSONをPowerShellで扱いやすい状態に変換してくれますが、
Webサーバから返却された値を変換せずに取得したい場合は、Invoke-WebRequestコマンドレットを使用します。
また、取得後にJSONをPowerShellで扱いやすいオブジェクトに変換するには、ConvertFrom-Jsonコマンドレットを使用します。

  1. セキュリティプロトコルをTLS1.2に変更します。(2018/5/31時点、TLS1.2を指定しないと動作しなくなったので追記)
    PS C:\> [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
  2. Invoke-WebRequestコマンドレットでAPIを呼び出し返却された情報を変数に格納しています。
    PS C:\> $response = Invoke-WebRequest -UseBasicParsing "https://www.coinexchange.io/api/v1/getcurrency?ticker_code=ETH"
  3. 返却された情報を表示してみます。
    PS C:\> $response
    
    
    StatusCode        : 200
    StatusDescription : OK
    Content           : {"success":"1","request":"\/api\/v1\/getcurrency","message":"","result":{"CurrencyID":"70","Name":"
                        Ethereum","TickerCode":"ETH","WalletStatus":"online","Type":"currency"}}
    RawContent        : HTTP/1.1 200 OK
                        Connection: keep-alive
                        x-frame-options: SAMEORIGIN
                        Content-Length: 171
                        Cache-Control: no-cache
                        Content-Type: application/json
                        Date: Wed, 20 Jun 2018 05:34:43 GMT
                        Set-Cookie: vis...
    Forms             :
    Headers           : {[Connection, keep-alive], [x-frame-options, SAMEORIGIN], [Content-Length, 171], [Cache-Control, no
                        -cache]...}
    Images            : {}
    InputFields       : {}
    Links             : {}
    ParsedHtml        :
    RawContentLength  : 171
  4. 各種HTTPの情報が簡単に取得できます。
    PS C:\> $response.StatusCode
    200
    PS C:\> $response.Headers.'Content-Type'
    application/json
  5. ConvertFrom-Jsonを使うとContentの内容をInvoke-RestMethodのresultと同様にできます。
    PS C:\> $json = ConvertFrom-Json($response.Content)
    PS C:\> $json
    
    success request             message result
    ------- -------             ------- ------
    1       /api/v1/getcurrency         @{CurrencyID=70; Name=Ethereum; TickerCode=ETH; WalletStatus=online; Type=currency}
    
    
    PS C:\> $json.result
    
    
    CurrencyID   : 70
    Name         : Ethereum
    TickerCode   : ETH
    WalletStatus : online
    Type         : currency

Webの応答を読み取っています。(Waiting for response)を非表示にする方法

上記のコマンドレットを実行すると、APIサーバとのやり取りのプログレスメッセージが表示されます。

02.png

これを非表示にしたい場合は、以下のようにしてください。

非表示にするには、$ProgressPreferenceにSilentlyContinueを設定します。
変更前は以下のように Continue が設定されています。

PS C:\> $ProgressPreference
Continue

非表示にするには、以下のように変更します。

PS C:\> $ProgressPreference = "SilentlyContinue"

以上、PowerShellを使って、CoinExchange.io の getcurrency APIを使って
通貨情報を取得する操作例でした。


CoinExchange.io へのリンク

CoinExchange.io

添付ファイル: file02.png 209件 [詳細]

トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2018-06-20 (水) 14:42:42