Skip to content
This repository has been archived by the owner on Nov 10, 2020. It is now read-only.

Commit

Permalink
Remove legacy uses of WHERE and ORDERBY querystrings
Browse files Browse the repository at this point in the history
  • Loading branch information
danbarratt committed Nov 17, 2011
1 parent 4ff9bc1 commit a01eac2
Showing 1 changed file with 31 additions and 28 deletions.
59 changes: 31 additions & 28 deletions source/XeroApi/Integration/IntegrationProxy.cs
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Net;
Expand All @@ -21,20 +22,18 @@ public IntegrationProxy (IOAuthSession oauthSession)
{
_oauthSession = oauthSession;
}


#region Structured Data Read/Write methods

public string FindElements(IApiQueryDescription apiQueryDescription)
{
IConsumerResponse consumerResponse = CallApi(
_oauthSession,
"GET",
string.Empty,
_oauthSession.ConsumerContext.BaseEndpointUri,
ModelTypeHelper.Pluralize(apiQueryDescription.ElementName),
apiQueryDescription.ElementId,
/*apiQueryDescription.Where,*/
/*apiQueryDescription.Order,*/
apiQueryDescription.UpdatedSinceDate,
apiQueryDescription.QueryStringParams,
null);
Expand All @@ -58,14 +57,11 @@ public string FindElements(IApiQueryDescription apiQueryDescription)
public byte[] FindOne(string endpointName, string itemId, string acceptMimeType)
{
IConsumerResponse consumerResponse = CallApi(
_oauthSession,
"GET",
string.Empty,
_oauthSession.ConsumerContext.BaseEndpointUri,
ModelTypeHelper.Pluralize(endpointName),
itemId,
/*null,*/
/*null, */
null,
null,
acceptMimeType);
Expand All @@ -82,13 +78,10 @@ public byte[] FindOne(string endpointName, string itemId, string acceptMimeType)
public string UpdateOrCreateElements(string endpointName, string body)
{
IConsumerResponse consumerResponse = CallApi(
_oauthSession,
"POST",
body,
_oauthSession.ConsumerContext.BaseEndpointUri,
ModelTypeHelper.Pluralize(endpointName),
/*null,*/
/*null,*/
null,
null,
new NameValueCollection { { "summarizeErrors", "false" } },
Expand All @@ -106,13 +99,10 @@ public string UpdateOrCreateElements(string endpointName, string body)
public string CreateElements(string endpointName, string body)
{
IConsumerResponse consumerResponse = CallApi(
_oauthSession,
"PUT",
body,
_oauthSession.ConsumerContext.BaseEndpointUri,
ModelTypeHelper.Pluralize(endpointName),
/*null,*/
/*null,*/
null,
null,
new NameValueCollection { { "summarizeErrors", "false" } },
Expand Down Expand Up @@ -214,13 +204,15 @@ public Stream FindOneAttachment(string endpointName, string itemId, string attac
#endregion


private static IConsumerResponse CallApi(IOAuthSession oauthSession, string method, string body, Uri baseUrl, string endpointName, string itemId, /*string whereClause, string orderBy,*/ DateTime? lastModifiedDate, NameValueCollection additionalQueryParams, string acceptMimeType)
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();

Uri uri = ConstructUri(baseUrl, endpointName, itemId, /*whereClause, orderBy,*/ additionalQueryParams);
NameValueCollection allQueryParams = additionalQueryParams ?? new NameValueCollection();

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

IConsumerRequest oauthRequest = oauthSession.Request()
IConsumerRequest oauthRequest = _oauthSession.Request()
.ForMethod(method)
.ForUri(uri)
.WithAcceptHeader(acceptMimeType ?? "text/xml")
Expand All @@ -236,8 +228,8 @@ private static IConsumerResponse CallApi(IOAuthSession oauthSession, string meth
oauthRequest.Context.Headers.Add("If-Modified-Since", lastModifiedDate.Value.ToString("yyyy-MM-dd HH:mm:ss"));
}

var consumerResponse = oauthRequest.ToConsumerResponse();

IConsumerResponse consumerResponse = oauthRequest.ToConsumerResponse();
// Check for <ApiException> response message
if (consumerResponse.Content.StartsWith("<ApiException"))
{
Expand All @@ -248,7 +240,16 @@ private static IConsumerResponse CallApi(IOAuthSession oauthSession, string meth
return consumerResponse;
}

public static Uri ConstructUri(Uri baseUrl, string endpointName, string itemId, /*string whereClause, string orderBy,*/ NameValueCollection additionalQueryParams)

/// <summary>
/// Constructs the URI.
/// </summary>
/// <param name="baseUrl">The base URL.</param>
/// <param name="endpointName">Name of the endpoint.</param>
/// <param name="itemId">The item id.</param>
/// <param name="additionalQueryParams">The additional query params.</param>
/// <returns></returns>
public static Uri ConstructUri(Uri baseUrl, string endpointName, string itemId, NameValueCollection additionalQueryParams)
{
UriBuilder uriBuilder = new UriBuilder(baseUrl);

Expand All @@ -264,23 +265,25 @@ public static Uri ConstructUri(Uri baseUrl, string endpointName, string itemId,
uriBuilder.Path += ("/");
uriBuilder.Path += (itemId);
}

NameValueCollection queryStringParameters = additionalQueryParams ?? new NameValueCollection();

/*if (!string.IsNullOrEmpty(whereClause))
queryStringParameters.Add("WHERE", whereClause.Trim());*/

/*if (!string.IsNullOrEmpty(orderBy))
queryStringParameters.Add("ORDER", orderBy);*/

string queryString = DevDefined.OAuth.Framework.UriUtility.FormatQueryString(queryStringParameters);

string queryString = DevDefined.OAuth.Framework.UriUtility.FormatQueryString(additionalQueryParams);

if (!string.IsNullOrEmpty(queryString))
uriBuilder.Query = queryString;

return uriBuilder.Uri;
}


/// <summary>
/// Constructs the child resource URI.
/// </summary>
/// <param name="baseUrl">The base URL.</param>
/// <param name="endpointName">Name of the endpoint.</param>
/// <param name="itemId">The item id.</param>
/// <param name="childResourceName">Name of the child resource.</param>
/// <param name="childResourceId">The child resource id.</param>
/// <returns></returns>
public static Uri ConstructChildResourceUri(Uri baseUrl, string endpointName, string itemId, string childResourceName, string childResourceId)
{
if (string.IsNullOrEmpty(endpointName)) throw new ArgumentNullException("endpointName");
Expand Down

0 comments on commit a01eac2

Please sign in to comment.