Example #1
0
 public ApiBatchResponse CallApiMethod(ApiBatchRequest request, bool encode)
 {
     var response = _batchHandler.ProcessBatchRequest(_context, request);
     if (encode && response!=null && response.Data!=null)
         response.Data = Convert.ToBase64String(Encoding.UTF8.GetBytes(response.Data));
     return response;
 }
Example #2
0
 public ApiBatchResponse CallApiMethod(ApiBatchRequest request)
 {
     return CallApiMethod(request, true);
 }
Example #3
0
 public string GetApiResponse(ApiBatchRequest request)
 {
     var response = CallApiMethod(request);
     return response!=null ? response.Data : null;
 }
        internal ApiBatchResponse ProcessBatchRequest(HttpContextBase context, ApiBatchRequest apiBatchRequest)
        {
            if (context.Request == null) throw new InvalidOperationException("Request is empty");
            if (context.Request.Url == null) throw new InvalidOperationException("Url is empty");

            using (var writer = new StringWriter())
            {
                var path = apiBatchRequest.RelativeUrl;
                if (context.Request.ApplicationPath != null && path.StartsWith(context.Request.ApplicationPath))
                {
                    path = path.Substring(context.Request.ApplicationPath.Length);
                }
                var uri = new Uri(context.Request.Url,"/"+path.TrimStart('/'));
                
                var workerRequest = new ApiWorkerRequest(Uri.UnescapeDataString(uri.AbsolutePath.TrimStart('/')),uri.Query.TrimStart('?'), writer, context, new ContentType(apiBatchRequest.BodyContentType));
                workerRequest.HttpVerb = apiBatchRequest.Method;

                if (!string.IsNullOrEmpty(apiBatchRequest.Body))
                {
                    var contentType = new ContentType(apiBatchRequest.BodyContentType);
                    var encoding = Encoding.GetEncoding(contentType.CharSet);
                    workerRequest.EntityBody = encoding.GetBytes(apiBatchRequest.Body);
                }
                var workContext = new HttpContext(workerRequest);

                var newContext = new HttpContextWrapper(workContext);
                
                //Make a faked request
                var routeData = RouteTable.Routes.GetRouteData(newContext);
                if (routeData != null)
                {
                    //Construct new context
                    Container.Resolve<IApiHttpHandler>(new DependencyOverride(typeof(RouteData), routeData)).Process(newContext);
                    newContext.Response.Flush();
                    
                    //Form response
                    var response = new ApiBatchResponse(apiBatchRequest)
                                       {
                                           Data = writer.GetStringBuilder().ToString(),
                                           Headers = new ItemDictionary<string, string>()
                                       };
                    foreach (var responseHeaderKey in workerRequest.ResponseHeaders.AllKeys)
                    {
                        response.Headers.Add(responseHeaderKey, workerRequest.ResponseHeaders[responseHeaderKey]);
                    }
                    response.Status = workerRequest.HttpStatus;
                    response.Name = apiBatchRequest.Name;
                    return response;
                }
                
            }
            return null;
        }
 public ApiBatchResponse(ApiBatchRequest apiBatchRequest)
 {
     Name = apiBatchRequest.Name;
 }
 public ApiBatchResponse(ApiBatchRequest apiBatchRequest)
 {
     Name = apiBatchRequest.Name;
 }
Example #7
0
        internal async Task <ApiBatchResponse> ProcessBatchRequest(HttpContextBase context, ApiBatchRequest apiBatchRequest)
        {
            if (context.Request == null)
            {
                throw new InvalidOperationException("Request is empty");
            }
            if (context.Request.Url == null)
            {
                throw new InvalidOperationException("Url is empty");
            }

            using (var writer = new StringWriter())
            {
                var path = apiBatchRequest.RelativeUrl;
                if (context.Request.ApplicationPath != null && path.StartsWith(context.Request.ApplicationPath))
                {
                    path = path.Substring(context.Request.ApplicationPath.Length);
                }
                var uri = new Uri(context.Request.Url, "/" + path.TrimStart('/'));

                var workerRequest = new ApiWorkerRequest(Uri.UnescapeDataString(uri.AbsolutePath.TrimStart('/')), uri.Query.TrimStart('?'), writer, context, new ContentType(apiBatchRequest.BodyContentType));
                workerRequest.HttpVerb = apiBatchRequest.Method;

                if (!string.IsNullOrEmpty(apiBatchRequest.Body))
                {
                    var contentType = new ContentType(apiBatchRequest.BodyContentType);
                    var encoding    = Encoding.GetEncoding(contentType.CharSet);
                    workerRequest.EntityBody = encoding.GetBytes(apiBatchRequest.Body);
                }
                var workContext = new HttpContext(workerRequest)
                {
                    Handler = this
                };
                var newContext = new HttpContextWrapper(workContext);

                //Make a faked request
                var routeData = RouteTable.Routes.GetRouteData(newContext);
                if (routeData != null)
                {
                    //Construct new context
                    await Container.BeginLifetimeScope().Resolve <ApiHttpAsyncHandler>(new TypedParameter(typeof(RouteData), routeData)).ProcessRequestAsync(workContext);

                    newContext.Response.Flush();

                    //Form response
                    var response = new ApiBatchResponse(apiBatchRequest)
                    {
                        Data    = writer.GetStringBuilder().ToString(),
                        Headers = new ItemDictionary <string, string>()
                    };
                    foreach (var responseHeaderKey in workerRequest.ResponseHeaders.AllKeys)
                    {
                        response.Headers.Add(responseHeaderKey, workerRequest.ResponseHeaders[responseHeaderKey]);
                    }
                    response.Status = workerRequest.HttpStatus;
                    response.Name   = apiBatchRequest.Name;
                    return(response);
                }
            }
            return(null);
        }