Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to provide an idempotency-key in create ticket #596

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 24 additions & 13 deletions src/ZendeskApi_v2/Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Newtonsoft.Json;
using ZendeskApi_v2.Extensions;
using ZendeskApi_v2.Models.Shared;
using System.Collections.Specialized;

namespace ZendeskApi_v2
{
Expand All @@ -28,14 +29,14 @@ public interface ICore
{
#if SYNC
T GetByPageUrl<T>(string pageUrl, int perPage = 100);
T RunRequest<T>(string resource, string requestMethod, object body = null, int? timeout = null, Dictionary<string, object> formParameters = null);
RequestResult RunRequest(string resource, string requestMethod, object body = null, int? timeout = null, Dictionary<string, object> formParameters = null);
T RunRequest<T>(string resource, string requestMethod, object body = null, int? timeout = null, Dictionary<string, object> formParameters = null, NameValueCollection headers = null);
RequestResult RunRequest(string resource, string requestMethod, object body = null, int? timeout = null, Dictionary<string, object> formParameters = null, NameValueCollection headers = null);
#endif

#if ASYNC
Task<T> GetByPageUrlAsync<T>(string pageUrl, int perPage = 100);
Task<T> RunRequestAsync<T>(string resource, string requestMethod, object body = null, int? timeout = null, Dictionary<string, object> formParameters = null);
Task<RequestResult> RunRequestAsync(string resource, string requestMethod, object body = null, int? timeout = null, Dictionary<string, object> formParameters = null);
Task<T> RunRequestAsync<T>(string resource, string requestMethod, object body = null, int? timeout = null, Dictionary<string, object> formParameters = null, NameValueCollection headers = null);
Task<RequestResult> RunRequestAsync(string resource, string requestMethod, object body = null, int? timeout = null, Dictionary<string, object> formParameters = null, NameValueCollection headers = null);
#endif
}

Expand Down Expand Up @@ -116,14 +117,14 @@ public T GetByPageUrl<T>(string pageUrl, int perPage = 100)
return RunRequest<T>(resource, RequestMethod.Get);
}

public T RunRequest<T>(string resource, string requestMethod, object body = null, int? timeout = null, Dictionary<string, object> formParameters = null)
public T RunRequest<T>(string resource, string requestMethod, object body = null, int? timeout = null, Dictionary<string, object> formParameters = null, NameValueCollection headers = null)
{
var response = RunRequest(resource, requestMethod, body, timeout, formParameters);
var response = RunRequest(resource, requestMethod, body, timeout, formParameters, headers);
var obj = JsonConvert.DeserializeObject<T>(response.Content, jsonSettings);
return obj;
}

public RequestResult RunRequest(string resource, string requestMethod, object body = null, int? timeout = null, Dictionary<string, object> formParameters = null)
public RequestResult RunRequest(string resource, string requestMethod, object body = null, int? timeout = null, Dictionary<string, object> formParameters = null, NameValueCollection headers = null)
{
try
{
Expand All @@ -137,6 +138,11 @@ public RequestResult RunRequest(string resource, string requestMethod, object bo
}

req.Headers["Authorization"] = GetPasswordOrTokenAuthHeader();
if (headers != null)
{
req.Headers.Add(headers);
}

req.PreAuthenticate = true;
AddCustomHeaders(req);

Expand Down Expand Up @@ -297,9 +303,9 @@ protected T GenericDelete<T>(string resource)
return res;
}

protected T GenericPost<T>(string resource, object body = null)
protected T GenericPost<T>(string resource, object body = null, NameValueCollection headers = null)
{
var res = RunRequest<T>(resource, RequestMethod.Post, body);
var res = RunRequest<T>(resource, RequestMethod.Post, body, headers: headers);
return res;
}

Expand Down Expand Up @@ -373,14 +379,14 @@ public async Task<T> GetByPageUrlAsync<T>(string pageUrl, int perPage = 100)
return await RunRequestAsync<T>(resource, RequestMethod.Get);
}

public async Task<T> RunRequestAsync<T>(string resource, string requestMethod, object body = null, int? timeout = null, Dictionary<string, object> formParameters = null)
public async Task<T> RunRequestAsync<T>(string resource, string requestMethod, object body = null, int? timeout = null, Dictionary<string, object> formParameters = null, NameValueCollection headers = null)
{
var response = await RunRequestAsync(resource, requestMethod, body, timeout, formParameters);
var obj = Task.Factory.StartNew(() => JsonConvert.DeserializeObject<T>(response.Content, jsonSettings));
return await obj;
}

