作ったもの

RESTClient

usingSystem;usingSystem.Collections.Generic;usingSystem.Threading.Tasks;usingUnityEngine;usingUnityEngine.Networking;/**
* Useful utility to use REST access to server
*/publicclassRESTClient{/**
* Host name of server
* This must be endpoint URI of the server
*/publicreadonlystringhostName;/**
* Header that is included in every request send from this class
*/publicreadonlyDictionary<string,string>headers=newDictionary<string,string>();/**
* Constructor
*/publicRESTClient(stringhostName){this.hostName=hostName;}/**
* Perform GET request to the path
*/publicTask<T>Get<T>(stringpath){UnityWebRequestreq=UnityWebRequest.Get(this.getURL(path));returnProcessRequest<T>(req);}/**
* Perform PUT request to the path
* You can specify content also
*/publicTask<T>Put<T>(stringpath,stringcontent=""){UnityWebRequestreq=UnityWebRequest.Put(this.getURL(path),content);returnProcessRequest<T>(req);}/**
* Perform DELETE request to the path
*/publicTask<T>Delete<T>(stringpath){UnityWebRequestreq=UnityWebRequest.Delete(this.getURL(path));returnProcessRequest<T>(req);}/**
* Perform HEAD request to the path
*/publicTask<T>Head<T>(stringpath){UnityWebRequestreq=UnityWebRequest.Head(this.getURL(path));returnProcessRequest<T>(req);}/**
* Perform POST request to the path
* You can specify parameter as dictioanry
*/publicTask<T>Post<T>(stringpath,Dictionary<string,string>param=null){if(param==null){param=newDictionary<string,string>();}UnityWebRequestreq=UnityWebRequest.Post(this.getURL(path),param);returnProcessRequest<T>(req);}privateasyncTask<T>ProcessRequest<T>(UnityWebRequestreq){SetHeaders(req);StringreqResult=awaitExecuteRequestToGetTask(req);AssertRequestErrors(req);returnJsonUtility.FromJson<T>(reqResult);}privatevoidSetHeaders(UnityWebRequestreq){foreach(KeyValuePair<string,string>pairinthis.headers){req.SetRequestHeader(pair.Key,pair.Value);}}privatevoidAssertRequestErrors(UnityWebRequestreq){if(req.isHttpError||req.isNetworkError){thrownewException(req.error);}}privateTask<String>ExecuteRequestToGetTask(UnityWebRequestreq){varasyncReq=req.SendWebRequest();TaskCompletionSource<String>tcs=newTaskCompletionSource<String>();asyncReq.completed+=(e)=>{tcs.SetResult(req.downloadHandler.text);};returntcs.Task;}privatestringgetURL(stringpath){return$"{this.hostName}{path}";}}