Skip to content

Instantly share code, notes, and snippets.

@stevehenderson
Last active November 17, 2016 01:27
Show Gist options
  • Save stevehenderson/9d7f6e6b22039c76d71af9037773f45b to your computer and use it in GitHub Desktop.
Save stevehenderson/9d7f6e6b22039c76d71af9037773f45b to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
//Other ideas
//http://stackoverflow.com/questions/23777076/get-variable-from-www-call-in-unity
public static class RequestWWW
{
private static string AUTH = "neo4j:Passwprd123456";
public delegate void RequestFinishedHandler(string error, string text);
private static string createNeo4jJSON(string statement, string props) {
StringBuilder sb = new StringBuilder ();
sb.Append("{ \"statements\" : [ { \"statement\" : \"");
sb.Append(statement);
sb.Append("\",");
sb.Append("\"parameters\" : {");
sb.Append(props);
sb.Append(" } } ] }");
return sb.ToString();
}
public static IEnumerator Authenticate(string url, RequestFinishedHandler onFinish)
{
string POSTAddUserURL = "http://192.168.125.129:7474/user/neo4j";
WWWForm form = new WWWForm();
form.AddField( "name", "value" );
Dictionary<string,string> headers = form.headers;
byte[] rawData = form.data;
// Add a custom header to the request.
// In this case a basic authentication to access a password protected resource.
headers["Authorization"] = "Basic " + System.Convert.ToBase64String(
System.Text.Encoding.ASCII.GetBytes(AUTH));
//The middle term must be null for GETrequest with headers
WWW www = new WWW(POSTAddUserURL, null, headers);
yield return www;
if (onFinish != null)
onFinish(www.error, www.text);
}
public static IEnumerator createAffordance2(string url, RequestFinishedHandler onFinish)
{
string POSTAddUserURL = "http://192.168.125.129:7474/db/data/transaction/commit";
string jsonStr = "{ \"statements\" : [ { \"statement\" : \"CREATE (n:affordance {props}) RETURN n\", \"parameters\" : { \"props\" : { \"affordance_id\" : \"1378212\", \"name\" : \"Affordance88\", \"unit\" : \"CMT54(1CAV)\", \"type\" : \"1\", \"html\" : \"<b>Hello!</b>\", \"location_x\": 1.2, \"location_y\": 2.2, \"location_z\": 5.6 } } } ]}";
WWWForm form = new WWWForm();
//form.AddField( "name", "value" );
Dictionary<string,string> headers = form.headers;
byte[] rawData = System.Text.Encoding.UTF8.GetBytes(jsonStr);
// Add a custom header to the request.
// In this case a basic authentication to access a password protected resource.
headers["Content-Type"] = "application/json";
headers["Authorization"] = "Basic " + System.Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(AUTH));
WWW www = new WWW(POSTAddUserURL, rawData, headers);
yield return www;
if (onFinish != null)
onFinish(www.error, www.text);
}
public static IEnumerator createAffordance(string url, RequestFinishedHandler onFinish)
{
string POSTAddUserURL = "http://192.168.125.129:7474/db/data/transaction/commit";
StringBuilder sb = new StringBuilder ();
//TO DO: Place this in Affordance code
sb.Append ("\"props\" : {");
sb.Append ("\"affordance_id\" : \"1378212\",");
sb.Append ("\"name\" : \"" + System.Guid.NewGuid ().ToString () +"\",");
sb.Append ("\"label\" : \"CCP102\",");
sb.Append ("\"unit\" : \"CMT54(1CAV)\",");
sb.Append ("\"type\" : \"1\",");
sb.Append ("\"html\" : \"<b>Hello!</b>\",");
sb.Append ("\"location_x\": 1.2,");
sb.Append ("\"location_y\": 2.2,");
sb.Append ("\"location_z\": 3.2");
sb.Append ("}");
string statement = "CREATE (n:affordance {props}) RETURN n";
string jsonStr = createNeo4jJSON (statement, sb.ToString ());
WWWForm form = new WWWForm();
//form.AddField( "name", "value" );
Dictionary<string,string> headers = form.headers;
byte[] rawData = System.Text.Encoding.UTF8.GetBytes(jsonStr);
// Add a custom header to the request.
// In this case a basic authentication to access a password protected resource.
headers["Content-Type"] = "application/json";
headers["Authorization"] = "Basic " + System.Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(AUTH));
WWW www = new WWW(POSTAddUserURL, rawData, headers);
yield return www;
if (onFinish != null)
onFinish(www.error, www.text);
}
}
public class RESTGo : MonoBehaviour {
string jsonStr = "seom json";
public static string text;
private void RequestFinished(string error, string text)
{
Debug.Log ("Done!");
Debug.Log(error);
Debug.Log(text);
}
public void post()
{
//StartCoroutine(RequestWWW.Authenticate("http://yahoo.com/", RequestFinished));
StartCoroutine(RequestWWW.createAffordance("http://yahoo.com/", RequestFinished));
}
// Use this for initialization
void Start () {
post();
}
// Update is called once per frame
void Update () {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment