// GET (one)

        public Attachment GetAttachmentFor <TModel>(TModel model)
            where TModel : ModelBase, IAttachmentParent
        {
            // List the attachments against this model.
            var modelItemId = ModelTypeHelper.GetModelItemId(model);

            var allAttachmentsXml = _integrationProxy.FindAttachments(typeof(TModel).Name, modelItemId);

            var allAttachments = ModelSerializer.DeserializeTo <Response>(allAttachmentsXml).Attachments;

            if (allAttachments == null || allAttachments.Count == 0)
            {
                return(null);
            }

            var theFirstAttachment = allAttachments.First();

            // Get the attachment content
            var content = _integrationProxy.FindOneAttachment(
                typeof(TModel).Name,
                modelItemId,
                theFirstAttachment.AttachmentID.ToString());

            return(theFirstAttachment.WithContent(content));
        }
Ejemplo n.º 2
0
        private IConsumerResponse CallApi(string method, string body, Uri baseUrl, string endpointName, string itemId, DateTime?lastModifiedDate, NameValueCollection additionalQueryParams, string acceptMimeType)
        {
            method = string.IsNullOrEmpty(method) ? "GET" : method.ToUpper();

            NameValueCollection allQueryParams = additionalQueryParams ?? new NameValueCollection();

            Uri uri = ConstructUri(baseUrl, endpointName, itemId, allQueryParams);

            IConsumerRequest request = _oauthSession.Request()
                                       .ForMethod(method)
                                       .ForUri(uri)
                                       .WithAcceptHeader(acceptMimeType ?? "text/xml")
                                       .WithIfModifiedSince(lastModifiedDate)
                                       .SignWithToken();

            if ((method == "PUT" || method == "POST"))
            {
                request = request.WithBody(body);
            }

            IConsumerResponse consumerResponse = request.ToConsumerResponse();

            // Check for <ApiException> response message
            if (consumerResponse.Content.StartsWith("<ApiException"))
            {
                ApiExceptionDetails details = ModelSerializer.DeserializeTo <ApiExceptionDetails>(consumerResponse.Content);
                throw new ApiException(details);
            }

            return(consumerResponse);
        }
