Skip to content

Commit

Permalink
Refactored code and resolved all TODOs
Browse files Browse the repository at this point in the history
  • Loading branch information
wadewegner committed Oct 29, 2014
1 parent d476353 commit 51675ab
Show file tree
Hide file tree
Showing 26 changed files with 53 additions and 131 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,6 @@ public async void Chatter_Get_Groups_IsNotNull()
Assert.IsNotNull(groups);
}

//TODO: Create a test that creates a chatter group and adds to the feed so that it never fails
[Test]
public async void Chatter_Get_Group_News_Feed_IsNotNull()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//TODO: add license header

using System;
using System.Net;
using System.Net.Http;
Expand All @@ -11,7 +9,7 @@ namespace Salesforce.Common.UnitTests
{
internal class AuthenticationClientRouteHandler : DelegatingHandler
{
Action<HttpRequestMessage> _testingAction;
readonly Action<HttpRequestMessage> _testingAction;

public AuthenticationClientRouteHandler(Action<HttpRequestMessage> testingAction)
{
Expand Down
12 changes: 7 additions & 5 deletions src/CommonLibrariesForNET.UnitTests/CommonTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ namespace Salesforce.Common.UnitTests
[TestFixture]
public class CommonTests
{
private const string UserAgent = "forcedotcom-toolkit-dotnet";

[Test]
public void Auth_HasApiVersion()
{
Expand Down Expand Up @@ -49,7 +51,7 @@ public async void Requests_CheckHttpRequestMessage_HttpGet()
Assert.AreEqual(r.RequestUri.ToString(), "http://localhost:1899/services/data/v32/wade");

Assert.IsNotNull(r.Headers.UserAgent);
Assert.AreEqual(r.Headers.UserAgent.ToString(), "forcedotcom-toolkit-dotnet/v32");
Assert.AreEqual(r.Headers.UserAgent.ToString(), UserAgent + "/v32");

Assert.IsNotNull(r.Headers.Authorization);
Assert.AreEqual(r.Headers.Authorization.ToString(), "Bearer accessToken");
Expand All @@ -69,7 +71,7 @@ public async void Requests_CheckHttpRequestMessage_HttpGet_WithNode()
Assert.AreEqual(r.RequestUri.ToString(), "http://localhost:1899/services/data/v32/wade");

Assert.IsNotNull(r.Headers.UserAgent);
Assert.AreEqual(r.Headers.UserAgent.ToString(), "forcedotcom-toolkit-dotnet/v32");
Assert.AreEqual(r.Headers.UserAgent.ToString(), UserAgent + "/v32");

Assert.IsNotNull(r.Headers.Authorization);
Assert.AreEqual(r.Headers.Authorization.ToString(), "Bearer accessToken");
Expand All @@ -89,7 +91,7 @@ public async void Requests_CheckHttpRequestMessage_HttpPost()
Assert.AreEqual(r.RequestUri.ToString(), "http://localhost:1899/services/data/v32/wade");

Assert.IsNotNull(r.Headers.UserAgent);
Assert.AreEqual(r.Headers.UserAgent.ToString(), "forcedotcom-toolkit-dotnet/v32");
Assert.AreEqual(r.Headers.UserAgent.ToString(), UserAgent + "/v32");

Assert.IsNotNull(r.Headers.Authorization);
Assert.AreEqual(r.Headers.Authorization.ToString(), "Bearer accessToken");
Expand All @@ -109,7 +111,7 @@ public async void Requests_CheckHttpRequestMessage_HttpPatch()
Assert.AreEqual(r.RequestUri.ToString(), "http://localhost:1899/services/data/v32/wade");

Assert.IsNotNull(r.Headers.UserAgent);
Assert.AreEqual(r.Headers.UserAgent.ToString(), "forcedotcom-toolkit-dotnet/v32");
Assert.AreEqual(r.Headers.UserAgent.ToString(), UserAgent + "/v32");

Assert.IsNotNull(r.Headers.Authorization);
Assert.AreEqual(r.Headers.Authorization.ToString(), "Bearer accessToken");
Expand All @@ -129,7 +131,7 @@ public async void Requests_CheckHttpRequestMessage_HttpDelete()
Assert.AreEqual(r.RequestUri.ToString(), "http://localhost:1899/services/data/v32/wade");

Assert.IsNotNull(r.Headers.UserAgent);
Assert.AreEqual(r.Headers.UserAgent.ToString(), "forcedotcom-toolkit-dotnet/v32");
Assert.AreEqual(r.Headers.UserAgent.ToString(), UserAgent + "/v32");

Assert.IsNotNull(r.Headers.Authorization);
Assert.AreEqual(r.Headers.Authorization.ToString(), "Bearer accessToken");
Expand Down
2 changes: 0 additions & 2 deletions src/CommonLibrariesForNET.UnitTests/JsonContent.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//TODO: add license header

using System.IO;
using System.Net;
using System.Net.Http;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//TODO: add license header

using System;
using System.Net;
using System.Net.Http;
Expand All @@ -10,7 +8,7 @@ namespace Salesforce.Common.UnitTests
{
internal class ServiceClientRouteHandler : DelegatingHandler
{
Action<HttpRequestMessage> _testingAction;
readonly Action<HttpRequestMessage> _testingAction;

public ServiceClientRouteHandler(Action<HttpRequestMessage> testingAction)
{
Expand Down
19 changes: 8 additions & 11 deletions src/CommonLibrariesForNET/AuthenticationClient.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
//TODO: add license header

using System;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
Expand Down Expand Up @@ -46,7 +44,7 @@ public async Task UsernamePasswordAsync(string clientId, string clientSecret, st
if (string.IsNullOrEmpty(username)) throw new ArgumentNullException("username");
if (string.IsNullOrEmpty(password)) throw new ArgumentNullException("password");
if (string.IsNullOrEmpty(tokenRequestEndpointUrl)) throw new ArgumentNullException("tokenRequestEndpointUrl");
//TODO: check to make sure tokenRequestEndpointUrl is a valid URI
if (!Uri.IsWellFormedUriString(tokenRequestEndpointUrl, UriKind.Absolute)) throw new FormatException("tokenRequestEndpointUrl");

var content = new FormUrlEncodedContent(new[]
{
Expand All @@ -57,7 +55,7 @@ public async Task UsernamePasswordAsync(string clientId, string clientSecret, st
new KeyValuePair<string, string>("password", password)
});

var request = new HttpRequestMessage()
var request = new HttpRequestMessage
{
Method = HttpMethod.Post,
RequestUri = new Uri(tokenRequestEndpointUrl),
Expand Down Expand Up @@ -94,21 +92,21 @@ public async Task WebServerAsync(string clientId, string clientSecret, string re
if (string.IsNullOrEmpty(clientId)) throw new ArgumentNullException("clientId");
if (string.IsNullOrEmpty(clientSecret)) throw new ArgumentNullException("clientSecret");
if (string.IsNullOrEmpty(redirectUri)) throw new ArgumentNullException("redirectUri");
//TODO: check to make sure redirectUri is a valid URI
if (!Uri.IsWellFormedUriString(redirectUri, UriKind.Absolute)) throw new FormatException("redirectUri");
if (string.IsNullOrEmpty(code)) throw new ArgumentNullException("code");
if (string.IsNullOrEmpty(tokenRequestEndpointUrl)) throw new ArgumentNullException("tokenRequestEndpointUrl");
//TODO: check to make sure tokenRequestEndpointUrl is a valid URI
if (!Uri.IsWellFormedUriString(tokenRequestEndpointUrl, UriKind.Absolute)) throw new FormatException("tokenRequestEndpointUrl");

var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("grant_type", "authorization_code"),
new KeyValuePair<string, string>("client_id", clientId),
new KeyValuePair<string, string>("client_secret", clientSecret),
new KeyValuePair<string, string>("redirect_uri", redirectUri),
new KeyValuePair<string, string>("code", code),
new KeyValuePair<string, string>("code", code)
});

var request = new HttpRequestMessage()
var request = new HttpRequestMessage
{
Method = HttpMethod.Post,
RequestUri = new Uri(tokenRequestEndpointUrl),
Expand Down Expand Up @@ -149,7 +147,7 @@ public async Task TokenRefreshAsync(string clientId, string refreshToken, string
refreshToken,
clientSecret);

var request = new HttpRequestMessage()
var request = new HttpRequestMessage
{
Method = HttpMethod.Post,
RequestUri = new Uri(url)
Expand Down Expand Up @@ -177,7 +175,6 @@ public async Task TokenRefreshAsync(string clientId, string refreshToken, string

public void Dispose()
{
//TODO: catch in case this has already been disposed or deallocated?
_httpClient.Dispose();
}
}
Expand Down
5 changes: 1 addition & 4 deletions src/CommonLibrariesForNET/Common.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
//TODO: add license header

using System;
using System;
using Salesforce.Common.Models;

namespace Salesforce.Common
Expand All @@ -13,7 +11,6 @@ public static string FormatUrl(string resourceName, string instanceUrl, string a
if (string.IsNullOrEmpty(instanceUrl)) throw new ArgumentNullException("instanceUrl");
if (string.IsNullOrEmpty(apiVersion)) throw new ArgumentNullException("apiVersion");

// is query a nextRecordUrl request?
if (resourceName.StartsWith("/services/data", StringComparison.CurrentCultureIgnoreCase))
{
return string.Format("{0}{1}", instanceUrl, resourceName);
Expand Down
4 changes: 1 addition & 3 deletions src/CommonLibrariesForNET/ForceAuthException.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
//TODO: add license header

using Salesforce.Common.Models;
using Salesforce.Common.Models;

namespace Salesforce.Common
{
Expand Down
4 changes: 1 addition & 3 deletions src/CommonLibrariesForNET/ForceException.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
//TODO: add license header

using System;
using System;
using Salesforce.Common.Models;

namespace Salesforce.Common
Expand Down
4 changes: 1 addition & 3 deletions src/CommonLibrariesForNET/IAuthenticationClient.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
//TODO: add license header

using System.Threading.Tasks;
using System.Threading.Tasks;

namespace Salesforce.Common
{
Expand Down
4 changes: 1 addition & 3 deletions src/CommonLibrariesForNET/IForceException.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
//TODO: add license header

using System;
using System;
using System.Collections;

namespace Salesforce.Common
Expand Down
3 changes: 0 additions & 3 deletions src/CommonLibrariesForNET/IServiceClient.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//TODO: add license header

using System.Threading.Tasks;
using Salesforce.Common.Models;

Expand All @@ -8,7 +6,6 @@ namespace Salesforce.Common
public interface IServiceHttpClient
{
Task<T> HttpGetAsync<T>(string urlSuffix);
//Task<T> HttpGetAsync<T>(string urlSuffix, string nodeName);
Task<T> HttpPostAsync<T>(object inputObject, string urlSuffix);
Task<SuccessResponse> HttpPatchAsync(object inputObject, string urlSuffix);
Task<bool> HttpDeleteAsync(string urlSuffix);
Expand Down
5 changes: 1 addition & 4 deletions src/CommonLibrariesForNET/Models/AuthErrorResponse.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
//TODO: add license header
//TODO: refactor casing?

namespace Salesforce.Common.Models
namespace Salesforce.Common.Models
{
public class AuthErrorResponse
{
Expand Down
5 changes: 1 addition & 4 deletions src/CommonLibrariesForNET/Models/AuthToken.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
//TODO: add license header
//TODO: refactor casing?

namespace Salesforce.Common.Models
namespace Salesforce.Common.Models
{
public class AuthToken
{
Expand Down
5 changes: 1 addition & 4 deletions src/CommonLibrariesForNET/Models/Error.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
//TODO: add license header
//TODO: refactor casing?

namespace Salesforce.Common.Models
namespace Salesforce.Common.Models
{
public enum Error
{
Expand Down
5 changes: 1 addition & 4 deletions src/CommonLibrariesForNET/Models/ErrorResponse.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
//TODO: add license header
//TODO: refactor casing?

namespace Salesforce.Common.Models
namespace Salesforce.Common.Models
{
public class ErrorResponse
{
Expand Down
5 changes: 1 addition & 4 deletions src/CommonLibrariesForNET/Models/ErrorResponses.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
//TODO: add license header
//TODO: refactor casing?

using System.Collections.Generic;
using System.Collections.Generic;

namespace Salesforce.Common.Models
{
Expand Down
5 changes: 1 addition & 4 deletions src/CommonLibrariesForNET/Models/SuccessResponse.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
//TODO: add license header
//TODO: refactor casing?

namespace Salesforce.Common.Models
namespace Salesforce.Common.Models
{
public class SuccessResponse
{
Expand Down
23 changes: 9 additions & 14 deletions src/CommonLibrariesForNET/ServiceHttpClient.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
//TODO: add license header

using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
Expand All @@ -16,21 +14,19 @@ namespace Salesforce.Common
{
public class ServiceHttpClient : IServiceHttpClient, IDisposable
{
private static string _userAgent = "forcedotcom-toolkit-dotnet";
private const string UserAgent = "forcedotcom-toolkit-dotnet";
private readonly string _instanceUrl;
private readonly string _apiVersion;
private readonly string _accessToken;
private HttpClient _httpClient;
private readonly HttpClient _httpClient;

public ServiceHttpClient(string instanceUrl, string apiVersion, string accessToken, HttpClient httpClient)
{
_instanceUrl = instanceUrl;
_apiVersion = apiVersion;
_accessToken = accessToken;
_httpClient = httpClient;

_httpClient.DefaultRequestHeaders.UserAgent.ParseAdd(string.Concat(_userAgent, "/", _apiVersion));
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _accessToken);
_httpClient.DefaultRequestHeaders.UserAgent.ParseAdd(string.Concat(UserAgent, "/", _apiVersion));
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);

_httpClient.DefaultRequestHeaders.Accept.Clear();
_httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
Expand All @@ -45,7 +41,7 @@ public async Task<T> HttpGetAsync<T>(string urlSuffix)
{
var url = Common.FormatUrl(urlSuffix, _instanceUrl, _apiVersion);

var request = new HttpRequestMessage()
var request = new HttpRequestMessage
{
RequestUri = new Uri(url),
Method = HttpMethod.Get
Expand Down Expand Up @@ -176,8 +172,7 @@ public async Task<T> HttpPostAsync<T>(object inputObject, Uri uri)
NullValueHandling = NullValueHandling.Ignore,
});

var content = new StringContent(inputObject.ToString(), Encoding.UTF8, "application/json");

var content = new StringContent(json, Encoding.UTF8, "application/json");
var responseMessage = await _httpClient.PostAsync(uri, content).ConfigureAwait(false);
var response = await responseMessage.Content.ReadAsStringAsync().ConfigureAwait(false);

Expand All @@ -195,7 +190,7 @@ public async Task<SuccessResponse> HttpPatchAsync(object inputObject, string url
{
var url = Common.FormatUrl(urlSuffix, _instanceUrl, _apiVersion);

var request = new HttpRequestMessage()
var request = new HttpRequestMessage
{
RequestUri = new Uri(url),
Method = new HttpMethod("PATCH")
Expand Down Expand Up @@ -233,7 +228,7 @@ public async Task<bool> HttpDeleteAsync(string urlSuffix)
{
var url = Common.FormatUrl(urlSuffix, _instanceUrl, _apiVersion);

var request = new HttpRequestMessage()
var request = new HttpRequestMessage
{
RequestUri = new Uri(url),
Method = HttpMethod.Delete
Expand Down
Loading

0 comments on commit 51675ab

Please sign in to comment.