Skip to content

Commit 7ed00da

Browse files
authored
Merge pull request #17 from mailjet/feature/add-v4-and-sms
Feature/add v4 and sms
2 parents e7180d7 + 843dab5 commit 7ed00da

16 files changed

+274
-67
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
language: csharp
22
dist: trusty
33
mono: none
4-
dotnet: 1.0.1
4+
dotnet: 1.1.4
55
script:
66
- dotnet restore
77
- dotnet build ./Mailjet.Client/Mailjet.Client.csproj -f netstandard1.1 -c Release

Mailjet.Client/DefaultProxy.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
42
using System.Net;
5-
using System.Text;
6-
using System.Threading.Tasks;
73

84
namespace Mailjet.Client
95
{

Mailjet.Client/IMailjetClient.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
1+
using System.Threading.Tasks;
62

73
namespace Mailjet.Client
84
{

Mailjet.Client/MailjetClient.cs

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
using Newtonsoft.Json;
22
using Newtonsoft.Json.Linq;
33
using System;
4-
using System.Collections.Generic;
5-
using System.Linq;
6-
using System.Net;
74
using System.Net.Http;
85
using System.Net.Http.Headers;
96
using System.Text;
@@ -15,6 +12,7 @@ public enum ApiVersion
1512
{
1613
V3,
1714
V3_1,
15+
V4,
1816
}
1917

2018
/// <summary>
@@ -27,29 +25,26 @@ public class MailjetClient : IMailjetClient
2725
private const string JsonMediaType = "application/json";
2826
private const string ApiVersionPathV3 = "v3";
2927
private const string ApiVersionPathV3_1 = "v3.1";
28+
private const string ApiVersionPathV4 = "v4";
3029

31-
private readonly HttpClient _httpClient;
30+
private HttpClient _httpClient;
3231

3332
public MailjetClient(string apiKey, string apiSecret, HttpMessageHandler httpMessageHandler = null)
3433
{
35-
// Create HttpClient
36-
_httpClient = (httpMessageHandler != null) ? new HttpClient(httpMessageHandler) : new HttpClient();
37-
38-
// Set base URI
39-
_httpClient.BaseAddress = new Uri(DefaultBaseAdress);
40-
41-
// Set accepted media type
42-
_httpClient.DefaultRequestHeaders.Accept.Clear();
43-
_httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(JsonMediaType));
44-
45-
// Set user-agent
46-
_httpClient.DefaultRequestHeaders.UserAgent.ParseAdd(UserAgent);
34+
InitHttpClient(httpMessageHandler);
4735

4836
// Set basic authentification
4937
var byteArray = Encoding.UTF8.GetBytes(string.Format("{0}:{1}", apiKey, apiSecret));
5038
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
5139
}
5240

41+
public MailjetClient(string token, HttpMessageHandler httpMessageHandler = null)
42+
{
43+
InitHttpClient(httpMessageHandler);
44+
45+
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
46+
}
47+
5348
public ApiVersion Version { get; set; } = ApiVersion.V3;
5449

5550
public string BaseAdress
@@ -131,6 +126,22 @@ private async Task<JObject> GetContent(HttpResponseMessage responseMessage)
131126
return content;
132127
}
133128

129+
private void InitHttpClient(HttpMessageHandler httpMessageHandler)
130+
{
131+
// Create HttpClient
132+
_httpClient = (httpMessageHandler != null) ? new HttpClient(httpMessageHandler) : new HttpClient();
133+
134+
// Set base URI
135+
_httpClient.BaseAddress = new Uri(DefaultBaseAdress);
136+
137+
// Set accepted media type
138+
_httpClient.DefaultRequestHeaders.Accept.Clear();
139+
_httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(JsonMediaType));
140+
141+
// Set user-agent
142+
_httpClient.DefaultRequestHeaders.UserAgent.ParseAdd(UserAgent);
143+
}
144+
134145
private string BuildUrl(MailjetRequest request)
135146
{
136147
return UrlHelper.CombineUrl(GetApiVersionPath(), request.BuildUrl());
@@ -140,8 +151,12 @@ private string GetApiVersionPath()
140151
{
141152
switch (Version)
142153
{
143-
case ApiVersion.V3_1: return ApiVersionPathV3_1;
144-
default: return ApiVersionPathV3;
154+
case ApiVersion.V3_1:
155+
return ApiVersionPathV3_1;
156+
case ApiVersion.V4:
157+
return ApiVersionPathV4;
158+
default:
159+
return ApiVersionPathV3;
145160
}
146161
}
147162
}

Mailjet.Client/MailjetClientHandler.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Net.Http;
4-
using System.Text;
1+
using System.Net.Http;
52

63
namespace Mailjet.Client
74
{

Mailjet.Client/MailjetRequest.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
using Newtonsoft.Json.Linq;
22
using System;
33
using System.Collections.Generic;
4-
using System.Linq;
5-
using System.Text;
6-
using System.Threading.Tasks;
74

85
namespace Mailjet.Client
96
{

Mailjet.Client/MailjetResponse.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
using Newtonsoft.Json.Linq;
22
using System;
3-
using System.Collections.Generic;
4-
using System.Linq;
5-
using System.Text;
6-
using System.Threading.Tasks;
73

84
namespace Mailjet.Client
95
{
@@ -51,7 +47,7 @@ public JArray GetData()
5147
return result;
5248
}
5349

54-
result = new JArray();
50+
result = new JArray(_content);
5551
return result;
5652
}
5753

Mailjet.Client/ResourceId.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
6-
7-
namespace Mailjet.Client
1+
namespace Mailjet.Client
82
{
93
public class ResourceId
104
{

Mailjet.Client/ResourceInfo.cs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.IO;
4-
using System.Linq;
5-
using System.Text;
6-
using System.Threading.Tasks;
7-
8-
namespace Mailjet.Client
1+
namespace Mailjet.Client
92
{
103
public enum ResourceType
114
{
125
NotSpecified,
136
Rest,
147
Data,
158
Send,
9+
V4,
1610
}
1711

1812
public class ResourceInfo
@@ -57,10 +51,15 @@ private string GetPath()
5751
{
5852
switch (Type)
5953
{
60-
case ResourceType.Rest: return "REST";
61-
case ResourceType.Data: return "DATA";
62-
case ResourceType.Send: return string.Empty;
63-
default: return Resource != "send" ? "REST" : string.Empty;
54+
case ResourceType.Rest:
55+
return "REST";
56+
case ResourceType.Data:
57+
return "DATA";
58+
case ResourceType.Send:
59+
case ResourceType.V4:
60+
return string.Empty;
61+
default:
62+
return Resource != "send" ? "REST" : string.Empty;
6463
}
6564
}
6665
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace Mailjet.Client.Resources.SMS
2+
{
3+
public static class Count
4+
{
5+
public static readonly ResourceInfo Resource = new ResourceInfo("sms/count", null, ResourceType.V4);
6+
7+
public const string To = "To";
8+
public const string FromTS = "FromTS";
9+
public const string ToTS = "ToTS";
10+
public const string StatusCode = "StatusCode";
11+
}
12+
}

0 commit comments

Comments
 (0)