Last active
December 25, 2016 17:03
-
-
Save ado3s/afe7996d8062b4db65644b5d24b75cbd to your computer and use it in GitHub Desktop.
【Unity】BestHTTPをUniRxのObservableWWW風にしてみた
This file contains 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 BestHTTP; | |
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using UniRx; | |
using UnityEngine; | |
namespace Sample | |
{ | |
public class HTTPResult | |
{ | |
public string Data { get; set; } | |
public string Message { get; set; } | |
public AppCode.ResultCode ResultCode { get; set; } | |
public static HTTPResult Instance() | |
{ | |
return new HTTPResult | |
{ | |
Data = null, | |
Message = string.Empty, | |
ResultCode = AppCode.ResultCode.Processing | |
}; | |
} | |
} | |
public class HTTPClient | |
{ | |
/// <summary> | |
/// Get | |
/// </summary> | |
/// <param name="uri"></param> | |
/// <returns></returns> | |
public static IObservable<HTTPResult> Get(Uri uri) | |
{ | |
return Observable.FromCoroutine<HTTPResult>(obs => Request(obs, uri, HTTPMethods.Get, null)); | |
} | |
/// <summary> | |
/// Post | |
/// </summary> | |
/// <param name="uri"></param> | |
/// <param name="values"></param> | |
/// <returns></returns> | |
public static IObservable<HTTPResult> Post(Uri uri, Dictionary<string, string> values) | |
{ | |
return Observable.FromCoroutine<HTTPResult>(obs => Request(obs, uri, HTTPMethods.Post, values)); ; | |
} | |
/// <summary> | |
/// Request | |
/// </summary> | |
/// <param name="observer"></param> | |
/// <param name="uri"></param> | |
/// <param name="methods"></param> | |
/// <param name="values"></param> | |
/// <returns></returns> | |
private static IEnumerator Request( | |
IObserver<HTTPResult> observer | |
, Uri uri | |
, HTTPMethods methods | |
, Dictionary<string, string> values) | |
{ | |
var request = new HTTPRequest(uri, methods); | |
if (values != null) | |
{ | |
foreach (var val in values) | |
request.AddField(val.Key, val.Value); | |
} | |
request.Timeout = TimeSpan.FromSeconds(10); | |
request.ConnectTimeout = TimeSpan.FromSeconds(10); | |
request.Send(); | |
yield return request; | |
var result = HTTPResult.Instance(); | |
switch(request.State) | |
{ | |
case HTTPRequestStates.Finished: | |
var response = request.Response; | |
if (response.StatusCode >= 400) | |
{ | |
result.Message = AppCode.CommonErrorMessage; | |
result.ResultCode = AppCode.ResultCode.ExecuteAPIError; | |
break; | |
} | |
result.Data = response.DataAsText; | |
result.ResultCode = AppCode.ResultCode.Normal; | |
break; | |
case HTTPRequestStates.Error: | |
case HTTPRequestStates.Aborted: | |
result.Message = AppCode.CommonErrorMessage; | |
result.ResultCode = AppCode.ResultCode.ExecuteAPIError; | |
break; | |
case HTTPRequestStates.TimedOut: | |
case HTTPRequestStates.ConnectionTimedOut: | |
result.Message = AppCode.TimedOutMessage; | |
result.ResultCode = AppCode.ResultCode.Timedout; | |
break; | |
default: | |
result.Message = AppCode.CommonErrorMessage; | |
result.ResultCode = AppCode.ResultCode.UnKnownError; | |
break; | |
}; | |
observer.OnNext(result); | |
observer.OnCompleted(); | |
} | |
} | |
// 呼び出し側 | |
public class Sample : MonoBehaviour | |
{ | |
void Start() | |
{ | |
// Get | |
HTTPClient.Get(new Uri("ザ・URL")).Subscribe(HandlingData); | |
// Post | |
var values = new Dictionary<string, string> | |
{ | |
{ "Foo", "フー!" }, { "Bar", "バー!" }, { "Baz", "バズ!" } | |
}; | |
HTTPClient.Post(new Uri("ザ・URL"), values).Subscribe(); | |
// ↑必要ならSubscribeに処理をいれてね。めんどいので割愛 | |
} | |
private void HandlingData(HttpResult res) | |
{ | |
if (res.ResultCode != AppCode.ResultCode.Normal) | |
{ | |
// エラー時の処理 | |
Debug.Log("Errorやで!! : " + res.ResultCode + "/" + res.Message); | |
return; | |
} | |
// 正常時の処理 | |
Debug.Log("とれたで!! : " + res.Data); | |
// ・・・ ごにょごにょしてあげてー ・・・ | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment