Skip to content

Commit

Permalink
Merge pull request #14 from Moesif/add-user-agent-string
Browse files Browse the repository at this point in the history
Add: User Agent String and Debug configuration option
  • Loading branch information
dkm199 committed Mar 12, 2021
2 parents d464910 + 834708a commit 58ad0d6
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Moesif.Api.Test/ControllerTestBase.cs
Expand Up @@ -44,7 +44,7 @@ public static MoesifApiClient GetClient()
{
if (client == null)
{
client = new MoesifApiClient("Your Moesif Application Id");
client = new MoesifApiClient("Your Moesif Application Id", null, false);
}
return client;
}
Expand Down
4 changes: 4 additions & 0 deletions Moesif.Api/Configuration.cs
Expand Up @@ -16,5 +16,9 @@ public partial class Configuration
//TODO: Replace the ApplicationId with your own
public static string ApplicationId = "SET_ME";

public static string UserAgentString = null;

public static bool Debug = false;

}
}
23 changes: 23 additions & 0 deletions Moesif.Api/Controllers/ApiController.cs
Expand Up @@ -223,6 +223,24 @@ public void CreateEventsBatch(List<EventModel> body)
StringBuilder _queryBuilder = new StringBuilder(_baseUri);
_queryBuilder.Append("/v1/events/batch");

if (Configuration.Debug)
{
if (body == null)
{
Console.WriteLine("Body before serialization is null");
} else
{
foreach (EventModel eventData in body)
{
if (eventData == null)
{
Console.WriteLine("Body before serialization contains null elements");
break;
}
}
}
}


//validate and preprocess url
string _queryUrl = ApiHelper.CleanUrl(_queryBuilder);
Expand All @@ -237,6 +255,11 @@ public void CreateEventsBatch(List<EventModel> body)
//append body params
var _body = ApiHelper.JsonSerialize(body);

if (Configuration.Debug)
{
Console.WriteLine("Serialized body before sending - " + _body);
}

//prepare the API call request to fetch the response
HttpRequest _request = ClientInstance.PostBody(_queryUrl, _headers, _body);

Expand Down
10 changes: 9 additions & 1 deletion Moesif.Api/Http/Client/UnirestClient.cs
Expand Up @@ -4,6 +4,7 @@
using System.IO.Compression;
using System.Linq;
using System.Threading.Tasks;
using Moesif.Api;
using Moesif.Api.Http.Request;
using Moesif.Api.Http.Response;
using unirest_net.http;
Expand Down Expand Up @@ -225,7 +226,14 @@ private static UniHttpRequest ConvertRequest(HttpRequest request)
//set request headers
Dictionary<string, Object> headers = request.Headers.ToDictionary(item=> item.Key,item=> (Object) item.Value);
uniRequest.headers(headers);
uniRequest.header("user-agent", "moesifapi-csharp/" + Version);
if (Configuration.UserAgentString != null)
{
uniRequest.header("user-agent", Configuration.UserAgentString);
}
else
{
uniRequest.header("user-agent", "moesifapi-csharp/" + Version);
}

//Set basic auth credentials if any
if (!string.IsNullOrWhiteSpace(request.Username))
Expand Down
2 changes: 1 addition & 1 deletion Moesif.Api/Moesif.Api.nuspec
Expand Up @@ -2,7 +2,7 @@
<package >
<metadata>
<id>Moesif.Api</id>
<version>2.0.2</version>
<version>2.0.3</version>
<title>MoesifApi</title>
<authors>Moesif</authors>
<owners>Moesif</owners>
Expand Down
4 changes: 3 additions & 1 deletion Moesif.Api/MoesifApiClient.cs
Expand Up @@ -57,9 +57,11 @@ public IHttpClient SharedHttpClient
/// <summary>
/// Client initialization constructor
/// </summary>
public MoesifApiClient(string applicationId)
public MoesifApiClient(string applicationId, string userAgentString = null, bool debug = false)
{
Configuration.ApplicationId = applicationId;
Configuration.UserAgentString = userAgentString;
Configuration.Debug = debug;
}
}
}
4 changes: 2 additions & 2 deletions Moesif.Api/Properties/AssemblyInfo.cs
Expand Up @@ -23,5 +23,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("2.0.2")]
[assembly: AssemblyFileVersion("2.0.2")]
[assembly: AssemblyVersion("2.0.3")]
[assembly: AssemblyFileVersion("2.0.3")]

0 comments on commit 58ad0d6

Please sign in to comment.