chore: simplify request.

oneRain 2021-01-14 15:14:31 +08:00
parent 940f6a70ab
commit 164b59d95d
3 changed files with 30 additions and 95 deletions

View File

@ -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);

View File

@ -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]

View File

@ -42,40 +42,42 @@ namespace LeanCloud.Storage.Internal.Http {
md5 = MD5.Create();
}
public async Task<T> Get<T>(string path,
public Task<T> Get<T>(string path,
Dictionary<string, object> headers = null,
Dictionary<string, object> 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<T>(resultString,
LCJsonConverter.Default);
return ret;
}
throw HandleErrorResponse(response.StatusCode, resultString);
return Request<T>(path, HttpMethod.Get, headers, null, queryParams);
}
public async Task<T> Post<T>(string path,
public Task<T> Post<T>(string path,
Dictionary<string, object> headers = null,
object data = null,
Dictionary<string, object> queryParams = null) {
return Request<T>(path, HttpMethod.Post, headers, data, queryParams);
}
public Task<T> Put<T>(string path,
Dictionary<string, object> headers = null,
object data = null,
Dictionary<string, object> queryParams = null) {
return Request<T>(path, HttpMethod.Put, headers, data, queryParams);
}
public Task Delete(string path,
Dictionary<string, object> headers = null,
object data = null,
Dictionary<string, object> queryParams = null) {
return Request<Dictionary<string, object>>(path, HttpMethod.Delete, headers, data, queryParams);
}
async Task<T> Request<T>(string path,
HttpMethod method,
Dictionary<string, object> headers = null,
object data = null,
Dictionary<string, object> 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<T> Put<T>(string path,
Dictionary<string, object> headers = null,
object data = null,
Dictionary<string, object> 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<T>(resultString,
LCJsonConverter.Default);
return ret;
}
throw HandleErrorResponse(response.StatusCode, resultString);
}
public async Task Delete(string path,
Dictionary<string, object> headers = null,
object data = null,
Dictionary<string, object> 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<Dictionary<string, object>>(resultString,
LCJsonConverter.Default);
return;
}
throw HandleErrorResponse(response.StatusCode, resultString);
}
LCException HandleErrorResponse(HttpStatusCode statusCode, string responseContent) {
int code = (int)statusCode;
string message = responseContent;