getcurrency APIは、getcurrencies APIにより返却されたCurrencyID, TickerCodeを指定することにより
指定したID(CurrencyID), コード(TickerCode)の通貨情報を返却します。
以下、PowerShellによる、TickerCode, CurrencyIDを指定して、指定した通貨情報を取得する操作例を記します。
CoinExchange.io
へのリンク
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を利用します。
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を指定して通貨情報を取得します。
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
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の通貨情報が取得できました。
上記では、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/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
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/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
上記のコマンドレットを実行すると、APIサーバとのやり取りのプログレスメッセージが表示されます。
これを非表示にしたい場合は、以下のようにしてください。
非表示にするには、$ProgressPreferenceにSilentlyContinueを設定します。
変更前は以下のように Continue が設定されています。
PS C:\> $ProgressPreference Continue
非表示にするには、以下のように変更します。
PS C:\> $ProgressPreference = "SilentlyContinue"
以上、PowerShellを使って、CoinExchange.io
の getcurrency APIを使って
通貨情報を取得する操作例でした。
CoinExchange.io
へのリンク