From 0c89ed15799411c14a58eaf6dc4b1cd38291e50f Mon Sep 17 00:00:00 2001 From: Silkage Date: Tue, 10 Jan 2023 11:11:53 +0800 Subject: [PATCH] feat: support after hooks --- Runtime/RestClient.cs | 12 ++++++++---- Runtime/RestClientOptions.cs | 14 ++++++++++++++ package.json | 2 +- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/Runtime/RestClient.cs b/Runtime/RestClient.cs index f3365bd..c94bce5 100644 --- a/Runtime/RestClient.cs +++ b/Runtime/RestClient.cs @@ -33,13 +33,17 @@ public RestClient(string baseUrl) : this(new Uri(baseUrl)) public IEnumerator Do(RestRequest request, Action completedAction) { + Options.BeforeHooks?.ForEach(hook => hook(request)); using var webRequest = BuildUnityWebRequest(request); yield return webRequest.SendWebRequest(); - completedAction?.Invoke(new RestResponse(webRequest, request)); + var response = new RestResponse(webRequest, request); + Options.AfterHooks?.ForEach(hook => hook(response)); + completedAction?.Invoke(response); } public async Task Do(RestRequest request) { + Options.BeforeHooks?.ForEach(hook => hook(request)); var webRequest = BuildUnityWebRequest(request); try { @@ -49,13 +53,13 @@ public async Task Do(RestRequest request) { Debug.LogError($"do request exception: {e.Message}"); } - - return new RestResponse(webRequest, request); + var response = new RestResponse(webRequest, request); + Options.AfterHooks?.ForEach(hook => hook(response)); + return response; } private UnityWebRequest BuildUnityWebRequest(RestRequest request) { - Options.BeforeHooks?.ForEach(hook => hook(request)); var url = request.BuildUrl(Options.BaseUrl); var webRequest = new UnityWebRequest(url, request.Method.String()); webRequest.downloadHandler = request.DownloadHandler; diff --git a/Runtime/RestClientOptions.cs b/Runtime/RestClientOptions.cs index 9955235..a249c77 100644 --- a/Runtime/RestClientOptions.cs +++ b/Runtime/RestClientOptions.cs @@ -39,6 +39,11 @@ public RestClientOptions(string baseUrl) : this(new Uri(baseUrl)) /// public List> BeforeHooks { get; set; } = new(); + /// + /// 请求执行后的Hooks + /// + public List> AfterHooks { get; set; } = new(); + /// /// 添加执行前Hook /// @@ -47,5 +52,14 @@ public void AddBeforeHook(Action hook) { BeforeHooks.Add(hook); } + + /// + /// 添加执行后Hook + /// + /// + public void AddAfterHook(Action hook) + { + AfterHooks.Add(hook); + } } } \ No newline at end of file diff --git a/package.json b/package.json index 944e200..918037d 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "com.cysharp.unitask": "2.3.3" }, "displayName": "RestClient", - "version": "1.0.1", + "version": "1.0.2", "unity": "2021.3", "description": "Send HTTP requests in unity through Restful", "keywords": [