Skip to content

Commit

Permalink
Add Custom Header (#587)
Browse files Browse the repository at this point in the history
* Add Custom Header

* Formatting fix

* Format Fix #2

* Add Delay to unit test in attempt to fix test

* Change Custom headers to Dictionary<string,string>
  • Loading branch information
jcrawfor74 authored Nov 29, 2023
1 parent 1a9ae09 commit 88df80a
Show file tree
Hide file tree
Showing 38 changed files with 207 additions and 133 deletions.
28 changes: 25 additions & 3 deletions src/ZendeskApi_v2/Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public class Core : ICore
protected string Password;
protected string ZendeskUrl;
protected string ApiToken;
protected Dictionary<string, string> CustomHeaders;

private readonly JsonSerializerSettings jsonSettings = new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
Expand All @@ -63,7 +65,7 @@ public class Core : ICore
/// <param name="zendeskApiUrl"></param>
/// <param name="p_OAuthToken"></param>
public Core(string zendeskApiUrl, string p_OAuthToken) :
this(zendeskApiUrl, null, null, null, p_OAuthToken)
this(zendeskApiUrl, null, null, null, p_OAuthToken, null)
{
}

Expand All @@ -73,7 +75,7 @@ public Core(string zendeskApiUrl, string p_OAuthToken) :
/// <param name="zendeskApiUrl"></param>
/// <param name="p_OAuthToken"></param>
public Core(string zendeskApiUrl, string user, string password, string apiToken) :
this(zendeskApiUrl, user, password, apiToken, null)
this(zendeskApiUrl, user, password, apiToken, null, null)
{
}

Expand All @@ -84,7 +86,8 @@ public Core(string zendeskApiUrl, string user, string password, string apiToken)
/// <param name="user"></param>
/// <param name="password">LEAVE BLANK IF USING TOKEN</param>
/// <param name="apiToken">Optional Param which is used if specified instead of the password</param>
public Core(string zendeskApiUrl, string user, string password, string apiToken, string p_OAuthToken)
/// <param name="customHeaders">Optional Dictionary of custom headers that adds these headers to the request</param>
public Core(string zendeskApiUrl, string user, string password, string apiToken, string p_OAuthToken, Dictionary<string,string> customHeaders)
{
User = user;
Password = password;
Expand All @@ -96,6 +99,7 @@ public Core(string zendeskApiUrl, string user, string password, string apiToken,
ZendeskUrl = zendeskApiUrl;
ApiToken = apiToken;
OAuthToken = p_OAuthToken;
CustomHeaders = customHeaders;
}

#if SYNC
Expand Down Expand Up @@ -134,6 +138,7 @@ public RequestResult RunRequest(string resource, string requestMethod, object bo

req.Headers["Authorization"] = GetPasswordOrTokenAuthHeader();
req.PreAuthenticate = true;
AddCustomHeaders(req);

req.Method = requestMethod; //GET POST PUT DELETE
req.Accept = "application/json, application/xml, text/json, text/x-json, text/javascript, text/xml";
Expand Down Expand Up @@ -386,6 +391,7 @@ public async Task<RequestResult> RunRequestAsync(string resource, string request
req.Headers["Authorization"] = GetPasswordOrTokenAuthHeader();
req.Method = requestMethod; //GET POST PUT DELETE
req.Accept = "application/json, application/xml, text/json, text/x-json, text/javascript, text/xml";
AddCustomHeaders(req);

byte[] data = null;

Expand Down Expand Up @@ -620,5 +626,21 @@ private WebException GetWebException(string resource, object body, WebException

return wException;
}

/// <summary>
/// If the CustomHeaders Dictionary contains records then add them to the headers
/// of the passed in request
/// </summary>
/// <param name="request"></param>
private void AddCustomHeaders(HttpWebRequest request)
{
if (CustomHeaders != null)
{
foreach (var key in CustomHeaders.Keys)
{
request.Headers.Add(key, CustomHeaders[key]);
}
}
}
}
}
6 changes: 5 additions & 1 deletion src/ZendeskApi_v2/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace ZendeskApi_v2.Extensions
using ZendeskApi_v2.Models.Shared;

namespace ZendeskApi_v2.Extensions
{
internal static class StringExtensions
{
Expand All @@ -22,5 +24,7 @@ internal static bool IsNullOrWhiteSpace(this string value)

return true;
}

internal static bool IsNotNullOrWhiteSpace(this string value) => !IsNullOrWhiteSpace(value);
}
}
26 changes: 14 additions & 12 deletions src/ZendeskApi_v2/HelpCenterApi.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using ZendeskApi_v2.Requests.HelpCenter;
using System.Collections.Generic;
using ZendeskApi_v2.Requests.HelpCenter;

namespace ZendeskApi_v2.HelpCenter
{
Expand All @@ -25,19 +26,20 @@ public HelpCenterApi(
string password,
string apiToken,
string locale,
string p_OAuthToken)
string p_OAuthToken,
Dictionary<string, string> customHeaders)
{
Categories = new Categories(yourZendeskUrl, user, password, apiToken, locale, p_OAuthToken);
Sections = new Sections(yourZendeskUrl, user, password, apiToken, locale, p_OAuthToken);
Articles = new Articles(yourZendeskUrl, user, password, apiToken, locale, p_OAuthToken);
Translations = new Translations(yourZendeskUrl, user, password, apiToken, p_OAuthToken);
Votes = new Votes(yourZendeskUrl, user, password, apiToken, p_OAuthToken);
Comments = new Comments(yourZendeskUrl, user, password, apiToken, p_OAuthToken);
UserSegments = new UserSegments(yourZendeskUrl, user, password, apiToken, p_OAuthToken);
Topics = new Topics(yourZendeskUrl, user, password, apiToken, p_OAuthToken);
Posts = new Posts(yourZendeskUrl, user, password, apiToken, p_OAuthToken);
Categories = new Categories(yourZendeskUrl, user, password, apiToken, locale, p_OAuthToken, customHeaders);
Sections = new Sections(yourZendeskUrl, user, password, apiToken, locale, p_OAuthToken, customHeaders);
Articles = new Articles(yourZendeskUrl, user, password, apiToken, locale, p_OAuthToken, customHeaders);
Translations = new Translations(yourZendeskUrl, user, password, apiToken, p_OAuthToken, customHeaders);
Votes = new Votes(yourZendeskUrl, user, password, apiToken, p_OAuthToken, customHeaders);
Comments = new Comments(yourZendeskUrl, user, password, apiToken, p_OAuthToken, customHeaders);
UserSegments = new UserSegments(yourZendeskUrl, user, password, apiToken, p_OAuthToken, customHeaders);
Topics = new Topics(yourZendeskUrl, user, password, apiToken, p_OAuthToken, customHeaders);
Posts = new Posts(yourZendeskUrl, user, password, apiToken, p_OAuthToken, customHeaders);
Locale = locale;
ArticleAttachments = new ArticleAttachments(yourZendeskUrl, user, password, apiToken, locale, p_OAuthToken);
ArticleAttachments = new ArticleAttachments(yourZendeskUrl, user, password, apiToken, locale, p_OAuthToken, customHeaders);
}

public ICategories Categories { get; }
Expand Down
17 changes: 9 additions & 8 deletions src/ZendeskApi_v2/Requests/AccountsAndActivity.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#if ASYNC
#if ASYNC
using System.Collections.Generic;
using System.Threading.Tasks;
#endif
using ZendeskApi_v2.Models.AccountsAndActivities;
Expand All @@ -15,16 +16,16 @@ public interface IAccountsAndActivity : ICore

#if ASYNC
Task<SettingsResponse> GetSettingsAsync();
Task<GroupActivityResponse> GetActivitiesAync();
Task<IndividualActivityResponse> GetActivityByIdAync(long activityId);
Task<GroupActivityResponse> GetActivitiesAsync();
Task<IndividualActivityResponse> GetActivityByIdAsync(long activityId);
#endif
}

