Example #1
0
        public async Task <HttpResponseMessage> SendRequest(HttpRequest request)
        {
            string requestContent;

            using (var receiveStream = request.Body)
            {
                using (var readStream = new StreamReader(receiveStream, Encoding.UTF8))
                {
                    requestContent = readStream.ReadToEnd();
                }
            }

            using (var newRequest = new HttpRequestMessage(new HttpMethod(request.Method), CreateDestinationUrl(request)))
            {
                newRequest.Content = new StringContent(requestContent, Encoding.UTF8, request.ContentType);
                try
                {
                    var client   = _clientFactory.CreateClient();
                    var response = await client.SendAsync(newRequest);

                    return(response);
                }
                catch (Exception ex)
                {
                    // PARK-1781 To Do: Log Error Message
                    return(ErrorHandler.ConstructErrorMessage(HttpStatusCode.ServiceUnavailable, Messages.NoResponse(Url)));
                }
            }
        }
Example #2
0
        public async Task <HttpResponseMessage> RouteRequest(HttpRequest request)
        {
            var requestPath = request.Path.ToString().TrimEnd('/');

            if (string.IsNullOrEmpty(requestPath))
            {
                return(ErrorHandler.ConstructErrorMessage(HttpStatusCode.Forbidden, Messages.ContentsNotListed));
            }

            var route = GetRouteByFullRequestPath(requestPath);

            if (route == null)
            {
                route = GetRouteByServiceNameInRequestPath(requestPath);
                if (route == null)
                {
                    return(ErrorHandler.ConstructErrorMessage(HttpStatusCode.NotFound, Messages.RequestPathNotFound(request.Path)));
                }
            }

            var destination = route.Destination;

            destination.Url = destination.Url ?? CoreApiUrl;

            if (destination.RequiresAuthentication)
            {
                if (!AuthenticationService.Authenticate(request))
                {
                    return(ErrorHandler.ConstructErrorMessage(HttpStatusCode.Unauthorized, Messages.Unauthorized));
                }
                if (destination.RequiresMemberId)
                {
                    destination.MemberId = AuthenticationService.GetMemberId().ToString();
                    if (string.IsNullOrEmpty(destination.MemberId))
                    {
                        return(ErrorHandler.ConstructErrorMessage(HttpStatusCode.Unauthorized, Messages.Unauthorized));
                    }
                }
            }

            return(await destination.SendRequest(request));
        }