Last active
July 14, 2019 23:12
-
-
Save DraconInteractive/eeeb92eb769c5d32e550d1f35790ec2e to your computer and use it in GitHub Desktop.
API Controller for simple data storage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.Networking; | |
using SimpleJSON; | |
public class APIController : MonoBehaviour | |
{ | |
public static string mainURL = @"https://testurl.com"; | |
public static string authURL = @"https://testurl.com"; | |
//Allow scripts to access this as long as there is an instance of it in the scene. | |
public static APIController Instance; | |
private void Awake() | |
{ | |
Instance = this; | |
} | |
//General utilitarian debugger. | |
public void PrintData (string url = @"https://jsonstorage.net/api/items/d1f03102-2aa9-496e-a4a0-da13600a3a92") | |
{ | |
StartCoroutine(PrintData_Routine(url)); | |
} | |
public IEnumerator PrintData_Routine (string url) | |
{ | |
yield return StartCoroutine(GetData_Routine(url, callback => { print(callback); })); | |
yield break; | |
} | |
//Used to retrieve data from server. Callback so that the return value can be used easily. | |
public IEnumerator GetData_Routine (string url, Action<string> callback) | |
{ | |
//UWebRequest because it works well with CORS | |
using (UnityWebRequest webRequest = UnityWebRequest.Get(url)) | |
{ | |
// Request and wait for the desired page. | |
yield return webRequest.SendWebRequest(); | |
if (webRequest.isNetworkError) | |
{ | |
Debug.Log("Error: " + webRequest.error); | |
} | |
else | |
{ | |
Debug.Log("Received: " + webRequest.downloadHandler.text); | |
} | |
//Do the thing with the result | |
callback(webRequest.downloadHandler.text); | |
} | |
yield break; | |
} | |
//Overwrite data on server with this data | |
public void PostData (string data, string url = @"https://jsonstorage.net/api/items/d1f03102-2aa9-496e-a4a0-da13600a3a92") | |
{ | |
StartCoroutine(PostData_Routine(url, data)); | |
} | |
//Same as above, but allows for larger amounts of data to be posted in a single call with less processing. | |
public void PostData(Dictionary<string, string> data, string url = @"https://jsonstorage.net/api/items/d1f03102-2aa9-496e-a4a0-da13600a3a92") | |
{ | |
//This constructs the data to be posted. Untested so far. | |
string d = "{"; | |
foreach (KeyValuePair<string, string> pair in data) | |
{ | |
d += "\n" + "\"" + pair.Key + "\" : \"" + pair.Value + "\"" + ","; | |
} | |
d = d.Remove(d.Length - 1); | |
d += "}"; | |
StartCoroutine(PostData_Routine(url, d)); | |
} | |
//Poster. Content type has to be JSON. | |
public IEnumerator PostData_Routine (string url, string data) | |
{ | |
using (UnityWebRequest www = UnityWebRequest.Put(url, data)) | |
{ | |
www.SetRequestHeader("Content-Type", "application/json"); | |
yield return www.SendWebRequest(); | |
if (www.isNetworkError || www.isHttpError) | |
{ | |
Debug.Log(www.error); | |
} | |
else | |
{ | |
Debug.Log(www.downloadHandler.text); | |
} | |
} | |
yield break; | |
} | |
//Add data onto existing. Will insert itself into the last bracket. | |
public void AddData_Basic (string key, string value, string url = @"https://jsonstorage.net/api/items/d1f03102-2aa9-496e-a4a0-da13600a3a92") | |
{ | |
string d = ",\n" + "\"" + key + "\" : \"" + value + "\""; | |
StartCoroutine(AddData_Routine(url, d)); | |
} | |
//Allows for more complex adding of data. | |
public void AddData_Dictionary(Dictionary<string, string> data, string url = @"https://jsonstorage.net/api/items/d1f03102-2aa9-496e-a4a0-da13600a3a92") | |
{ | |
string d = ""; | |
foreach (KeyValuePair<string, string> pair in data) | |
{ | |
d += ",\n" + "\"" + pair.Key + "\" : \"" + pair.Value + "\""; | |
} | |
StartCoroutine(AddData_Routine(url, d)); | |
} | |
//Allows for block add data. | |
public void AddData (string data, string url = @"https://jsonstorage.net/api/items/d1f03102-2aa9-496e-a4a0-da13600a3a92") | |
{ | |
StartCoroutine(AddData_Routine(url, data)); | |
} | |
//Get current data, remove the end bracket, add the new data, add the end bracket then post it. | |
public IEnumerator AddData_Routine (string url, string data) | |
{ | |
string d = ""; | |
yield return StartCoroutine(GetData_Routine(url, (callback => { d = callback; }))); | |
d = d.Remove(d.Length - 1); | |
d += data; | |
d += "}"; | |
print("D \n" + d); | |
yield return StartCoroutine(PostData_Routine(url, d)); | |
print("put done"); | |
yield break; | |
} | |
//Find and replace value, then update on server. | |
public void UpdateData (string key, string value, string url = @"https://jsonstorage.net/api/items/d1f03102-2aa9-496e-a4a0-da13600a3a92") | |
{ | |
StartCoroutine(UpdateData_Routine(url, key, value)); | |
} | |
public IEnumerator UpdateData_Routine (string url, string key, string value) | |
{ | |
string d = ""; | |
yield return StartCoroutine(GetData_Routine(url, callback => { d = callback; })); | |
var n = JSON.Parse(d); | |
n[key] = value; | |
string nn = n.ToString(); | |
yield return StartCoroutine(PostData_Routine(url, nn)); | |
yield break; | |
} | |
//Auth Functions | |
//Run this once to set up an authentication database. Note, this will format any existing data. | |
public void InitialiseDatabaseAsType_Auth() | |
{ | |
//string data = "{\"players\": [{\"user1\":{\"password\":\"pass1\"},\"user2\":{\"password\":\"pass2\"}}]}"; | |
string data = "{}"; | |
var N = JSON.Parse(data); | |
N["players"]["user1"]["password"] = "pass1"; | |
N["players"]["user2"]["password"] = "pass2"; | |
data = N.ToString(); | |
PostData(data, authURL); | |
} | |
public void AddPlayerAuth (string playerName, string playerPassword) | |
{ | |
StartCoroutine(AddPlayerAuth_Routine(playerName, playerPassword)); | |
} | |
public IEnumerator AddPlayerAuth_Routine (string playerName, string playerPassword) | |
{ | |
string data = ""; | |
yield return StartCoroutine(GetData_Routine(authURL, callback => data = callback)); | |
var N = JSON.Parse(data); | |
N["players"][playerName]["password"] = playerPassword; | |
string r = N.ToString(); | |
print("R: \n" + r); | |
yield return StartCoroutine(PostData_Routine(authURL, r)); | |
yield break; | |
} | |
public void FindPlayerAuth (string playerName) | |
{ | |
StartCoroutine(FindPlayerAuth_Routine(playerName)); | |
} | |
public IEnumerator FindPlayerAuth_Routine(string playerName) | |
{ | |
string data = ""; | |
yield return StartCoroutine(GetData_Routine(authURL, callback => data = callback)); | |
var N = JSON.Parse(data); | |
if (N["players"].HasKey(playerName)) { | |
string p = N["players"][playerName]["password"].Value; | |
print("P: \n" + p); | |
} else | |
{ | |
print("Player Not Found"); | |
} | |
yield break; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment