here is a working example of using the glot.io API in C# for Unity 2018.2 which may be helpful to someone. I'm sure there is a more efficient way to do some of this and suggestions are welcome but this works as our first iteration :-)
Especially the part that extracts the content should probably be a regexp or something. Ideally there would be like a raw.glot.io/snippets to get only the content but I couldn't find a way to do this.
public static void SaveToPastebin()
{
BlockManager.Instance.StartCoroutine(SaveToPastebinHelper());
}
public static void LoadFromPastebin(string key)
{
BlockManager.Instance.StartCoroutine(LoadFromPastebinHelper(key));
}
private static IEnumerator SaveToPastebinHelper()
{
string json = BlocksToCondensedJson(); // converts our save data to json
json = json.Replace("\"", "\\\""); //escape quotes
string data = "{\"language\": \"json\", \"title\": \"test\", \"public\": true, \"files\": [{\"name\": \"mystage.json\", \"content\": \""+json+"\"}]}";
using (UnityWebRequest www = new UnityWebRequest("https://snippets.glot.io/snippets", "POST"))
{
www.uploadHandler = new UploadHandlerRaw(Encoding.UTF8.GetBytes(data));
www.downloadHandler = new DownloadHandlerBuffer();
www.SetRequestHeader("Authorization", "Token paste-your-token-key-here");
www.SetRequestHeader("Content-Type", "application/json");
www.chunkedTransfer = false;
yield return www.SendWebRequest();
if (www.isNetworkError)
{
Debug.Log("Network Error:" + www.error);
Debug.Log(www.downloadHandler.text);
}
else if (www.isHttpError)
{
Debug.Log("HTTP Error:" + www.error);
Debug.Log(www.downloadHandler.text);
}
else
{
// Show results as text
Debug.Log(www.downloadHandler.text);
// retrieve results as binary data
byte[] results = www.downloadHandler.data;
}
}
}
private static IEnumerator LoadFromPastebinHelper(string key)
{
using (UnityWebRequest www = UnityWebRequest.Get("https://snippets.glot.io/snippets/"+key))
{
www.downloadHandler = new DownloadHandlerBuffer();
www.SetRequestHeader("Content-Type", "application/json");
www.chunkedTransfer = false;
yield return www.SendWebRequest();
if (www.isNetworkError)
{
Debug.Log("Network Error:" + www.error);
Debug.Log(www.downloadHandler.text);
}
else if (www.isHttpError)
{
Debug.Log("HTTP Error:" + www.error);
Debug.Log(www.downloadHandler.text);
}
else
{
// Show results as text
Debug.Log(www.downloadHandler.text);
// retrieve results as binary data
byte[] results = www.downloadHandler.data;
string stripped = Encoding.UTF8.GetString(results);
stripped = stripped.Substring(stripped.IndexOf("\"content\":")+11);
stripped = stripped.Substring(0, stripped.Length-4);
stripped = stripped.Replace("\\\"", "\"");
BlocksFromJsonStream(Encoding.UTF8.GetBytes(stripped)); //restores data from the glot.io paste
}
}
}