Skip to content

Commit 9ca45d7

Browse files
refacor HttpPost for other methods
1 parent 3b7895d commit 9ca45d7

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

Code/MethodSystem/Methods/HTTPMethods/HTTPPostMethod.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
using Newtonsoft.Json.Linq;
44
using SER.Code.ArgumentSystem.Arguments;
55
using SER.Code.ArgumentSystem.BaseArguments;
6+
using SER.Code.Helpers;
67
using SER.Code.Helpers.Exceptions;
8+
using SER.Code.MethodSystem.BaseMethods;
79
using SER.Code.MethodSystem.BaseMethods.Synchronous;
810
using SER.Code.MethodSystem.MethodDescriptors;
911
using UnityEngine.Networking;
@@ -21,23 +23,25 @@ public class HTTPPostMethod : SynchronousMethod, ICanError
2123
new TextArgument("address"),
2224
new ReferenceArgument<JObject>("json data to post")
2325
];
24-
25-
public string[] ErrorReasons =>
26+
27+
public static string[] HttpErrorReasons { get; } =
2628
[
2729
nameof(UnityWebRequest.Result.ConnectionError),
2830
nameof(UnityWebRequest.Result.DataProcessingError),
2931
nameof(UnityWebRequest.Result.ProtocolError)
3032
];
31-
33+
34+
public string[] ErrorReasons => HttpErrorReasons;
35+
3236
public override void Execute()
3337
{
3438
var address = Args.GetText("address");
3539
var jsonData = Args.GetReference<JObject>("json data to post");
3640

37-
Timing.RunCoroutine(SendPost(address, jsonData.ToString()));
41+
Timing.RunCoroutine(SendPost(this, address, jsonData.ToString()));
3842
}
3943

40-
private IEnumerator<float> SendPost(string url, string jsonData)
44+
public static IEnumerator<float> SendPost(Method caller, string url, string jsonData)
4145
{
4246
using UnityWebRequest request = new UnityWebRequest(url, "POST");
4347
byte[] bodyRaw = System.Text.Encoding.UTF8.GetBytes(jsonData);
@@ -47,10 +51,11 @@ private IEnumerator<float> SendPost(string url, string jsonData)
4751

4852
yield return Timing.WaitUntilDone(request.SendWebRequest());
4953

54+
Log.Signal($"{request.error} error");
5055
if (request.result != UnityWebRequest.Result.Success)
5156
{
5257
throw new ScriptRuntimeError(
53-
this,
58+
caller,
5459
$"Address {url} has returned {request.result} ({request.responseCode}): {request.error}"
5560
);
5661
}

0 commit comments

Comments
 (0)