public class AccountsAndActivity : Core, IAccountsAndActivity
{

public AccountsAndActivity(string yourZendeskUrl, string user, string password, string apiToken, string p_OAuthToken)
: base(yourZendeskUrl, user, password, apiToken, p_OAuthToken)
public AccountsAndActivity(string yourZendeskUrl, string user, string password, string apiToken, string p_OAuthToken, Dictionary<string,string> customHeaders)
: base(yourZendeskUrl, user, password, apiToken, p_OAuthToken, customHeaders)
{
}
#if SYNC
Expand All @@ -49,15 +50,15 @@ public async Task<SettingsResponse> GetSettingsAsync()
{
return await GenericGetAsync<SettingsResponse>("account/settings.json");
}
public async Task<GroupActivityResponse> GetActivitiesAync()
public async Task<GroupActivityResponse> GetActivitiesAsync()
{
return await GenericGetAsync<GroupActivityResponse>("activities.json");
}

public async Task<IndividualActivityResponse> GetActivityByIdAync(long activityId)
public async Task<IndividualActivityResponse> GetActivityByIdAsync(long activityId)
{
return await GenericGetAsync<IndividualActivityResponse>($"activities/{activityId}.json");
}
#endif
}
}
}
4 changes: 2 additions & 2 deletions src/ZendeskApi_v2/Requests/Attachments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ public interface IAttachments : ICore

