private void InitRequest(UnityWebRequest webRequest, HttpParams args) { if (args.timeout > 0) { webRequest.timeout = args.timeout; } }
IEnumerator OnDeleteCoroutine(int id, HttpParams args) { var newUrl = args.url; var request = UnityWebRequest.Delete(newUrl); InitRequest(request, args); yield return(request.SendWebRequest()); OnRequestCallback(id, request, args); }
IEnumerator OnPutCoroutine(int id, HttpParams args) { var newUrl = args.url; var request = UnityWebRequest.Put(newUrl, args.bodyData); InitRequest(request, args); yield return(request.SendWebRequest()); OnRequestCallback(id, request, args); }
IEnumerator OnGetCoroutine(int id, HttpParams args) { var newUrl = (args.formData != null) ? args.formData.ToGetData(args.url): args.url; var request = UnityWebRequest.Get(newUrl); InitRequest(request, args); yield return(request.SendWebRequest()); OnRequestCallback(id, request, args); }
IEnumerator OnPostCoroutine(int id, HttpParams args) { var wwwForm = (args.formData != null) ? args.formData.ToPostData(args.url) : null; var request = UnityWebRequest.Post(args.url, wwwForm); InitRequest(request, args); yield return(request.SendWebRequest()); OnRequestCallback(id, request, args); }
void OnRequestCallback(int id, UnityWebRequest webRequest, HttpParams args) { var result = new HttpResult(webRequest); if (webRequest.isHttpError || webRequest.isNetworkError || !webRequest.isDone) { args.onFailed?.Invoke(webRequest.error); } else { args.onSuccess?.Invoke(webRequest.downloadHandler.data); } args.onCallback?.Invoke(result); m_coroutines.Remove(id); }
private int StartRequest(RequestMode requestMode, HttpParams args) { if (args == null) { return(-1); } if (string.IsNullOrEmpty(args.url)) { return(-1); } var newId = m_coroutineId++; Coroutine coroutine = null; switch (requestMode) { case RequestMode.Post: coroutine = StartCoroutine(OnPostCoroutine(newId, args)); break; case RequestMode.Get: coroutine = StartCoroutine(OnGetCoroutine(newId, args)); break; case RequestMode.Put: coroutine = StartCoroutine(OnPutCoroutine(newId, args)); break; case RequestMode.Delete: coroutine = StartCoroutine(OnDeleteCoroutine(newId, args)); break; } m_coroutines[newId] = coroutine; return(newId); }
public int Delete(HttpParams args) { return(StartRequest(RequestMode.Delete, args)); }
public int Put(HttpParams args) { return(StartRequest(RequestMode.Put, args)); }
public int Get(HttpParams args) { return(StartRequest(RequestMode.Get, args)); }