#author("2018-06-02T00:30:54+09:00","","") #navi(../) * PowerShellでCoinExchange.ioのgetmarkets APIにアクセスしてみる [#a9aa60e8] &htmlinsert(coinexchange.io.html);の''getmarkets'' APIをPowerShelld呼び出す方法の記事になります。 * 関連資料・記事 [#e12477ca] -[[CoinExchange.io API v1 Reference>http://coinexchangeio.github.io/slate/]] -[[マーケット一覧情報取得・getmarkets(PowerShell)>API/CoinExchange.io/マーケット一覧情報取得・getmarkets(PowerShell)]] -[[マーケットサマリ情報取得・getmarketsummaries(PowerShell)>API/CoinExchange.io/マーケットサマリ情報取得・getmarketsummaries(PowerShell)]] ---- &htmlinsert(coinexchange.io.html);へのリンク #htmlinsert(coinexchange.io.logo.html) ---- * getmarketsの仕様 [#m0f90f16] 以下、[[CoinExchange.io API v1 Reference>http://coinexchangeio.github.io/slate/]]のgetmarkets APIの抜粋です。 Get Markets This endpoint retrieves all markets. HTTP Request GET https://www.coinexchange.io/api/v1/getmarkets ‘getmarkets’ returns JSON structured like this: { "success": "1", "request": "/api/v1/public/getmarkets", "message": "", "result": [ { "MarketID": "1", "MarketAssetName": "Megacoin", "MarketAssetCode": "MEC", "MarketAssetID": "3", "MarketAssetType": "currency", "BaseCurrency": "Bitcoin", "BaseCurrencyCode": "BTC", "BaseCurrencyID": "1", "Active": true }, { "MarketID": "3", "MarketAssetName": "Litecoin", "MarketAssetCode": "LTC", "MarketAssetID": "2", "MarketAssetType": "currency", "BaseCurrency": "Bitcoin", "BaseCurrencyCode": "BTC", "BaseCurrencyID": "1", "Active": true } ] } * getmarkets APIをブラウザでアクセスしてみる [#of505e25] 以下のURLにブラウザでアクセスしてみます。~ - https://www.coinexchange.io/api/v1/getmarkets FirefoxだとJSON形式で確認することができます。~ |''JSON''|''生データ''| |&ref(01.png);|&ref(02.png);| * PowerShellのInvoke-RestMethodを使用してAPIを呼び出してみる [#vae68601] 実際に、&htmlinsert(coinexchange.io.html);の''getmarkets''をInvoke-RestMethodコマンドレットを使用して呼び出してみます。 +CoinExchange.ioのhttpsがTLS1.2のみなっているので、以下のコマンドでTLS1.2(のみ)を指定します。 PS C:\> [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 +呼び出し元のURL情報を各変数に設定しています。 PS C:\> $base_url = "https://www.coinexchange.io/api/" PS C:\> $version = "v1" PS C:\> $method = "getmarkets" +''getmarkets'' APIのURLをInvoke-RestMethodの引数にして実行します。 PS C:\> $response = Invoke-RestMethod -UseBasicParsing "${base_url}${version}/$method" +''getmarkets''から返却された値を表示しています。 PS C:\> $response success request message result ------- ------- ------- ------ 1 /api/v1/getmarkets {@{MarketID=18; MarketAssetName=Litecoin; MarketAssetCode=LTC; MarketAssetID=2; M... ** 取得した値から各種値を抽出する [#a199c755] 上記で取得し、$responseに格納した値を操作してみます。 +取得したレスポンスは以下のようになっています。 PS C:\> $response success request message result ------- ------- ------- ------ 1 /api/v1/getmarkets {@{MarketID=18; MarketAssetName=Litecoin; MarketAssetCode=LTC; MarketAssetID=2; M... + success, request, messageを取得し表示しています。 PS C:\> $response.success 1 PS C:\> $response.request /api/v1/getmarkets PS C:\> $response.message +返却されたマッケート情報の数を表示しています。~ PS C:\> $response.result.count 823 +0番目と823番目を表示しています。 PS C:\> $response.result[0] MarketID : 18 MarketAssetName : Litecoin MarketAssetCode : LTC MarketAssetID : 2 MarketAssetType : currency BaseCurrency : Bitcoin BaseCurrencyCode : BTC BaseCurrencyID : 1 Active : True #br PS C:\> $response.result[822] MarketID : 849 MarketAssetName : Sophos MarketAssetCode : SOPH MarketAssetID : 623 MarketAssetType : ethereum_asset BaseCurrency : Ethereum BaseCurrencyCode : ETH BaseCurrencyID : 70 Active : False + MarketAssetNameのみ抽出し、ソート、ユニーク、先頭から10銘柄を表示しています。 PS C:\> $response.result | % {$_.MarketAssetName} | Sort-Object | Get-Unique | Select-Object -First 10 imbrex Accelerator AcesCoin ACoin Adcoin Adshares Adzcoin AgriNovusCoin AkuyaCoin ALIS * HTTPステータスなどを一緒に取得したい場合はInvoke-WebRequestが便利! [#e524c011] 上記では、Invoke-RestMethodにてJSONをPowerShellで扱いやすい状態に変換してくれますが、~ Webサーバから返却された値を変換せずに取得したい場合は、Invoke-WebRequestコマンドレットを使用します。~ また、取得後にJSONをPowerShellで扱いやすいオブジェクトに変換するには、''ConvertFrom-Json''コマンドレットを使用します。~ 以下に実行例を記します。 +CoinExchange.ioのhttpsがTLS1.2のみなっているので、以下のコマンドでTLS1.2(のみ)を指定します。 PS C:\> [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 + APIのURL作成用の文字列を変数に格納しています。 PS C:\> $base_url = "https://www.coinexchange.io/api/" PS C:\> $version = "v1" PS C:\> $method = "getmarkets" + Invoke-WebRequestコマンドレットでAPIを呼び出し返却された情報を変数に格納しています。 PS C:\> $response = Invoke-WebRequest -UseBasicParsing "${base_url}${version}/$method" +返却された情報を表示してみます。 PS C:\> $response StatusCode : 200 StatusDescription : OK Content : {"success":"1","request":"\/api\/v1\/getmarkets","message":"","result":[{"MarketID":"18","MarketAss etName":"Litecoin","MarketAssetCode":"LTC","MarketAssetID":"2","MarketAssetType":"currency","BaseCu rr... 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: Wed, 30 May 2018 14:08:28 GMT Set-Cook... Forms : Headers : {[Transfer-Encoding, chunked], [Connection, keep-alive], [x-frame-options, SAMEORIGIN], [Cache-Cont rol, no-cache]...} Images : {} InputFields : {} Links : {} ParsedHtml : RawContentLength : 174298 + HTTPステータスやContent-Typeを簡単に取得することができます。 PS C:\> $response.StatusCode 200 PS C:\> $response.Headers.'Content-Type' application/json +取得したJSONをPowerShellで扱うためのオブジェクトに変換する''''を実行してみます。 PS C:\> $json = $response.Content | ConvertFrom-Json PS C:\> $json success request message result ------- ------- ------- ------ 1 /api/v1/getmarkets {@{MarketID=18; MarketAssetName=Litecoin; MarketAssetCode=LTC; MarketAssetID=2; M... 上記のInvoke-RestMethodと同じ状態となります。 ** Webの応答を読み取っています。(Waiting for response)を非表示にする方法 [#ve7dc280] 上記のコマンドレットを実行すると、APIサーバとのやり取りのプログレスメッセージが表示されます。~ #ref(03.png) これを非表示にしたい場合は、以下のようにしてください。 非表示にするには、$ProgressPreferenceにSilentlyContinueを設定します。~ 変更前は以下のように Continue が設定されています。 PS C:\> $ProgressPreference Continue 非表示にするには、以下のように変更します。 PS C:\> $ProgressPreference = "SilentlyContinue" 以上、PowerShellを使って、&htmlinsert(coinexchang.io.html);の getmarkets APIを呼び出す方法でした。 ---- &htmlinsert(coinexchange.io.html); #htmlinsert(coinexchange.io.logo.html)