/// <summary> /// Attempts to prepare for retrying request sending upon catching the /// specified exception. /// </summary> /// <param name="context">Request context to be used for token generation.</param> /// <param name="exception">The exception caused failure of the previous /// request sending.</param> /// <returns>True if and only if the request sending could be repeated.</returns> private static bool _PrepareRetry( IRestRequestContext context, Exception exception) { var restException = exception as RestException; if (restException != null) { if (restException.ErrorCode == AgsConst.ARCGIS_EXPIRED_TOKEN) { context.Connection.GenerateToken(); return(true); } return(false); } if (_IsTransientError(exception)) { Thread.Sleep(RETRY_WAIT_TIME); return(true); } return(false); }
protected override void Get(RestRequest request, IRestRequestContext context) { if (log.IsDebugEnabled) { log.DebugFormat("MonitorRequestHandler, get '{0}'", request.Uri); } var queryParams = HttpUtility.ParseQueryString(request.Uri.Query); var appId = queryParams["appid"]; if (log.IsDebugEnabled) { log.DebugFormat("MonitorRequestHandler, appId '{0}'", appId); } if (string.IsNullOrEmpty(appId)) { context.SendResponse("AppId missing"); } //TODO change this method to a check and only and call GetMonitoringResult separately? //TODO how to decide if the servernames have to be updated? // if (this.MonitoringCache.GetMonitoringResultFromCache(appId, GetMonitoringResultCallback, context)) // { // return; // } photonCloudApp.AuthenticationCache.GetAccount(appId, fiber, account => this.OnGetApplicationAccount(account, request, context, queryParams)); }
public void IsValidResponse(IRestRequestContext <object> request, IRestResponseContext <IRestClientResponse <object> > response) { Assert.AreNotEqual(response.Data, null); Assert.AreEqual(response.Data.IsSuccessFul, true); Assert.AreEqual(response.Data.Error, null); Assert.IsTrue(response.Data.Result == null); }
public void IsValidResponse(IRestRequestContext <EditClientRequest> request, IRestResponseContext <IRestClientResponse <EditClientResponse> > response) { Assert.AreNotEqual(response.Data, null); Assert.AreEqual(response.Data.IsSuccessFul, true); Assert.AreEqual(response.Data.Error, null); Assert.IsTrue(response.Data.Result.Result.Id != Guid.Empty); Assert.IsTrue(response.Data.Result.Result.Name != string.Empty); }
private T _SendRequest <T>( IRestRequestContext context, string url, string query, HttpRequestOptions opt) { String queryWithToken = ""; // Add authentication token to the request. if (context.Connection.RequiresTokens) { var requestUri = new Uri(url); var tokenCookie = new Cookie( TOKEN_COOKIE_NAME, context.Connection.LastToken, TOKEN_COOKIE_PATH, requestUri.Host); // Adding a cookie will replace existing one (if any) with the same name, path // and domain using case-insensitive comparison. opt.CookieContainer.Add(requestUri, tokenCookie); // 10.1 routing/VRP services need the "token=..." in the query string queryWithToken = AgsHelper.FormatTokenQuery(query, context.Connection.LastToken); } else { queryWithToken = query; } // Add session cookie to the request so all requests in the session would be processed // by the same server. if (context.SessionCookie != null) { opt.CookieContainer.Add(context.SessionCookie); } var responseInfo = default(HttpResponseInfo); string json = WebHelper.SendRequest(url, queryWithToken, opt, out responseInfo); json = _resultPreprocessor(json); // Get session cookie from the response. When this cookie is sent in the request // we might get no cookie in the response, so we update session cookie only when // it's not null. var sessionCookie = responseInfo.Cookies[ELB_SESSION_COOKIE_NAME]; if (sessionCookie != null) { context.SessionCookie = sessionCookie; } return(JsonSerializeHelper.DeserializeResponse <T>(json, context.KnownTypes)); }
public TestApi(ILogger logger) { _logger = logger; RestClient = new Stellar.IntegrationTests.RestClient.RestClient { OnTrace = logger.Trace }; Request = new RestRequestContext <TRequest>(logger); Response = new RestResponseContext <IRestClientResponse <TResponse> >(); }
public void IsValidResponse(IRestRequestContext <GetInvoiceRequest> request, IRestResponseContext <IRestClientResponse <GetInvoiceResponse> > response) { Assert.AreNotEqual(response.Data, null); Assert.AreEqual(response.Data.IsSuccessFul, true); Assert.AreEqual(response.Data.Error, null); Assert.IsTrue(response.Data.Result.Result.Id != Guid.Empty); Assert.IsTrue(response.Data.Result.Result.ClientId != Guid.Empty); Assert.IsTrue(response.Data.Result.Result.OrderId != Guid.Empty); Assert.IsTrue(response.Data.Result.Result.Description != string.Empty); Assert.IsTrue(response.Data.Result.Result.Amount > 0); }
/// <summary> /// Translates the specified exception into the more application-specific /// one. /// </summary> /// <param name="context">Context of the failed request.</param> /// <param name="exception">The exception to be translated.</param> /// <returns>Translated exception or null reference if the exception /// cannot be translated.</returns> private static Exception _TranslateException( IRestRequestContext context, Exception exception) { if (ServiceHelper.IsCommunicationError(exception) || exception is SerializationException) { var serverName = context.Connection.Title; return(ServiceHelper.CreateCommException(serverName, exception)); } return(null); }
private void HandleGetMonitoringResult(MonitoringResult monitoringResult, IRestRequestContext context) { if (monitoringResult.MonitoringServiceResult == MonitoringServiceResult.Ok) { if (log.IsDebugEnabled) { log.DebugFormat("HandleGetMonitoringResult, response for app {0}: {1}", monitoringResult.ApplicationId, monitoringResult.Status); } context.SendResponse(monitoringResult.Status); } else { if (log.IsDebugEnabled) { log.DebugFormat("HandleGetMonitoringResult, MonitoringServiceResult {0}: {1}", monitoringResult.MonitoringServiceResult, monitoringResult.DebugMessage); } context.SendResponse("An error occured"); } }
/// <summary> /// Sends REST request to the specified url. /// </summary> /// <typeparam name="T">The type of the request result.</typeparam> /// <param name="context">The reference to the rest request context object.</param> /// <param name="url">The url to send request to.</param> /// <param name="query">The query to be sent.</param> /// <param name="opt">The reference to the request sending options.</param> /// <returns>Result of the specified REST request.</returns> /// <exception cref="T:ESRI.ArcLogistics.Routing.RestException">error was /// returned by the REST API.</exception> /// <exception cref="T:ESRI.ArcLogistics.CommunicationException">failed /// to communicate with the REST service at the specified url.</exception> /// <exception cref="T:System.ArgumentNullException">Any of /// <paramref name="context"/>, <paramref name="url"/>, /// <paramref name="query"/> or <paramref name="opt"/> arguments is a null /// reference.</exception> public T SendRequest <T>( IRestRequestContext context, string url, string query, HttpRequestOptions opt) { Debug.Assert(context != null); Debug.Assert(!string.IsNullOrEmpty(url)); Debug.Assert(!string.IsNullOrEmpty(query)); Debug.Assert(opt != null); var sendRequestWrapper = new RetriableInvocationWrapper( MAX_RETRY_COUNT, (e) => _PrepareRetry(context, e), (e) => _TranslateException(context, e)); var stopwatch = new Stopwatch(); stopwatch.Start(); return(sendRequestWrapper.Invoke(() => { var timeout = opt.Timeout - stopwatch.ElapsedMilliseconds; timeout = Math.Max(timeout, 0); opt.Timeout = (int)timeout; var response = _SendRequest <T>( context, url, query, opt); RestHelper.ValidateResponse(response); return response; })); }
public void OnGetApplicationAccount(ApplicationAccount applicationAccount, RestRequest request, IRestRequestContext context, NameValueCollection queryParams) { if (log.IsDebugEnabled) { log.DebugFormat("MonitorRequestHandler, OnGetApplicationAccount '{0}': {1}/{2}", applicationAccount.ApplicationId, applicationAccount.PrivateCloud, applicationAccount.ServiceType); } if (string.IsNullOrEmpty(applicationAccount.ApplicationId)) { if (log.IsDebugEnabled) { log.DebugFormat("MonitorRequestHandler, OnGetApplicationAccount error: '{0}'", applicationAccount.DebugMessage); } context.SendResponse("Could not retrieve account information"); return; } if (applicationAccount.ServiceType != ServiceType.Realtime && applicationAccount.ServiceType != ServiceType.Pun) { context.SendResponse("Only Realtime applications are supported. Application has ServiceType " + applicationAccount.ServiceType); return; } var servers = this.photonCloudApp.CloudCache.TryGetPhotonEndpoints(applicationAccount.PrivateCloud, applicationAccount.ServiceType); if (servers == null || servers.Count == 0) { if (log.IsDebugEnabled) { log.DebugFormat("No servers found for appId '{0}' cloud '{1}' ServiceType '{2}'", applicationAccount.ApplicationId, applicationAccount.PrivateCloud, applicationAccount.ServiceType); } context.SendResponse("Found no servers for App"); return; } var servernamesList = new List <string>(); //TODO ? add cluster filter param? foreach (var photonEndpointInfo in servers) { var servernameAndRegion = GetServernameAndRegion(photonEndpointInfo); if (string.IsNullOrEmpty(servernameAndRegion)) { continue; } servernamesList.Add(servernameAndRegion); } var servernames = string.Join(";", servernamesList.ToArray()); if (log.IsDebugEnabled) { log.DebugFormat("Call GetMonitoringResult for application {0}/{1}: {2}", applicationAccount.ApplicationId, applicationAccount.PrivateCloud, servernames); } this.MonitoringCache.GetMonitoringResult(applicationAccount.ApplicationId, servernames, GetMonitoringResultCallback, context); }