CoinExchange.io のgetcurrencies APIをPowerShellで呼び出す方法の記事になります。
CoinExchange.io
へのリンク
以下、CoinExchange.io API v1 Reference からの抜粋です。
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 をFirefoxでアクセスしてみました。
https://www.coinexchange.io/api/v1/getcurrencies
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
上記では、Invoke-RestMethodにてJSONをPowerShellで扱いやすい状態に変換してくれますが、
Webサーバから返却された値を変換せずに取得したい場合は、Invoke-WebRequestコマンドレットを使用します。
また、取得後にJSONをPowerShellで扱いやすいオブジェクトに変換するには、ConvertFrom-Jsonコマンドレットを使用します。
以下に実行例を記します。
PS C:\> [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
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
PS C:\> $response.StatusCode 200 PS C:\> $response.Headers.'Content-Type' application/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
上記のコマンドレットを実行すると、APIサーバとのやり取りのプログレスメッセージが表示されます。
これを非表示にしたい場合は、以下のようにしてください。
非表示にするには、$ProgressPreferenceにSilentlyContinueを設定します。
変更前は以下のように Continue が設定されています。
PS C:\> $ProgressPreference Continue
非表示にするには、以下のように変更します。
PS C:\> $ProgressPreference = "SilentlyContinue"
以上、PowerShellを使って、CoinExchange.io
の getcurrenciesを使って
通貨情報を取得する操作例でした。
CoinExchange.io
へのリンク