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); }
public static T Select <T>(this IConsumerRequest request, Func <NameValueCollection, T> selectFunc) { try { return(selectFunc(request.ToConsumerResponse().ToBodyParameters())); } catch (ArgumentNullException) { throw Error.FailedToParseResponse(request.ToString()); } }
public Stream FindOneAttachment(string endpointName, string itemId, string attachmentIdOrFileName) { Uri uri = ConstructChildResourceUri(_oauthSession.ConsumerContext.BaseEndpointUri, endpointName, itemId, "Attachments", attachmentIdOrFileName); IConsumerRequest oauthRequest = _oauthSession.Request() .ForMethod("GET") .WithAcceptHeader(MimeTypes.Unknown) .ForUri(uri) .SignWithToken(); var consumerResponse = oauthRequest.ToConsumerResponse(); if (consumerResponse.IsGoodResponse || consumerResponse.IsClientError) { return(consumerResponse.Stream); } throw new ApiResponseException(consumerResponse); }
public string FindAttachments(string endpointName, string itemId) { Uri uri = ConstructChildResourceUri(_oauthSession.ConsumerContext.BaseEndpointUri, endpointName, itemId, "Attachments", null); IConsumerRequest oauthRequest = _oauthSession.Request() .ForMethod("GET") .WithAcceptHeader(MimeTypes.TextXml) .ForUri(uri) .SignWithToken(); var consumerResponse = oauthRequest.ToConsumerResponse(); if (consumerResponse.IsGoodResponse || consumerResponse.IsClientError) { return(consumerResponse.Content); } throw new ApiResponseException(consumerResponse); }
public string CreateAttachment(string endpointName, string itemId, Attachment attachment) { Uri uri = ConstructChildResourceUri(_oauthSession.ConsumerContext.BaseEndpointUri, endpointName, itemId, "Attachments", attachment.FileName); IConsumerRequest oauthRequest = _oauthSession.Request() .ForMethod("PUT") .ForUri(uri) .WithRequestStream(attachment.ContentStream) .SignWithToken(); var consumerResponse = oauthRequest.ToConsumerResponse(); if (consumerResponse.IsGoodResponse || consumerResponse.IsClientError) { return(consumerResponse.Content); } throw new ApiResponseException(consumerResponse); }
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); }