Example #1
0
        /// <summary>
        /// This method turns the entity in to binary content using
        /// the primary transport
        /// </summary>
        /// <param name="entity">The entity to convert.</param>
        /// <returns>The ByteArrayContent to transmit.</returns>
        protected virtual ByteArrayContent GetContent(E entity)
        {
            var data    = mPrimaryTransport.GetData(entity);
            var content = new ByteArrayContent(data);

            content.Headers.ContentType = new MediaTypeWithQualityHeaderValue(mPrimaryTransport.MediaType);
            return(content);
        }
Example #2
0
        /// <summary>
        /// This method processes the outgoing response.
        /// </summary>
        /// <typeparam name="KT">The key type.</typeparam>
        /// <typeparam name="ET">The entity type.</typeparam>
        /// <param name="responseHolder">The response message.</param>
        /// <param name="entitySerializer">The optional serializer.</param>
        /// <returns>Returns the relevant result.</returns>
        protected virtual IHttpActionResult ResponseFormat <KT, ET>(RepositoryHolder <KT, ET> responseHolder
                                                                    , TransportSerializer <ET> entitySerializer = null)
        {
            ApiResponse response = null;

            switch (responseHolder.ResponseCode)
            {
            case 200:
                response = new ApiResponse(HttpStatusCode.OK);
                break;

            case 201:
                response = new ApiResponse(HttpStatusCode.Created);
                break;

            case 202:
                response = new ApiResponse(HttpStatusCode.Accepted);
                break;

            case 204:
                response = new ApiResponse(HttpStatusCode.NoContent);
                break;

            case 400:
                return(BadRequest(responseHolder.ResponseMessage ?? string.Empty));

            case 404:
                return(StatusCode(HttpStatusCode.NotFound));

            case 405:
                return(StatusCode(HttpStatusCode.MethodNotAllowed));

            case 408:
                return(StatusCode(HttpStatusCode.RequestTimeout));

            case 409:
                return(StatusCode(HttpStatusCode.Conflict));

            case 412:
                return(StatusCode(HttpStatusCode.PreconditionFailed));

            case 417:
                return(StatusCode(HttpStatusCode.ExpectationFailed));

            case 442:
                return(StatusCode(HttpStatusCode.UnsupportedMediaType));

            case 502:
                return(StatusCode(HttpStatusCode.BadGateway));

            case 504:
                return(StatusCode(HttpStatusCode.GatewayTimeout));

            default:
                LogMessage(LoggingLevel.Error, $"Unexpected error occurred {responseHolder}");
                return(StatusCode(HttpStatusCode.InternalServerError));
            }

            if (entitySerializer != null && responseHolder.Entity != null)
            {
                response.MediaType = entitySerializer.MediaType;
                response.Data      = entitySerializer.GetData(responseHolder.Entity);
            }

            if (responseHolder.KeyReference != null)
            {
                response.ContentId = responseHolder.KeyReference.Item1;
                response.VersionId = responseHolder.KeyReference.Item2;
            }

            if (responseHolder.IsCached)
            {
                response.IsCached = true;
            }

            return(response);
        }