- 追加された行はこの色です。
- 削除された行はこの色です。
#author("2018-06-20T14:42:14+09:00","","")
#navi(../)
* PowerShellでCoinExchange.ioで扱う通貨の情報を取得する方法・getcurrency [#z6a101ab]
getcurrency APIは、getcurrencies APIにより返却されたCurrencyID, TickerCodeを指定することにより~
指定したID(CurrencyID), コード(TickerCode)の通貨情報を返却します。~
以下、PowerShellによる、TickerCode, CurrencyIDを指定して、指定した通貨情報を取得する操作例を記します。
----
&htmlinsert(coinexchange.io.html);へのリンク~
#htmlinsert(coinexchange.io.logo.html)
#contents
* 関連資料・記事 [#qba74c6c]
-[[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)]]
* CurrencyID, TickerCodeは、getcurrencies APIにより一覧を取得することができます。~ [#mfd13d23]
getcurrencies APIについては、以下のリンク先の記事を参照してください。
-[[仮想通貨一覧を取得・getcurrencies>API/CoinExchange.io/仮想通貨一覧を取得・getcurrencies(PowerShell)]]
メジャーな仮想通貨の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の仕様 [#o171eb42]
[[CoinExchange.io API v1 Reference>http://coinexchangeio.github.io/slate/]]の抜粋です。
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を指定して通貨情報を取得 [#t2c22236]
以下、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が便利 [#pc9f7e70]
上記では、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/getcurrency?ticker_code=ETH"
+返却された情報を表示してみます。
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
+ 各種HTTPの情報が簡単に取得できます。
PS C:\> $response.StatusCode
200
PS C:\> $response.Headers.'Content-Type'
application/json
+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)を非表示にする方法 [#za344b05]
上記のコマンドレットを実行すると、APIサーバとのやり取りのプログレスメッセージが表示されます。~
#ref(02.png)
これを非表示にしたい場合は、以下のようにしてください。
非表示にするには、$ProgressPreferenceにSilentlyContinueを設定します。~
変更前は以下のように Continue が設定されています。
PS C:\> $ProgressPreference
Continue
非表示にするには、以下のように変更します。
PS C:\> $ProgressPreference = "SilentlyContinue"
以上、PowerShellを使って、&htmlinsert(coinexchange.io.html);の getcurrency APIを使って~
通貨情報を取得する操作例でした。
----
&htmlinsert(coinexchange.io.html);へのリンク~
#htmlinsert(coinexchange.io.logo.html)