我的问题:我该怎么做?
所以,直到本周,我还没有触及任何东西。大约6年。有很多我已经忘记了甚至更多我从未知道的事情,虽然我喜欢async / await关键字的想法,但我在为客户实现以下要求时遇到了一些问题&# 39; s API实现:
ServerAPI
类为每个API方法都有一个方法,采用适当的输入参数(例如方法Login
接受id
和password
, API调用并将结果返回给调用者。)Login
方法返回一个带有您的身份验证令牌的User
对象,uid,等)true
=成功)或状态代码。HttpResponseMessage
对象并让调用者处理它。这与我到目前为止大致相同,我不确定如何使其符合上述要求,我是否正确行事。任何指导都受到赞赏(但是,燃烧,不是)。
// 200 (+User JSON) = success, otherwise APIError JSON
internal async Task<User> Login (string id, string password)
{
LoginPayload payload = new LoginPayload() { LoginId = id, Password = password};
var request = NewRequest(HttpMethod.Post, "login");
JsonPayload<LoginPayload>(payload, ref request);
return await Execute<Account>(request, false);
}
// 204: success, anything else failure
internal async Task<Boolean> LogOut ()
{
return await Execute<Boolean>(NewRequest(HttpMethod.Delete, "login"), true);
}
internal async Task<HttpResponseMessage> GetRawResponse ()
{
return await Execute<HttpResponseMessage>(NewRequest(HttpMethod.Get, "raw/something"), true);
}
internal async Task<Int32> GetMeStatusCode ()
{
return await Execute<Int32>(NewRequest(HttpMethod.Get, "some/intstatus"), true);
}
private async Task<RESULT> Execute<RESULT>(HttpRequestMessage request, bool authenticate)
{
if (authenticate)
AuthenticateRequest(ref request); // add auth token to request
var tcs = new TaskCompletionSource<RESULT>();
var response = await client.SendAsync(request);
// TODO: If the RESULT is just HTTPResponseMessage, the rest is unnecessary
if (response.IsSuccessStatusCode)
{
try
{
// TryParse needs to handle Boolean differently than other types
RESULT result = await TryParse<RESULT>(response);
tcs.SetResult(result);
}
catch (Exception e)
{
tcs.SetException(e);
}
}
else
{
try
{
APIError error = await TryParse<APIError>(response);
tcs.SetException(new APIException(error));
}
catch (Exception e)
{
tcs.SetException(new APIException("Unknown error"));
}
}
return tcs.Task.Result;
}
这是APIError
JSON结构(它是状态代码+自定义错误代码)。
{
"status": 404,
"code":216,
"msg":"User not found"
}
我更愿意留在System.Net
,但这主要是因为我不想将所有代码都切换过来。如果我想要的更容易以其他方式完成,那么它显然值得额外的工作。
感谢。
答案 0 :(得分:5)
以下是我如何使用MVC API 2作为后端的示例。如果凭据正确,我的后端会返回json结果。 UserCredentials
类与json结果完全相同。你必须使用System.Net.Http.Formatting
女巫可以在Microsoft.AspNet.WebApi.Client
NugetPackage中找到
public static async Task<UserCredentials> Login(string username, string password)
{
string baseAddress = "127.0.0.1/";
HttpClient client = new HttpClient();
var authorizationHeader = Convert.ToBase64String(Encoding.UTF8.GetBytes("xyz:secretKey"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", authorizationHeader);
var form = new Dictionary<string, string>
{
{ "grant_type", "password" },
{ "username", username },
{ "password", password },
};
var Response = await client.PostAsync(baseAddress + "oauth/token", new FormUrlEncodedContent(form));
if (Response.StatusCode == HttpStatusCode.OK)
{
return await Response.Content.ReadAsAsync<UserCredentials>(new[] { new JsonMediaTypeFormatter() });
}
else
{
return null;
}
}
您还需要Newtonsoft.Json
个包裹。
public class UserCredentials
{
[JsonProperty("access_token")]
public string AccessToken { get; set; }
[JsonProperty("token_type")]
public string TokenType { get; set; }
[JsonProperty("expires_in")]
public int ExpiresIn { get; set; }
//more properties...
}
答案 1 :(得分:0)
所以,首先要解决你需要Newtonsoft.Json
的评论,我真的感觉不到需要。我发现内置的支持到目前为止工作得很好(在我原来的问题中使用APIError
Json:
[DataContract]
internal class APIError
{
[DataMember (Name = "status")]
public int StatusCode { get; set; }
[DataMember (Name = "code")]
public int ErrorCode { get; set; }
}
我还定义了一个JsonHelper
类来(de)序列化:
public class JsonHelper
{
public static T fromJson<T> (string json)
{
var bytes = Encoding.Unicode.GetBytes (json);
using (MemoryStream mst = new MemoryStream(bytes))
{
var serializer = new DataContractJsonSerializer (typeof (T));
return (T)serializer.ReadObject (mst);
}
}
public static string toJson (object instance)
{
using (MemoryStream mst = new MemoryStream())
{
var serializer = new DataContractJsonSerializer (instance.GetType());
serializer.WriteObject (mst, instance);
mst.Position = 0;
using (StreamReader r = new StreamReader(mst))
{
return r.ReadToEnd();
}
}
}
}
以上我已经开始工作了。至于一个基于预期结果类型处理每个请求执行的单个方法,它可以更容易更改我处理事物的方式(如错误等),这也增加了复杂性和因此我的代码的可读性。我最终创建了单独的方法(原始问题中Execute
方法的所有变体:
// execute and return response.StatusCode
private static async Task<HttpStatusCode> ExecuteForStatusCode (HttpRequestMessage request, bool authenticate = true)
// execute and return response without processing
private static async Task<HttpResponseMessage> ExecuteForRawResponse(HttpRequestMessage request, bool authenticate = true)
// execute and return response.IsSuccessStatusCode
private static async Task<Boolean> ExecuteForBoolean (HttpRequestMessage request, bool authenticate = true)
// execute and extract JSON payload from response content and convert to RESULT
private static async Task<RESULT> Execute<RESULT>(HttpRequestMessage request, bool authenticate = true)
我可以将未经授权的响应(我当前的代码现在无法处理)移动到新方法CheckResponse
中,如果收到401,将会(例如)将用户注销。