#author("2018-07-05T00:20:14+09:00","","")
#author("2018-07-05T01:39:56+09:00","","")
#navi(../)
* PowerShellでCoinExchange.ioの通貨一覧情報を取得する・getcurrencies [#t7bbb3ec]
&htmlinsert(coinexchange.io.html);の''getcurrencies'' APIをPowerShellで呼び出す方法の記事になります。

----
&htmlinsert(coinexchange.io.html);へのリンク~
#htmlinsert(coinexchange.io.logo.html)

#contents

* 追記 [#t9a7c049]
-2018/7/5~
''「サーバーによってプロトコル違反が発生しました. Section=ResponseHeader Detail=CR の後には LF を指定しなければなりません。」''~
が出力されるようであれば、以下リンク記事を参考にしてください。~
-[[Section=ResponseHeader Detail=CR の後には LF...の対処方法>http://win.just4fun.biz/?PowerShell/%E4%BE%8B%E5%A4%96...Section%3DResponseHeader%20Detail%3DCR%20%E3%81%AE%E5%BE%8C%E3%81%AB%E3%81%AF%20LF...%E3%81%AE%E5%AF%BE%E5%87%A6%E6%96%B9%E6%B3%95]]


* 関連資料・記事 [#na55a333]
-[[CoinExchange.io API v1 Reference>http://coinexchangeio.github.io/slate/]]
-[[マーケット一覧情報取得・getmarkets(PowerShell)>API/CoinExchange.io/マーケット一覧情報取得・getmarkets(PowerShell)]]
-[[マーケットサマリ情報取得・getmarketsummaries(PowerShell)>API/CoinExchange.io/マーケットサマリ情報取得・getmarketsummaries(PowerShell)]]
-[[指定した通貨ペアのマーケットサマリ情報を取得・getmarketsummary(PowerShell)>API/CoinExchange.io/指定した通貨ペアのマーケットサマリ情報を取得・getmarketsummary(PowerShell)]]
-[[MarketID一覧情報を取得する>API/CoinExchange.io/MarketID一覧を取得する・getmarkets(PowerShell)]]
-[[仮想通貨一覧を取得・getcurrencies>API/CoinExchange.io/仮想通貨一覧を取得・getcurrencies(PowerShell)]]
-[[指定した通貨の通貨情報を取得する・getcurrency>API/CoinExchange.io/指定した通貨の通貨情報を取得する・getcurrency(PowerShell)]]


* getcurrencies APIの仕様 [#h7d2c4a5]
以下、[[CoinExchange.io API v1 Reference>http://coinexchangeio.github.io/slate/]] からの抜粋です。

 Get Currencies
 This endpoint retrieves all enabled currencies / assets.
 
 HTTP Request
 GET https://www.coinexchange.io/api/v1/getcurrencies
 
 'getcurrencies’ JSON return:
 {
    "success":"1",
    "request":"\/api\/v1\/getcurrencies",
    "message":"","result": [
        {"CurrencyID":"1","Name":"Bitcoin","TickerCode":"BTC","WalletStatus":"online","Type":"currency"},
        {"CurrencyID":"2","Name":"Darkcoin","TickerCode":"DRK","WalletStatus":"offline","Type":"currency"},
        {"CurrencyID":"3","Name":"Ethereum","TickerCode":"ETH","WalletStatus":"online","Type":"currency"}
    ]
 }

* getcurrencies API をブラウザでアクセスしてみた [#ud3940f8]
getcurrencies API をFirefoxでアクセスしてみました。~
https://www.coinexchange.io/api/v1/getcurrencies
#ref(01.png)

* PowerShellのInvoke-RestMethodを使用してAPIを呼び出してみる [#ncfd1307]
PowerShellのInvoke-RestMethodコマンドレットを使用して、getcurrencies APIを呼び出し操作してみます。~
以下、getcurrencies APIを呼び出し操作するサンプル手順です。

セキュリティプロトコルをTLS1.2に変更します。(2018/5/31時点、TLS1.2を指定しないと動作しなくなったので追記)
 PS C:\> [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
 PS C:\> $currencies = Invoke-RestMethod -UseBasicParsing "https://www.coinexchange.io/api/v1/getcurrencies"
 PS C:\> $currencies
  
 success request               message result
 ------- -------               ------- ------
 1       /api/v1/getcurrencies         {@{CurrencyID=44; Name=Elite; TickerCode=1337; WalletStatus=online; Type=currency}, @{CurrencyID...

Invoke-RestMethodコマンドレットでgetcurrencies APIを呼び出すことにより、値が取得できます。~
実際の通貨一覧は、以下のようにして表示することができます。
 PS C:\> $currencies.result | Format-Table
 
 CurrencyID Name                        TickerCode WalletStatus Type
 ---------- ----                        ---------- ------------ ----
 44         Elite                       1337       online       currency
 218        Ganjacoin                   420G       offline      currency
 296        SixEleven                   611        online       currency
 404        Adcoin                      ACC        online       currency
 527        Accelerator                 ACCL       online       ethereum_asset
 <省略>

以下、Bitcoinのcurrency情報を確認する手順です。
 PS C:\> $currencies.result | Where-Object { $_.Name -eq "bitcoin" }
 
 
 CurrencyID   : 1
 Name         : Bitcoin
 TickerCode   : BTC
 WalletStatus : online
 Type         : currency

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

上記では、Invoke-RestMethodにてJSONをPowerShellで扱いやすい状態に変換してくれますが、~
Webサーバから返却された値を変換せずに取得したい場合は、Invoke-WebRequestコマンドレットを使用します。~
また、取得後にJSONをPowerShellで扱いやすいオブジェクトに変換するには、''ConvertFrom-Json''コマンドレットを使用します。~
以下に実行例を記します。
+ セキュリティプロトコルをTLS1.2に変更します。(2018/5/31時点、TLS1.2を指定しないと動作しなくなったので追記)
 PS C:\> [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
+ Invoke-WebRequestコマンドレットでAPIを呼び出し返却された情報を変数に格納しています。
 PS C:\> $response = Invoke-WebRequest -UseBasicParsing "https://www.coinexchange.io/api/v1/getcurrencies"
+返却された情報を表示してみます。
 PS C:\> $response
 
 
 StatusCode        : 200
 StatusDescription : OK
 Content           : {"success":"1","request":"\/api\/v1\/getcurrencies","message":"","result":[{"CurrencyID":"44","Name":"Elite","TickerC
                     ode":"1337","WalletStatus":"online","Type":"currency"},{"CurrencyID":"218","Name":"...
 RawContent        : HTTP/1.1 200 OK
                     Transfer-Encoding: chunked
                     Connection: keep-alive
                     x-frame-options: SAMEORIGIN
                     Cache-Control: no-cache
                     Content-Type: application/json
                     Date: Sat, 16 Jun 2018 16:49:58 GMT
                     Set-Cook...
 Forms             :
 Headers           : {[Transfer-Encoding, chunked], [Connection, keep-alive], [x-frame-options, SAMEORIGIN], [Cache-Control, no-cache]...}
 Images            : {}
 InputFields       : {}
 Links             : {}
 ParsedHtml        :
 RawContentLength  : 59966

+ HTTPステータスやContent-Typeを簡単に取得することができます。
 PS C:\> $response.StatusCode
 200
 PS C:\> $response.Headers.'Content-Type'
 application/json
+ ''ConvertFrom-Json''コマンドレットを使ってみます。
 PS C:\> $json = ConvertFrom-Json $response.Content
 PS C:\> $json
 
 success request               message result
 ------- -------               ------- ------
 1       /api/v1/getcurrencies         {@{CurrencyID=44; Name=Elite; TickerCode=1337; WalletStatus=online; Type=currency}, @{CurrencyID...
 
 
 PS C:\> $json.result | Where-Object { $_.TickerCode -eq "XSH" }
 
 
 CurrencyID   : 532
 Name         : Shield
 TickerCode   : XSH
 WalletStatus : online
 Type         : currency



** Webの応答を読み取っています。(Waiting for response)を非表示にする方法 [#za344b05]
上記のコマンドレットを実行すると、APIサーバとのやり取りのプログレスメッセージが表示されます。~

#ref(02.png)

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

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

 PS C:\> $ProgressPreference
 Continue

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

 PS C:\> $ProgressPreference = "SilentlyContinue"

以上、PowerShellを使って、&htmlinsert(coinexchange.io.html);の getcurrenciesを使って~
通貨情報を取得する操作例でした。

----
&htmlinsert(coinexchange.io.html);へのリンク~
#htmlinsert(coinexchange.io.logo.html)

トップ   編集 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS