1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

unity / plotly > REST API入門 > サンプルは動いた

Last updated at Posted at 2015-09-19
動作確認
Unity 5.1.3-f1 on MacOS X 10.8.5

入門

https://plot.ly/
というグラフ描画サービスをUnityからアクセスしたい。

でも、RESTなんたらはよく分からない。
とりあえず、入門してみた。

UnityでRESTを使った例は以下で見つかった。
http://enpel.hateblo.jp/entry/2012/10/15/200048

実際のAPIについては
https://plot.ly/rest/
の 「POST/clientresp」をクリックすると表示される例を元にコーディングした。

code

using UnityEngine;
using System.Collections;

public class restTestCS : MonoBehaviour {


	IEnumerator getRestResponse()
	{
		string url = "https://plot.ly/clientresp";
		string dataStr = 
			"un=7of9&" +
			"key=bkzm2ze0iz&" +
			"origin=plot&" +
			"platform=lisp&" +
			"args=[[0, 1, 2], [3, 4, 5], [1, 2, 3], [6, 6, 5]]&" +
			"kwargs={\"filename\": \"plot from api\"," +
			"\"fileopt\": \"overwrite\"," + 
			"\"style\": {" + 
			"\"type\": \"bar\"" +
			"}," + 
			"\"traces\": [1]," + 
			"\"layout\": {" +
			"\"title\": \"experimental data\"" +
			"}," + 
			"\"world_readable\": true" +
			"}";

		byte [] data = System.Text.Encoding.UTF8.GetBytes (dataStr);

		WWW www = new WWW (url, data);
		yield return www;

		int nop = 1; // for breakpoint
	}

	void Start () {
		StartCoroutine ("getRestResponse");
	}
	
	void Update () {
	
	}
}

int nop = 1の行でブレーク設定して上記を実行して、イミディエイトにてwww.textを見ると以下の文字列が取得できた。

> www.text
"{\"url\": \"https://plot.ly/~7of9/127\", \"message\": \"\", \"warning\": \"\", \"filename\": \"plot from api\", \"error\": \"\"}"

とりあえず成功したようだ。

補足

なお、上記のコードを現時点で実行すると、以下のエラーが出るようになっている。理由としては、API Keyをbkzm2ze0izから変更済みだから。

> www.text
"{\"url\": \"\", \"message\": \"\", \"warning\": \"\", \"filename\": \"\", \"error\": \"Aw, snap! You tried to use our API as the user '7of9', but the supplied API key doesn't match our records. You can view your API key at plot.ly/settings.\\n\\nYou're most likely getting this message because your local credentials file isn't synced with the Plotly server you're communicating with.\\n\\nGo to plot.ly/<language>/getting-started (e.g., plot.ly/python/getting-started) for more information.\\n\\nMake sure that you're logged in as 7of9.\\n\\nNeed help? Feedback? Write support@plot.ly.\"}"

API Keyはplot.lyでログインして Setting -> API settingsの画面で表示される。

Generate a new keyでAPI Keyを変更可能。

コードの以下の部分は、それぞれのユーザのidとAPI Keyを設定して使う。

"un=7of9&" +
"key=bkzm2ze0iz&" +

やっていること

上記のコードでやっているのは、データのx,y座標を指定して、グラフを作成しているようだ。
既存データの読込みをやろうとしていたら、データの追加(とグラフの作成)ができていた。

実際に追加されたデータとグラフは以下で見ることが出来る。
https://plot.ly/128/~7of9/
https://plot.ly/127/~7of9/

www.textで戻ってくるurl (https://plot.ly/~7of9/127)を開くとグラフが見えるということらしい。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?