public class Attachments : Core, IAttachments
{
public Attachments(string yourZendeskUrl, string user, string password, string apiToken, string p_OAuthToken)
: base(yourZendeskUrl, user, password, apiToken, p_OAuthToken)
public Attachments(string yourZendeskUrl, string user, string password, string apiToken, string p_OAuthToken, Dictionary<string,string> customHeaders)
: base(yourZendeskUrl, user, password, apiToken, p_OAuthToken, customHeaders)
{ }
#if SYNC

Expand Down
4 changes: 2 additions & 2 deletions src/ZendeskApi_v2/Requests/Automations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public interface IAutomations : ICore

public class Automations : Core, IAutomations
{
public Automations(string yourZendeskUrl, string user, string password, string apiToken, string p_OAuthToken)
: base(yourZendeskUrl, user, password, apiToken, p_OAuthToken)
public Automations(string yourZendeskUrl, string user, string password, string apiToken, string p_OAuthToken, Dictionary<string,string> customHeaders)
: base(yourZendeskUrl, user, password, apiToken, p_OAuthToken, customHeaders)
{
}

Expand Down
8 changes: 4 additions & 4 deletions src/ZendeskApi_v2/Requests/Brands.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if ASYNC
#if ASYNC
using System.Threading.Tasks;
#endif
using System.Collections.Generic;
Expand Down Expand Up @@ -28,8 +28,8 @@ public interface IBrands : ICore

public class Brands : Core, IBrands
{
public Brands(string yourZendeskUrl, string user, string password, string apiToken, string p_OAuthToken)
: base(yourZendeskUrl, user, password, apiToken, p_OAuthToken)
public Brands(string yourZendeskUrl, string user, string password, string apiToken, string p_OAuthToken, Dictionary<string,string> customHeaders)
: base(yourZendeskUrl, user, password, apiToken, p_OAuthToken, customHeaders)
{
}

Expand Down Expand Up @@ -91,4 +91,4 @@ public async Task<bool> DeleteBrandAsync(long id)
}
#endif
}
}
}
9 changes: 5 additions & 4 deletions src/ZendeskApi_v2/Requests/CustomAgentRoles.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#if ASYNC
#if ASYNC
using System.Collections.Generic;
using System.Threading.Tasks;
#endif
using ZendeskApi_v2.Models.CustomRoles;
Expand All @@ -18,8 +19,8 @@ public interface ICustomAgentRoles : ICore

