Skip to content

Commit 04146be

Browse files
Merge remote-tracking branch 'refs/remotes/origin/master' into 891-async-fix
# Conflicts: # Example/Example.cs # README.md
2 parents 391b060 + c387bf0 commit 04146be

File tree

10 files changed

+102
-16
lines changed

10 files changed

+102
-16
lines changed

CHANGELOG.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,23 @@ All notable changes to this project will be documented in this file.
33

44
This project adheres to [Semantic Versioning](http://semver.org/).
55

6+
## [2.0.7] - 2016-07-19
7+
### Added
8+
- [Pull request #11](https://github.com/sendgrid/csharp-http-client/pull/11): Adding the option to set WebProxy object to be used on HttpClient
9+
- Big thanks to [Juliano Nunes](https://github.com/julianonunes) for the pull request!
10+
11+
## [2.0.6] - 2016-07-18
12+
### Added
13+
- Sign assembly with a strong name
14+
15+
## [2.0.5] - 2016-07-14
16+
### Fixed
17+
- Solves [issue #7](https://github.com/sendgrid/csharp-http-client/issues/7)
18+
- Solves [issue #256](https://github.com/sendgrid/sendgrid-csharp/issues/256) in the SendGrid C# Client
19+
- Do not try to encode the JSON request payload by replacing single quotes with double quotes
20+
- Updated examples and README to use JSON.NET to encode the payload
21+
- Thanks to [Gunnar Liljas](https://github.com/gliljas) for helping identify the issue!
22+
623
## [2.0.2] - 2016-06-16
724
### Added
825
- Fix async, per https://github.com/sendgrid/sendgrid-csharp/issues/235

CSharpHTTPClient/Client.cs

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,27 @@ public class Client : DynamicObject
6868
public string Version;
6969
public string UrlPath;
7070
public string MediaType;
71+
public WebProxy WebProxy;
7172
public enum Methods
7273
{
7374
DELETE, GET, PATCH, POST, PUT
7475
}
7576

77+
78+
/// <summary>
79+
/// REST API client.
80+
/// </summary>
81+
/// <param name="host">Base url (e.g. https://api.sendgrid.com)</param>
82+
/// <param name="requestHeaders">A dictionary of request headers</param>
83+
/// <param name="version">API version, override AddVersion to customize</param>
84+
/// <param name="urlPath">Path to endpoint (e.g. /path/to/endpoint)</param>
85+
/// <returns>Fluent interface to a REST API</returns>
86+
public Client(WebProxy webProxy, string host, Dictionary<string, string> requestHeaders = null, string version = null, string urlPath = null)
87+
: this(host, requestHeaders, version, urlPath)
88+
{
89+
WebProxy = webProxy;
90+
}
91+
7692
/// <summary>
7793
/// REST API client.
7894
/// </summary>
@@ -159,6 +175,28 @@ private Client BuildClient(string name = null)
159175

160176
}
161177

178+
/// <summary>
179+
/// Factory method to return the right HttpClient settings.
180+
/// </summary>
181+
/// <returns>Instance of HttpClient</returns>
182+
private HttpClient BuildHttpClient()
183+
{
184+
// Add the WebProxy if set
185+
if (WebProxy != null)
186+
{
187+
var httpClientHandler = new HttpClientHandler()
188+
{
189+
Proxy = WebProxy,
190+
PreAuthenticate = true,
191+
UseDefaultCredentials = false,
192+
};
193+
194+
return new HttpClient(httpClientHandler);
195+
}
196+
197+
return new HttpClient();
198+
}
199+
162200
/// <summary>
163201
/// Add the authorization header, override to customize
164202
/// </summary>
@@ -272,9 +310,9 @@ public async virtual Task<Response> MakeRequest(HttpClient client, HttpRequestMe
272310
/// <param name="requestBody">JSON formatted string</param>
273311
/// <param name="queryParams">JSON formatted queary paramaters</param>
274312
/// <returns>Response object</returns>
275-
private async Task<Response> RequestAsync(string method, String requestBody = null, String queryParams = null)
313+
private async Task<Response> RequestAsync(string method, string requestBody = null, string queryParams = null)
276314
{
277-
using (var client = new HttpClient())
315+
using (var client = BuildHttpClient())
278316
{
279317
try
280318
{
@@ -308,7 +346,7 @@ private async Task<Response> RequestAsync(string method, String requestBody = nu
308346
StringContent content = null;
309347
if (requestBody != null)
310348
{
311-
content = new StringContent(requestBody.ToString().Replace("'", "\""), Encoding.UTF8, MediaType);
349+
content = new StringContent(requestBody, Encoding.UTF8, MediaType);
312350
}
313351

314352
// Build the final request

CSharpHTTPClient/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@
3131
// You can specify all the values or you can default the Build and Revision Numbers
3232
// by using the '*' as shown below:
3333
// [assembly: AssemblyVersion("1.0.*")]
34-
[assembly: AssemblyVersion("2.0.4")]
35-
[assembly: AssemblyFileVersion("2.0.4")]
34+
[assembly: AssemblyVersion("2.0.7")]
35+
[assembly: AssemblyFileVersion("2.0.7")]

Example/App.config

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<configuration>
33
<startup>
4-
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
55
</startup>
6+
<runtime>
7+
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
8+
<dependentAssembly>
9+
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
10+
<bindingRedirect oldVersion="0.0.0.0-9.0.0.0" newVersion="9.0.0.0" />
11+
</dependentAssembly>
12+
</assemblyBinding>
13+
</runtime>
614
</configuration>

Example/Example.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using SendGrid.CSharp.HTTP.Client;
44
using System.Web.Script.Serialization;
55
using System.Threading.Tasks;
6+
using Newtonsoft.Json;
67

78
// This is a working example, using the SendGrid API
89
// You will need a SendGrid account and an active API Key
@@ -62,6 +63,7 @@ static async Task Execute()
6263
'alerts.read'
6364
]
6465
}";
66+
Object json = JsonConvert.DeserializeObject<Object>(requestBody);
6567
requestHeaders.Clear();
6668
requestHeaders.Add("X-Test", "test2");
6769
response = await client.api_keys.post(requestBody: requestBody, requestHeaders: requestHeaders);
@@ -88,7 +90,8 @@ static async Task Execute()
8890
requestBody = @"{
8991
'name': 'A New Hope'
9092
}";
91-
response = await client.api_keys._(api_key_id).patch(requestBody: requestBody);
93+
json = JsonConvert.DeserializeObject<Object>(requestBody);
94+
response = await client.api_keys._(api_key_id).patch(requestBody: json.ToString());
9295
Console.WriteLine(response.StatusCode);
9396
Console.WriteLine(response.Body.ReadAsStringAsync().Result);
9497
Console.WriteLine(response.Headers.ToString());
@@ -104,7 +107,8 @@ static async Task Execute()
104107
'user.profile.update'
105108
]
106109
}";
107-
response = await client.api_keys._(api_key_id).put(requestBody: requestBody);
110+
json = JsonConvert.DeserializeObject<Object>(requestBody);
111+
response = await client.api_keys._(api_key_id).put(requestBody: json.ToString());
108112
Console.WriteLine(response.StatusCode);
109113
Console.WriteLine(response.Body.ReadAsStringAsync().Result);
110114
Console.WriteLine(response.Headers.ToString());

Example/Example.csproj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,17 @@
3434
<WarningLevel>4</WarningLevel>
3535
</PropertyGroup>
3636
<ItemGroup>
37+
<Reference Include="Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
38+
<HintPath>..\CSharpHTTPClient\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
39+
<Private>True</Private>
40+
</Reference>
3741
<Reference Include="System" />
3842
<Reference Include="System.Core" />
43+
<Reference Include="System.Net.Http.Formatting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
44+
<HintPath>..\CSharpHTTPClient\packages\Microsoft.AspNet.WebApi.Client.4.0.20710.0\lib\net40\System.Net.Http.Formatting.dll</HintPath>
45+
<Private>True</Private>
46+
</Reference>
47+
<Reference Include="System.Net.Http.WebRequest" />
3948
<Reference Include="System.Web.Extensions" />
4049
<Reference Include="System.Xml.Linq" />
4150
<Reference Include="System.Data.DataSetExtensions" />
@@ -50,6 +59,7 @@
5059
</ItemGroup>
5160
<ItemGroup>
5261
<None Include="App.config" />
62+
<None Include="packages.config" />
5363
</ItemGroup>
5464
<ItemGroup>
5565
<ProjectReference Include="..\CSharpHTTPClient\CSharpHTTPClient.csproj">

Example/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@
3232
// You can specify all the values or you can default the Build and Revision Numbers
3333
// by using the '*' as shown below:
3434
// [assembly: AssemblyVersion("1.0.*")]
35-
[assembly: AssemblyVersion("2.0.4")]
36-
[assembly: AssemblyFileVersion("2.0.4")]
35+
[assembly: AssemblyVersion("2.0.7")]
36+
[assembly: AssemblyFileVersion("2.0.7")]

Example/packages.config

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="Microsoft.AspNet.WebApi.Client" version="4.0.20710.0" targetFramework="net45" />
4+
<package id="Microsoft.Net.Http" version="2.0.20710.0" targetFramework="net45" />
5+
<package id="Newtonsoft.Json" version="9.0.1" targetFramework="net45" />
6+
</packages>

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,15 @@ Console.WriteLine(response.Headers.ToString());
4444

4545
```csharp
4646
using SendGrid.CSharp.HTTP.Client;
47+
using Newtonsoft.Json;
4748
globalRequestHeaders.Add("Authorization", "Bearer XXXXXXX");
4849
dynamic client = new Client(host: baseUrl, requestHeaders: globalRequestHeaders);
49-
string queryParams = "{'Hello': 0, 'World': 1}";
50+
string queryParams = @"{'Hello': 0, 'World': 1}";
5051
requestHeaders.Add("X-Test", "test");
51-
string requestBody = "{'some': 1, 'awesome': 2, 'data': 3}";
52-
var response = await client.your.api._(param).call.post(requestBody: requestBody,
52+
<<<<<<< HEAD
53+
string requestBody = @"{'some': 1, 'awesome': 2, 'data': 3}";
54+
Object json = JsonConvert.DeserializeObject<Object>(requestBody);
55+
var response = await client.your.api._(param).call.post(requestBody: json.ToString(),
5356
queryParams: queryParams,
5457
requestHeaders: requestHeaders)
5558
Console.WriteLine(response.StatusCode);

UnitTest/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@
3232
// You can specify all the values or you can default the Build and Revision Numbers
3333
// by using the '*' as shown below:
3434
// [assembly: AssemblyVersion("1.0.*")]
35-
[assembly: AssemblyVersion("2.0.4")]
36-
[assembly: AssemblyFileVersion("2.0.4")]
35+
[assembly: AssemblyVersion("2.0.7")]
36+
[assembly: AssemblyFileVersion("2.0.7")]

0 commit comments

Comments
 (0)