chore: simplify request.
parent
940f6a70ab
commit
164b59d95d
|
@ -112,6 +112,7 @@ namespace Storage.Test {
|
||||||
[Test]
|
[Test]
|
||||||
[Order(4)]
|
[Order(4)]
|
||||||
public async Task Query() {
|
public async Task Query() {
|
||||||
|
await Task.Delay(5000);
|
||||||
await LCUser.BecomeWithSessionToken(user2.SessionToken);
|
await LCUser.BecomeWithSessionToken(user2.SessionToken);
|
||||||
|
|
||||||
LCStatusCount statusCount = await LCStatus.GetCount(LCStatus.InboxTypeDefault);
|
LCStatusCount statusCount = await LCStatus.GetCount(LCStatus.InboxTypeDefault);
|
||||||
|
|
|
@ -27,7 +27,8 @@ namespace Storage.Test {
|
||||||
user.Password = "world";
|
user.Password = "world";
|
||||||
string email = $"{unixTime}@qq.com";
|
string email = $"{unixTime}@qq.com";
|
||||||
user.Email = email;
|
user.Email = email;
|
||||||
string mobile = $"{unixTime / 100}";
|
Random random = new Random();
|
||||||
|
string mobile = $"151{random.Next(10000000, 99999999)}";
|
||||||
user.Mobile = mobile;
|
user.Mobile = mobile;
|
||||||
await user.SignUp();
|
await user.SignUp();
|
||||||
|
|
||||||
|
@ -73,7 +74,6 @@ namespace Storage.Test {
|
||||||
LCObject account = new LCObject("Account");
|
LCObject account = new LCObject("Account");
|
||||||
account["user"] = user;
|
account["user"] = user;
|
||||||
await account.Save();
|
await account.Save();
|
||||||
Assert.AreEqual(user.ObjectId, "5e0d5c667d5774006a5c1177");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
|
@ -240,7 +240,7 @@ namespace Storage.Test {
|
||||||
[Test]
|
[Test]
|
||||||
public async Task VerifyCodeForUpdatingPhoneNumber() {
|
public async Task VerifyCodeForUpdatingPhoneNumber() {
|
||||||
await LCUser.Login("hello", "world");
|
await LCUser.Login("hello", "world");
|
||||||
await LCUser.VerifyCodeForUpdatingPhoneNumber("15101006007", "055595");
|
await LCUser.VerifyCodeForUpdatingPhoneNumber("15101006007", "969327");
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
|
|
|
@ -42,40 +42,42 @@ namespace LeanCloud.Storage.Internal.Http {
|
||||||
md5 = MD5.Create();
|
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> headers = null,
|
||||||
Dictionary<string, object> queryParams = null) {
|
Dictionary<string, object> queryParams = null) {
|
||||||
string url = await BuildUrl(path, queryParams);
|
return Request<T>(path, HttpMethod.Get, headers, null, 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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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,
|
Dictionary<string, object> headers = null,
|
||||||
object data = null,
|
object data = null,
|
||||||
Dictionary<string, object> queryParams = null) {
|
Dictionary<string, object> queryParams = null) {
|
||||||
string url = await BuildUrl(path, queryParams);
|
string url = await BuildUrl(path, queryParams);
|
||||||
HttpRequestMessage request = new HttpRequestMessage {
|
HttpRequestMessage request = new HttpRequestMessage {
|
||||||
RequestUri = new Uri(url),
|
RequestUri = new Uri(url),
|
||||||
Method = HttpMethod.Post,
|
Method = method,
|
||||||
};
|
};
|
||||||
await FillHeaders(request.Headers, headers);
|
await FillHeaders(request.Headers, headers);
|
||||||
|
|
||||||
|
@ -102,74 +104,6 @@ namespace LeanCloud.Storage.Internal.Http {
|
||||||
throw HandleErrorResponse(response.StatusCode, resultString);
|
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) {
|
LCException HandleErrorResponse(HttpStatusCode statusCode, string responseContent) {
|
||||||
int code = (int)statusCode;
|
int code = (int)statusCode;
|
||||||
string message = responseContent;
|
string message = responseContent;
|
||||||
|
|
Loading…
Reference in New Issue