public class CustomAgentRoles : Core, ICustomAgentRoles
{
public CustomAgentRoles(string yourZendeskUrl, string user, string password, string apiToken, string p_OAuthToken)
: base(yourZendeskUrl, user, password, apiToken, p_OAuthToken)
public CustomAgentRoles(string yourZendeskUrl, string user, string password, string apiToken, string p_OAuthToken, Dictionary<string,string> customHeaders)
: base(yourZendeskUrl, user, password, apiToken, p_OAuthToken, customHeaders)
{
}

Expand All @@ -37,4 +38,4 @@ public async Task<CustomRoles> GetCustomRolesAsync()
}
#endif
}
}
}
5 changes: 3 additions & 2 deletions src/ZendeskApi_v2/Requests/Groups.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#if ASYNC
using System.Collections.Generic;
using System.Threading.Tasks;
#endif
using ZendeskApi_v2.Models.Groups;
Expand Down Expand Up @@ -68,8 +69,8 @@ public interface IGroups : ICore

public class Groups : Core, IGroups
{
public Groups(string yourZendeskUrl, string user, string password, string apiToken, string p_OAuthToken)
: base(yourZendeskUrl, user, password, apiToken, p_OAuthToken)
public Groups(string yourZendeskUrl, string user, string password, string apiToken, string p_OAuthToken, Dictionary<string,string> customHeaders)
: base(yourZendeskUrl, user, password, apiToken, p_OAuthToken, customHeaders)
{
}

Expand Down
5 changes: 3 additions & 2 deletions src/ZendeskApi_v2/Requests/HelpCenter/ArticleAttachments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ public ArticleAttachments(
string password,
string apiToken,
string locale,
string p_OAuthToken)
: base(yourZendeskUrl, user, password, apiToken, p_OAuthToken)
string p_OAuthToken,
Dictionary<string, string> customHeaders)
: base(yourZendeskUrl, user, password, apiToken, p_OAuthToken, customHeaders)
{
_locale = locale;
}
Expand Down
4 changes: 2 additions & 2 deletions src/ZendeskApi_v2/Requests/HelpCenter/Articles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ public class Articles : Core, IArticles
{
private readonly string urlPrefix = "help_center";

public Articles(string yourZendeskUrl, string user, string password, string apiToken, string locale, string p_OAuthToken)
: base(yourZendeskUrl, user, password, apiToken, p_OAuthToken)
public Articles(string yourZendeskUrl, string user, string password, string apiToken, string locale, string p_OAuthToken, Dictionary<string,string> customHeaders)
: base(yourZendeskUrl, user, password, apiToken, p_OAuthToken, customHeaders)
{
if (!locale.IsNullOrWhiteSpace())
{
Expand Down
6 changes: 4 additions & 2 deletions src/ZendeskApi_v2/Requests/HelpCenter/Categories.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@

using ZendeskApi_v2.Models.HelpCenter.Categories;
using System.Collections.Generic;

#if ASYNC
using System.Threading.Tasks;
#endif
Expand Down Expand Up @@ -36,8 +38,8 @@ public class Categories : Core, ICategories
? "help_center/categories"
: $"help_center/{Locale}/categories";

public Categories(string yourZendeskUrl, string user, string password, string apiToken, string locale, string p_OAuthToken)
: base(yourZendeskUrl, user, password, apiToken, p_OAuthToken)
public Categories(string yourZendeskUrl, string user, string password, string apiToken, string locale, string p_OAuthToken, Dictionary<string,string> customHeaders)
: base(yourZendeskUrl, user, password, apiToken, p_OAuthToken, customHeaders)
{
Locale = locale;
}
Expand Down
5 changes: 3 additions & 2 deletions src/ZendeskApi_v2/Requests/HelpCenter/Comments.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#if ASYNC
using System.Collections.Generic;
using System.Threading.Tasks;
#endif
using ZendeskApi_v2.Models.HelpCenter.Comments;
Expand Down Expand Up @@ -40,8 +41,8 @@ public interface IComments : ICore
/// </summary>
public class Comments : Core, IComments
{
public Comments(string yourZendeskUrl, string user, string password, string apiToken, string p_OAuthToken)
: base(yourZendeskUrl, user, password, apiToken, p_OAuthToken)
public Comments(string yourZendeskUrl, string user, string password, string apiToken, string p_OAuthToken, Dictionary<string,string> customHeaders)
: base(yourZendeskUrl, user, password, apiToken, p_OAuthToken, customHeaders)
{
}

Expand Down
5 changes: 3 additions & 2 deletions src/ZendeskApi_v2/Requests/HelpCenter/Posts.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#if ASYNC
using System.Collections.Generic;
using System.Threading.Tasks;
#endif
using ZendeskApi_v2.Models.HelpCenter.Post;
Expand Down Expand Up @@ -38,8 +39,8 @@ public interface IPosts : ICore

public class Posts : Core, IPosts
{
public Posts(string yourZendeskUrl, string user, string password, string apiToken, string p_OAuthToken)
: base(yourZendeskUrl, user, password, apiToken, p_OAuthToken)
public Posts(string yourZendeskUrl, string user, string password, string apiToken, string p_OAuthToken, Dictionary<string,string> customHeaders)
: base(yourZendeskUrl, user, password, apiToken, p_OAuthToken, customHeaders)
{
}
#if SYNC
Expand Down
5 changes: 3 additions & 2 deletions src/ZendeskApi_v2/Requests/HelpCenter/Sections.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#if ASYNC
using System.Collections.Generic;
using System.Threading.Tasks;
using ZendeskApi_v2.Models.HelpCenter.Subscriptions;
#endif
Expand Down Expand Up @@ -42,8 +43,8 @@ public class Sections : Core, ISections
private readonly string _locale;
private readonly string _generalSectionsPath;

public Sections(string yourZendeskUrl, string user, string password, string apiToken, string locale, string p_OAuthToken)
: base(yourZendeskUrl, user, password, apiToken, p_OAuthToken)
public Sections(string yourZendeskUrl, string user, string password, string apiToken, string locale, string p_OAuthToken, Dictionary<string,string> customHeaders)
: base(yourZendeskUrl, user, password, apiToken, p_OAuthToken, customHeaders)
{
_locale = locale;
_generalSectionsPath = string.IsNullOrWhiteSpace(_locale) ? "help_center/sections" : $"help_center/{_locale}/sections";
Expand Down
5 changes: 3 additions & 2 deletions src/ZendeskApi_v2/Requests/HelpCenter/Topics.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#if ASYNC
using System.Collections.Generic;
using System.Threading.Tasks;
using ZendeskApi_v2.Models.HelpCenter.Subscriptions;
#endif
Expand Down Expand Up @@ -36,8 +37,8 @@ public interface ITopics : ICore

public class Topics : Core, ITopics
{
public Topics(string yourZendeskUrl, string user, string password, string apiToken, string p_OAuthToken)
: base(yourZendeskUrl, user, password, apiToken, p_OAuthToken)
public Topics(string yourZendeskUrl, string user, string password, string apiToken, string p_OAuthToken, Dictionary<string,string> customHeaders)
: base(yourZendeskUrl, user, password, apiToken, p_OAuthToken, customHeaders)
{
}
#if SYNC
Expand Down
4 changes: 2 additions & 2 deletions src/ZendeskApi_v2/Requests/HelpCenter/Translations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ public interface ITranslations : ICore

public class Translations : Core, ITranslations
{
public Translations( string yourZendeskUrl, string user, string password, string apiToken, string p_OAuthToken )
: base( yourZendeskUrl, user, password, apiToken, p_OAuthToken )
public Translations( string yourZendeskUrl, string user, string password, string apiToken, string p_OAuthToken, Dictionary<string,string> customHeaders)
: base(yourZendeskUrl, user, password, apiToken, p_OAuthToken, customHeaders)
{
}

Expand Down
Loading

0 comments on commit 88df80a

Please sign in to comment.