Skip to content

Commit 0c71235

Browse files
updated user agent logic
1 parent f0e131c commit 0c71235

File tree

3 files changed

+30
-24
lines changed

3 files changed

+30
-24
lines changed

src/sib_api_v3_sdk/Client/ApiClient.cs

+17-14
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public ApiClient(Configuration config)
7777
/// <param name="basePath">The base path.</param>
7878
public ApiClient(String basePath = "https://api.sendinblue.com/v3")
7979
{
80-
if (String.IsNullOrEmpty(basePath))
80+
if (String.IsNullOrEmpty(basePath))
8181
throw new ArgumentException("basePath cannot be empty");
8282

8383
RestClient = new RestClient(basePath);
@@ -121,23 +121,23 @@ private RestRequest PrepareRequest(
121121
request.Serializer = null;
122122

123123
// add path parameter, if any
124-
foreach(var param in pathParams)
124+
foreach (var param in pathParams)
125125
request.AddParameter(param.Key, param.Value, ParameterType.UrlSegment);
126126

127127
// add header parameter, if any
128-
foreach(var param in headerParams)
128+
foreach (var param in headerParams)
129129
request.AddHeader(param.Key, param.Value);
130130

131131
// add query parameter, if any
132-
foreach(var param in queryParams)
132+
foreach (var param in queryParams)
133133
request.AddQueryParameter(param.Key, param.Value);
134134

135135
// add form parameter, if any
136-
foreach(var param in formParams)
136+
foreach (var param in formParams)
137137
request.AddParameter(param.Key, param.Value);
138138

139139
// add file parameter, if any
140-
foreach(var param in fileParams)
140+
foreach (var param in fileParams)
141141
{
142142
request.AddFile(param.Value);
143143
}
@@ -175,15 +175,18 @@ public Object CallApi(
175175

176176
// set timeout
177177
RestClient.Timeout = TimeSpan.FromMilliseconds(Configuration.Timeout);
178-
178+
179179
// set user agent
180-
RestClient.UserAgent = Configuration.UserAgent.Replace("v#", "v3.1.0");
180+
if (!Configuration.UserAgent.ToLower().StartsWith("sendinblue_"))
181+
Configuration.UserAgent = "sendinblue_clientAPI/v3.1.0/c#";
182+
183+
RestClient.UserAgent = Configuration.UserAgent;
181184

182185
InterceptRequest(request);
183186
var response = RestClient.Execute(request).Result;
184187
InterceptResponse(request, response);
185188

186-
return (Object) response;
189+
return (Object)response;
187190
}
188191
/// <summary>
189192
/// Makes the asynchronous HTTP request.
@@ -251,13 +254,13 @@ public string ParameterToString(object obj)
251254
// Defaults to an ISO 8601, using the known as a Round-trip date/time pattern ("o")
252255
// https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx#Anchor_8
253256
// For example: 2009-06-15T13:45:30.0000000
254-
return ((DateTime)obj).ToString (Configuration.DateTimeFormat);
257+
return ((DateTime)obj).ToString(Configuration.DateTimeFormat);
255258
else if (obj is DateTimeOffset)
256259
// Return a formatted date string - Can be customized with Configuration.DateTimeFormat
257260
// Defaults to an ISO 8601, using the known as a Round-trip date/time pattern ("o")
258261
// https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx#Anchor_8
259262
// For example: 2009-06-15T13:45:30.0000000
260-
return ((DateTimeOffset)obj).ToString (Configuration.DateTimeFormat);
263+
return ((DateTimeOffset)obj).ToString(Configuration.DateTimeFormat);
261264
else if (obj is IList)
262265
{
263266
var flattenedString = new StringBuilder();
@@ -272,7 +275,7 @@ public string ParameterToString(object obj)
272275
else if (obj is bool)
273276
return ((bool)obj).ToString().ToLower();
274277
else
275-
return Convert.ToString (obj);
278+
return Convert.ToString(obj);
276279
}
277280

278281
/// <summary>
@@ -315,7 +318,7 @@ public object Deserialize(IRestResponse response, Type type)
315318

316319
if (type.Name.StartsWith("System.Nullable`1[[System.DateTime")) // return a datetime object
317320
{
318-
return DateTime.Parse(response.Content, null, System.Globalization.DateTimeStyles.RoundtripKind);
321+
return DateTime.Parse(response.Content, null, System.Globalization.DateTimeStyles.RoundtripKind);
319322
}
320323

321324
if (type == typeof(String) || type.Name.StartsWith("System.Nullable")) // return primitive type
@@ -434,7 +437,7 @@ public static dynamic ConvertType(dynamic fromObject, Type toObject)
434437
/// <returns>Byte array</returns>
435438
public static byte[] ReadAsBytes(Stream inputStream)
436439
{
437-
byte[] buf = new byte[16*1024];
440+
byte[] buf = new byte[16 * 1024];
438441
using (MemoryStream ms = new MemoryStream())
439442
{
440443
int count;

src/sib_api_v3_sdk/Client/Configuration.cs

+12-9
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public class Configuration : IReadableConfiguration
5757
string.Format("Error calling {0}: {1}", methodName, response.Content),
5858
response.Content);
5959
}
60-
60+
6161
return null;
6262
};
6363

@@ -110,7 +110,7 @@ static Configuration()
110110
/// </summary>
111111
public Configuration()
112112
{
113-
UserAgent = "sendinblue_clientAPI/v#/c#";
113+
UserAgent = "sendinblue_clientAPI/v" + Version + "/c#";
114114
BasePath = "https://api.sendinblue.com/v3";
115115
DefaultHeader = new ConcurrentDictionary<string, string>();
116116
ApiKey = new ConcurrentDictionary<string, string>();
@@ -183,7 +183,7 @@ public Configuration(
183183
string tempFolderPath = null,
184184
string dateTimeFormat = null,
185185
int timeout = 100000,
186-
string userAgent = "sendinblue_clientAPI/v#/c#"
186+
string userAgent = "sendinblue_clientAPI/v" + Version + "/c#"
187187
// ReSharper restore UnusedParameter.Local
188188
)
189189
{
@@ -223,12 +223,15 @@ public virtual ApiClient ApiClient
223223
/// <summary>
224224
/// Gets or sets the base path for API access.
225225
/// </summary>
226-
public virtual string BasePath {
226+
public virtual string BasePath
227+
{
227228
get { return _basePath; }
228-
set {
229+
set
230+
{
229231
_basePath = value;
230232
// pass-through to ApiClient if it's set.
231-
if(_apiClient != null) {
233+
if (_apiClient != null)
234+
{
232235
_apiClient.RestClient.BaseUrl = new Uri(_basePath);
233236
}
234237
}
@@ -274,9 +277,9 @@ public virtual int Timeout
274277
public string GetApiKeyWithPrefix(string apiKeyIdentifier)
275278
{
276279
var apiKeyValue = "";
277-
ApiKey.TryGetValue (apiKeyIdentifier, out apiKeyValue);
280+
ApiKey.TryGetValue(apiKeyIdentifier, out apiKeyValue);
278281
var apiKeyPrefix = "";
279-
if (ApiKeyPrefix.TryGetValue (apiKeyIdentifier, out apiKeyPrefix))
282+
if (ApiKeyPrefix.TryGetValue(apiKeyIdentifier, out apiKeyPrefix))
280283
return apiKeyPrefix + " " + apiKeyValue;
281284
else
282285
return apiKeyValue;
@@ -416,7 +419,7 @@ public static String ToDebugReport()
416419
String report = "C# SDK (sib_api_v3_sdk) Debug Report:\n";
417420
report += " OS: " + System.Runtime.InteropServices.RuntimeInformation.OSDescription + "\n";
418421
report += " Version of the API: 3.0.0\n";
419-
report += " SDK Package Version: 3.1.0\n";
422+
report += " SDK Package Version: " + Version + "\n";
420423

421424
return report;
422425
}

src/sib_api_v3_sdk/Client/IReadableConfiguration.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public interface IReadableConfiguration
7070
/// Gets the user agent.
7171
/// </summary>
7272
/// <value>User agent.</value>
73-
string UserAgent { get; }
73+
string UserAgent { get; set; }
7474

7575
/// <summary>
7676
/// Gets the username.

0 commit comments

Comments
 (0)