コード例 #1
0
        private async Task<HttpCall> SendAsync(RestRequest request, bool retryInvalidToken)
        {
            var url = _instanceUrl + request.Path;
            var headers = request.AdditionalHeaders != null
                ? new HttpCallHeaders(_accessToken, request.AdditionalHeaders)
                : new HttpCallHeaders(_accessToken, new Dictionary<string, string>());

            var call =
                await
                    new HttpCall(request.Method, headers, url, request.RequestBody, request.ContentType).ExecuteAsync()
                        .ConfigureAwait(false);

            if (call.StatusCode != HttpStatusCode.Unauthorized && call.StatusCode != HttpStatusCode.Forbidden)
            {
                return call;
            }

            if (!retryInvalidToken || _accessTokenProvider == null) return call;

            var newAccessToken = await _accessTokenProvider();
            if (newAccessToken == null)
            {
                return call;
            }
                
            _accessToken = newAccessToken;
            call = await SendAsync(request, false);

            // Done
            return call;
        }
コード例 #2
0
 public async void SendAsync(RestRequest request, AsyncRequestCallback callback)
 {
     RestResponse result = await SendAsync(request).ConfigureAwait(false);
     if (callback != null)
     {
         callback(result);
     }
 }
コード例 #3
0
 public void TestAdditionalHeaders()
 {
     Dictionary<string, string> headers = new Dictionary<string, string>() {{"X-Foo", "RestRequestName"}};
     RestRequest request = new RestRequest(RestMethod.GET, "/my/foo/", null, ContentType.NONE, headers);
     Assert.AreEqual(RestMethod.GET, request.Method, "Wrong method");
     Assert.AreEqual(ContentType.NONE, request.ContentType, "Wrong content type");
     Assert.AreEqual("/my/foo/", request.Path, "Wrong path");
     Assert.IsNull(request.Body, "Wrong body");
     Assert.AreEqual(headers, request.AdditionalHeaders, "Wrong headers");
 }
コード例 #4
0
        private HttpCall SendSync(RestRequest request, bool retryInvalidToken)
        {
            string url = _instanceUrl + request.Path;
            Dictionary<string, string> headers = new Dictionary<string, string>() {};
            if (_accessToken != null) 
            {
                headers["Authorization"] = "Bearer " + _accessToken;
            }
            if (request.AdditionalHeaders != null)
            {
                headers.Concat(request.AdditionalHeaders);
            }

            HttpCall call = new HttpCall(request.Method, headers, url, request.Body, request.ContentType).Execute().Result;

            if (!call.HasResponse)
            {
                throw call.Error;
            }

            if (call.StatusCode == HttpStatusCode.Unauthorized)
            {
                if (retryInvalidToken && _accessTokenProvider != null)
                {
                    string newAccessToken = _accessTokenProvider().Result;
                    if (newAccessToken != null)
                    {
                        _accessToken = newAccessToken;
                        call = SendSync(request, false);
                    }
                }
            }

            // Done
            return call;
        }
コード例 #5
0
 public static async Task<IdentityResponse> CallIdentityServiceAsync(string idUrl, IRestClient client)
 {
     var request = new RestRequest(HttpMethod.Get, new Uri(idUrl).AbsolutePath);
     var response = await client.SendAsync(request);
     if (response.Success)
     {
         LoggingService.Log("success", LoggingLevel.Verbose);
         return JsonConvert.DeserializeObject<IdentityResponse>(response.AsString);
     }
     else
     {
         LoggingService.Log("Error occured:", LoggingLevel.Critical);
         LoggingService.Log(response.Error, LoggingLevel.Critical);
     }
     throw response.Error;
 }
コード例 #6
0
 public async Task<IRestResponse> SendAsync(RestRequest request)
 {
     var result = await SendAsync(request, true);
     return new RestResponse(result);
 }
コード例 #7
0
 public async void SendAsync(RestRequest request, AsyncRequestCallback callback)
 {
     var result = await SendAsync(request).ConfigureAwait(false);
     callback?.Invoke(result);
 }
コード例 #8
0
        public override async Task<JArray> ContinueFetch(SyncManager syncManager)
        {
            if (String.IsNullOrWhiteSpace(NextRecordsUrl))
            {
                return null;
            }

            var request = new RestRequest(HttpMethod.Get, NextRecordsUrl);
            var response = await syncManager.SendRestRequest(request);
            JObject responseJson = response.AsJObject;
            var records = responseJson.ExtractValue<JArray>(Constants.Records);
            return records;
        }
コード例 #9
0
 public RestResponse SendSync(RestRequest request)
 {
     return new RestResponse(SendSync(request, true));
 }
コード例 #10
0
 public async Task<RestResponse> SendAsync(RestRequest request)
 {
     return await Task.Factory.StartNew(() => SendSync(request));
 }
コード例 #11
0
 public void SendAsync(RestRequest request, AsyncRequestCallback callback)
 {
     SendAsync(request).ContinueWith((t) => callback(t.Result));
 }
コード例 #12
0
 public void TestAdditionalHeaders()
 {
     var headers = new Dictionary<string, string> {{"X-Foo", "RestRequestName"}};
     var request = new RestRequest(HttpMethod.Get, "/my/foo/", null, ContentTypeValues.None, headers);
     Assert.AreEqual(HttpMethod.Get, request.Method, "Wrong method");
     Assert.AreEqual(ContentTypeValues.None, request.ContentType, "Wrong content type");
     Assert.AreEqual("/my/foo/", request.Path, "Wrong path");
     Assert.IsNull(request.RequestBody, "Wrong body");
     Assert.AreEqual(headers, request.AdditionalHeaders, "Wrong headers");
 }
コード例 #13
0
 public static async Task<IdentityResponse> CallIdentityService(string idUrl, RestClient client)
 {
     var request = new RestRequest(HttpMethod.Get, new Uri(idUrl).AbsolutePath);
     RestResponse response = await client.SendAsync(request);
     if (response.Success)
     {
         PlatformAdapter.SendToCustomLogger("OAuth2.CallIdentityService - success", LoggingLevel.Verbose);
         return JsonConvert.DeserializeObject<IdentityResponse>(response.AsString);
     }
     else
     {
         PlatformAdapter.SendToCustomLogger("OAuth2.CallIdentityService - Error occured:", LoggingLevel.Critical);
         PlatformAdapter.SendToCustomLogger(response.Error, LoggingLevel.Critical);
     }
     throw response.Error;
 }
コード例 #14
0
        private async Task<HttpCall> Send(RestRequest request, bool retryInvalidToken)
        {
            string url = _instanceUrl + request.Path;
            var headers = request.AdditionalHeaders != null ? new HttpCallHeaders(_accessToken, request.AdditionalHeaders) : new HttpCallHeaders(_accessToken, new Dictionary<string, string>());

            HttpCall call =
                await
                    new HttpCall(request.Method, headers, url, request.RequestBody, request.ContentType).Execute()
                        .ConfigureAwait(false);

            if (!call.HasResponse)
            {
                throw call.Error;
            }

            if (call.StatusCode == HttpStatusCode.Unauthorized || call.StatusCode == HttpStatusCode.Forbidden)
            {
                if (retryInvalidToken && _accessTokenProvider != null)
                {
                    string newAccessToken = await _accessTokenProvider();
                    if (newAccessToken != null)
                    {
                        _accessToken = newAccessToken;
                        call = await Send(request, false);
                    }
                }
            }

            // Done
            return call;
        }
コード例 #15
0
 public async Task<RestResponse> SendAsync(RestRequest request)
 {
     HttpCall result = await Send(request, true);
     return new RestResponse(result);
 }