How should I implement ExecuteAsync with RestSharp on Windows Phone 7?

前端 未结 6 1436
忘掉有多难
忘掉有多难 2020-12-08 04:50

I\'m attempting to use the documentation on the RestSharp GitHub wiki to implement calls to my REST API service but I\'m having an issue with the ExecuteAsync method in part

6条回答
  •  轮回少年
    2020-12-08 05:02

    Your code should look something like this:

    public class HarooApi
    {
        const string BaseUrl = "https://domain.here";
    
        readonly string _accountSid;
        readonly string _secretKey;
    
        public HarooApi(string accountSid, string secretKey)
        {
            _accountSid = accountSid;
            _secretKey = secretKey;
        }
    
        public void ExecuteAndGetContent(RestRequest request, Action callback)
        {
            var client = new RestClient();
            client.BaseUrl = BaseUrl;
            client.Authenticator = new HttpBasicAuthenticator(_accountSid, _secretKey);
            request.AddParameter("AccountSid", _accountSid, ParameterType.UrlSegment);
            client.ExecuteAsync(request, response =>
            {
                callback(response.Content);
            });
        }
    
        public void ExecuteAndGetMyClass(RestRequest request, Action callback)
        {
            var client = new RestClient();
            client.BaseUrl = BaseUrl;
            client.Authenticator = new HttpBasicAuthenticator(_accountSid, _secretKey);
            request.AddParameter("AccountSid", _accountSid, ParameterType.UrlSegment);
            client.ExecuteAsync(request, (response) =>
            {
                callback(response.Data);
            });
        }
    }
    

    I added two methods, so you can check what you want (string content from the response body, or a deserialized class represented here by MyClass)

提交回复
热议问题