Ejemplo n.º 3
0
        public Allocation Allocate(CreditNote creditNote, Allocation allocation)
        {
            string requestXml = ModelSerializer.Serialize(allocation);

            string responseXml = _proxy.ApplyAllocation(creditNote, requestXml);

            Response response = ModelSerializer.DeserializeTo <Response>(responseXml);

            return(response.GetTypedProperty <Allocation>().First());
        }
        public Attachment Create <TModel>(TModel model, Attachment attachment)
            where TModel : ModelBase, IAttachmentParent
        {
            string xml = _integrationProxy.CreateAttachment(
                typeof(TModel).Name,
                ModelTypeHelper.GetModelItemId(model),
                attachment);

            return(ModelSerializer.DeserializeTo <Response>(xml).Attachments.First());
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Updates the specified items in the remote repository
        /// </summary>
        /// <typeparam name="TModel">The type of the model.</typeparam>
        /// <param name="itemToUpdate">The item to update.</param>
        /// <returns></returns>
        public TModel UpdateOrCreate <TModel>(TModel itemToUpdate)
            where TModel : EndpointModelBase
        {
            string requestXml = ModelSerializer.Serialize(itemToUpdate);

            string responseXml = _proxy.UpdateOrCreateElements(typeof(TModel).Name, requestXml);

            Response response = ModelSerializer.DeserializeTo <Response>(responseXml);

            return(response.GetTypedProperty <TModel>().First());
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Updates the specified items in the remote repository
        /// </summary>
        /// <typeparam name="TModel">The type of the model.</typeparam>
        /// <param name="itemsToUpdate">The items to update.</param>
        /// <returns></returns>
        public IEnumerable <TModel> UpdateOrCreate <TModel>(ICollection <TModel> itemsToUpdate)
            where TModel : EndpointModelBase
        {
            string requestXml = ModelSerializer.Serialize(itemsToUpdate);

            string responseXml = _proxy.UpdateOrCreateElements(typeof(TModel).Name, requestXml);

            Response response = ModelSerializer.DeserializeTo <Response>(responseXml);

            return(response.GetTypedProperty <TModel>());
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Finds an item from the remote repository by Id
        /// </summary>
        /// <typeparam name="TModel">The type of the model.</typeparam>
        /// <param name="id">The id.</param>
        /// <returns></returns>
        public TModel FindById <TModel>(string id)
            where TModel : EndpointModelBase
        {
            var queryDescription = new LinqQueryDescription {
                ElementId = id, ElementType = typeof(TModel)
            };
            string responseXml = _proxy.FindElements(queryDescription);

            Response response = ModelSerializer.DeserializeTo <Response>(responseXml);

            return(response.GetTypedProperty <TModel>().FirstOrDefault());
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Gets the published report.
        /// </summary>
        /// <param name="reportId">The report id.</param>
        /// <returns></returns>
        public Report GetPublishedReport(Guid reportId)
        {
            var queryDescription = new ReportQueryDescription
            {
                ElementName = "Report",
                ElementId   = reportId.ToString(),
            };

            string xml = _integrationProxy.FindElements(queryDescription);

            Response response = ModelSerializer.DeserializeTo <Response>(xml);

            return(response.Reports[0]);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Runs a dynamic report.
        /// </summary>
        /// <typeparam name="TReport">The type of the report.</typeparam>
        /// <param name="report">The report.</param>
        /// <returns></returns>
        public Report RunDynamicReport <TReport>(TReport report)
            where TReport : DynamicReportBase
        {
            var queryDescription = new ReportQueryDescription
            {
                ElementName       = "Report",
                ElementId         = report.ReportName,
                QueryStringParams = report.GetQueryStringParamCollection()
            };

            string xml = _integrationProxy.FindElements(queryDescription);

            Response response = ModelSerializer.DeserializeTo <Response>(xml);

            return(response.Reports[0]);
        }
Ejemplo n.º 10
0
        public string ApplyAllocation(CreditNote creditNote, string body)
        {
            Uri uri = ConstructChildResourceUri(_oauthSession.ConsumerContext.BaseEndpointUri, "CreditNotes", creditNote.CreditNoteID.ToString(), "Allocations", null);

            IConsumerRequest oauthRequest = _oauthSession.Request()
                                            .ForMethod("PUT")
                                            .WithAcceptHeader(MimeTypes.TextXml)
                                            .ForUri(uri)
                                            .SignWithToken()
                                            .WithBody(body);

            var consumerResponse = oauthRequest.ToConsumerResponse();

            // Check for <ApiException> response message
            if (consumerResponse.Content.StartsWith("<ApiException"))
            {
                ApiExceptionDetails details = ModelSerializer.DeserializeTo <ApiExceptionDetails>(consumerResponse.Content);
                throw new ApiException(details);
            }

            return(consumerResponse.Content);
        }
Ejemplo n.º 11
0
        public override object Execute(Expression expression)
        {
            LinqQueryDescription queryDescription = Translate(expression);

            // Call the API..
            string xml = _proxy.FindElements(queryDescription);

            Response response = ModelSerializer.DeserializeTo <Response>(xml);


            // Guard against an empty response..
            IModelList elementCollection = (response == null)
                ? (IModelList)Activator.CreateInstance(queryDescription.ElementListType)        // TODO: too much going on here, needs tidying up
                : response.GetTypedProperty(queryDescription.ElementListType);


            if (queryDescription.ClientSideExpression == null)
            {
                return(elementCollection);
            }

            switch (queryDescription.ClientSideExpression)
            {
            case "FirstOrDefault":

                return(elementCollection.Count == 0
                        ? null
                        : elementCollection[0]);

            case "First":

                if (elementCollection.Count == 0)
                {
                    throw new InvalidOperationException("The ModelList contains no items");
                }

                return(elementCollection[0]);

            case "SingleOrDefault":

                if (elementCollection.Count > 1)
                {
                    throw new InvalidOperationException("The ModelList contains no items");
                }

                if (elementCollection.Count == 0)
                {
                    return(null);
                }

                return(elementCollection[0]);

            case "Single":

                if (elementCollection.Count != 1)
                {
                    throw new InvalidOperationException("The ModelList contains no items");
                }

                return(elementCollection[0]);

            case "Count":

                return(elementCollection.Count);

            default:

                throw new NotImplementedException(string.Format("The client side aggregator {0} cannot currently be performed", queryDescription.ClientSideExpression));
            }
        }