public async Task<RequestResult> RunRequestAsync(string resource, string requestMethod, object body = null, int? timeout = null, Dictionary<string, object> formParameters = null)
public async Task<RequestResult> RunRequestAsync(string resource, string requestMethod, object body = null, int? timeout = null, Dictionary<string, object> formParameters = null, NameValueCollection headers = null)
{
var requestUrl = ZendeskUrl + resource;
try
Expand All @@ -389,6 +395,11 @@ public async Task<RequestResult> RunRequestAsync(string resource, string request
req.ContentType = "application/json";

req.Headers["Authorization"] = GetPasswordOrTokenAuthHeader();
if (headers != null)
{
req.Headers.Add(headers);
}

req.Method = requestMethod; //GET POST PUT DELETE
req.Accept = "application/json, application/xml, text/json, text/x-json, text/javascript, text/xml";
AddCustomHeaders(req);
Expand Down Expand Up @@ -546,9 +557,9 @@ protected async Task<T> GenericDeleteAsync<T>(string resource)
return await res;
}

protected async Task<T> GenericPostAsync<T>(string resource, object body = null)
protected async Task<T> GenericPostAsync<T>(string resource, object body = null, NameValueCollection headers = null)
{
return await RunRequestAsync<T>(resource, RequestMethod.Post, body);
return await RunRequestAsync<T>(resource, RequestMethod.Post, body, headers: headers);
}

protected async Task<T> GenericPostFormAsync<T>(string resource, object body = null, Dictionary<string, object> formParameters = null)
Expand Down
49 changes: 43 additions & 6 deletions src/ZendeskApi_v2/Requests/Tickets.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Net;

#if ASYNC
Expand Down Expand Up @@ -77,7 +78,16 @@ public interface ITickets : ICore

GroupTicketResponse GetMultipleTickets(IEnumerable<long> ids, TicketSideLoadOptionsEnum sideLoadOptions = TicketSideLoadOptionsEnum.None);

IndividualTicketResponse CreateTicket(Ticket ticket);
/// <summary>
/// Creates a ticket.
/// If a idempotency-key is provided and the same request using the same body and idempotency key is repeated, another ticket is not created.
/// Instead, you'll get the same response as before that is cached under the idempotency key
/// </summary>
/// <param name="ticket"></param>
/// <param name="idempotencyKey">The Ticketing API lets you specify an idempotency key that allows you to retry a ticket creation request without the
/// risk of creating duplicate records.</param>
/// <returns></returns>
IndividualTicketResponse CreateTicket(Ticket ticket, string idempotencyKey = null);

JobStatusResponse CreateManyTickets(IEnumerable<Ticket> tickets);

Expand Down Expand Up @@ -188,7 +198,16 @@ public interface ITickets : ICore

Task<GroupTicketResponse> GetMultipleTicketsAsync(IEnumerable<long> ids, TicketSideLoadOptionsEnum sideLoadOptions = TicketSideLoadOptionsEnum.None);

Task<IndividualTicketResponse> CreateTicketAsync(Ticket ticket);
/// <summary>
/// Creates a ticket.
/// If a idempotency-key is provided and the same request using the same body and idempotency key is repeated, another ticket is not created.
/// Instead, you'll get the same response as before that is cached under the idempotency key
/// </summary>
/// <param name="ticket"></param>
/// <param name="idempotencyKey">The Ticketing API lets you specify an idempotency key that allows you to retry a ticket creation request without the
/// risk of creating duplicate records.</param>
/// <returns></returns>
Task<IndividualTicketResponse> CreateTicketAsync(Ticket ticket, string idempotencyKey = null);

Task<JobStatusResponse> CreateManyTicketsAsync(IEnumerable<Ticket> tickets);

Expand Down Expand Up @@ -418,9 +437,18 @@ public GroupTicketResponse GetMultipleTickets(IEnumerable<long> ids, TicketSideL
return GenericGet<GroupTicketResponse>(resource);
}

public IndividualTicketResponse CreateTicket(Ticket ticket)
public IndividualTicketResponse CreateTicket(Ticket ticket, string idempotencyKey = null)
{
return GenericPost<IndividualTicketResponse>($"{_tickets}.json", new { ticket });
NameValueCollection headers = null;
if (!string.IsNullOrEmpty(idempotencyKey))
{
headers = new NameValueCollection
{
{ "Idempotency-Key", idempotencyKey }
};
}

return GenericPost<IndividualTicketResponse>($"{_tickets}.json", new { ticket }, headers: headers);
}

public JobStatusResponse CreateManyTickets(IEnumerable<Ticket> tickets)
Expand Down Expand Up @@ -798,9 +826,18 @@ public async Task<GroupTicketResponse> GetMultipleTicketsAsync(IEnumerable<long>
return await GenericGetAsync<GroupTicketResponse>(GetResourceStringWithSideLoadOptionsParam($"{_tickets}/show_many.json?ids={ids.ToCsv()}", sideLoadOptions));
}

public async Task<IndividualTicketResponse> CreateTicketAsync(Ticket ticket)
public async Task<IndividualTicketResponse> CreateTicketAsync(Ticket ticket, string idempotencyKey = null)
{
return await GenericPostAsync<IndividualTicketResponse>($"{_tickets}.json", new { ticket });
NameValueCollection headers = null;
if (!string.IsNullOrEmpty(idempotencyKey))
{
headers = new NameValueCollection
{
{ "Idempotency-Key", idempotencyKey }
};
}

return await GenericPostAsync<IndividualTicketResponse>($"{_tickets}.json", new { ticket }, headers: headers);
}

public async Task<JobStatusResponse> CreateManyTicketsAsync(IEnumerable<Ticket> tickets)
Expand Down
2 changes: 1 addition & 1 deletion src/global.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"sdk": {
"version": "6.0.408"
"version": "6.0.423"
}
}
Loading