From 164b59d95d0808d79e94ca4ecb212dd781474d86 Mon Sep 17 00:00:00 2001 From: oneRain Date: Thu, 14 Jan 2021 15:14:31 +0800 Subject: [PATCH] chore: simplify request. --- Storage/Storage.Test/StatusTest.cs | 1 + Storage/Storage.Test/UserTest.cs | 6 +- Storage/Storage/Internal/Http/LCHttpClient.cs | 118 ++++-------------- 3 files changed, 30 insertions(+), 95 deletions(-) diff --git a/Storage/Storage.Test/StatusTest.cs b/Storage/Storage.Test/StatusTest.cs index a789d0a..cdec3f7 100644 --- a/Storage/Storage.Test/StatusTest.cs +++ b/Storage/Storage.Test/StatusTest.cs @@ -112,6 +112,7 @@ namespace Storage.Test { [Test] [Order(4)] public async Task Query() { + await Task.Delay(5000); await LCUser.BecomeWithSessionToken(user2.SessionToken); LCStatusCount statusCount = await LCStatus.GetCount(LCStatus.InboxTypeDefault); diff --git a/Storage/Storage.Test/UserTest.cs b/Storage/Storage.Test/UserTest.cs index 06ea54a..7d1ba8b 100644 --- a/Storage/Storage.Test/UserTest.cs +++ b/Storage/Storage.Test/UserTest.cs @@ -27,7 +27,8 @@ namespace Storage.Test { user.Password = "world"; string email = $"{unixTime}@qq.com"; user.Email = email; - string mobile = $"{unixTime / 100}"; + Random random = new Random(); + string mobile = $"151{random.Next(10000000, 99999999)}"; user.Mobile = mobile; await user.SignUp(); @@ -73,7 +74,6 @@ namespace Storage.Test { LCObject account = new LCObject("Account"); account["user"] = user; await account.Save(); - Assert.AreEqual(user.ObjectId, "5e0d5c667d5774006a5c1177"); } [Test] @@ -240,7 +240,7 @@ namespace Storage.Test { [Test] public async Task VerifyCodeForUpdatingPhoneNumber() { await LCUser.Login("hello", "world"); - await LCUser.VerifyCodeForUpdatingPhoneNumber("15101006007", "055595"); + await LCUser.VerifyCodeForUpdatingPhoneNumber("15101006007", "969327"); } [Test] diff --git a/Storage/Storage/Internal/Http/LCHttpClient.cs b/Storage/Storage/Internal/Http/LCHttpClient.cs index b5c1086..04e2562 100644 --- a/Storage/Storage/Internal/Http/LCHttpClient.cs +++ b/Storage/Storage/Internal/Http/LCHttpClient.cs @@ -42,40 +42,42 @@ namespace LeanCloud.Storage.Internal.Http { md5 = MD5.Create(); } - public async Task Get(string path, + public Task Get(string path, Dictionary headers = null, Dictionary queryParams = null) { - string url = await BuildUrl(path, queryParams); - HttpRequestMessage request = new HttpRequestMessage { - RequestUri = new Uri(url), - Method = HttpMethod.Get - }; - await FillHeaders(request.Headers, headers); - - LCHttpUtils.PrintRequest(client, request); - HttpResponseMessage response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead); - request.Dispose(); - - string resultString = await response.Content.ReadAsStringAsync(); - response.Dispose(); - LCHttpUtils.PrintResponse(response, resultString); - - if (response.IsSuccessStatusCode) { - T ret = JsonConvert.DeserializeObject(resultString, - LCJsonConverter.Default); - return ret; - } - throw HandleErrorResponse(response.StatusCode, resultString); + return Request(path, HttpMethod.Get, headers, null, queryParams); } - public async Task Post(string path, + public Task Post(string path, + Dictionary headers = null, + object data = null, + Dictionary queryParams = null) { + return Request(path, HttpMethod.Post, headers, data, queryParams); + } + + public Task Put(string path, + Dictionary headers = null, + object data = null, + Dictionary queryParams = null) { + return Request(path, HttpMethod.Put, headers, data, queryParams); + } + + public Task Delete(string path, + Dictionary headers = null, + object data = null, + Dictionary queryParams = null) { + return Request>(path, HttpMethod.Delete, headers, data, queryParams); + } + + async Task Request(string path, + HttpMethod method, Dictionary headers = null, object data = null, Dictionary queryParams = null) { string url = await BuildUrl(path, queryParams); HttpRequestMessage request = new HttpRequestMessage { RequestUri = new Uri(url), - Method = HttpMethod.Post, + Method = method, }; await FillHeaders(request.Headers, headers); @@ -102,74 +104,6 @@ namespace LeanCloud.Storage.Internal.Http { throw HandleErrorResponse(response.StatusCode, resultString); } - public async Task Put(string path, - Dictionary headers = null, - object data = null, - Dictionary queryParams = null) { - string url = await BuildUrl(path, queryParams); - HttpRequestMessage request = new HttpRequestMessage { - RequestUri = new Uri(url), - Method = HttpMethod.Put, - }; - await FillHeaders(request.Headers, headers); - - string content = null; - if (data != null) { - content = JsonConvert.SerializeObject(data); - StringContent requestContent = new StringContent(content); - requestContent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); - request.Content = requestContent; - } - LCHttpUtils.PrintRequest(client, request, content); - HttpResponseMessage response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead); - request.Dispose(); - - string resultString = await response.Content.ReadAsStringAsync(); - response.Dispose(); - LCHttpUtils.PrintResponse(response, resultString); - - if (response.IsSuccessStatusCode) { - T ret = JsonConvert.DeserializeObject(resultString, - LCJsonConverter.Default); - return ret; - } - throw HandleErrorResponse(response.StatusCode, resultString); - } - - public async Task Delete(string path, - Dictionary headers = null, - object data = null, - Dictionary queryParams = null) { - string url = await BuildUrl(path, queryParams); - HttpRequestMessage request = new HttpRequestMessage { - RequestUri = new Uri(url), - Method = HttpMethod.Delete - }; - await FillHeaders(request.Headers, headers); - - string content = null; - if (data != null) { - content = JsonConvert.SerializeObject(data); - StringContent requestContent = new StringContent(content); - requestContent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); - request.Content = requestContent; - } - LCHttpUtils.PrintRequest(client, request, content); - HttpResponseMessage response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead); - request.Dispose(); - - string resultString = await response.Content.ReadAsStringAsync(); - response.Dispose(); - LCHttpUtils.PrintResponse(response, resultString); - - if (response.IsSuccessStatusCode) { - _ = JsonConvert.DeserializeObject>(resultString, - LCJsonConverter.Default); - return; - } - throw HandleErrorResponse(response.StatusCode, resultString); - } - LCException HandleErrorResponse(HttpStatusCode statusCode, string responseContent) { int code = (int)statusCode; string message = responseContent;