Skip to content

Commit

Permalink
Feature/sdk 5045 forms support (#760)
Browse files Browse the repository at this point in the history
  • Loading branch information
kailash-b authored Jan 8, 2025
2 parents ee4bc19 + e4fb62f commit 49c42d6
Show file tree
Hide file tree
Showing 26 changed files with 581 additions and 8 deletions.
96 changes: 96 additions & 0 deletions src/Auth0.ManagementApi/Clients/FormsClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;

using Auth0.ManagementApi.Models.Forms;
using Auth0.ManagementApi.Paging;

using Newtonsoft.Json;

namespace Auth0.ManagementApi.Clients
{
public class FormsClient : BaseClient, IFormsClient
{
private readonly JsonConverter[] formsConverters = { new PagedListConverter<FormBase>("forms") };

/// <summary>
/// Initializes a new instance of <see cref="FormsClient"/>.
/// </summary>
/// <param name="connection"><see cref="IManagementConnection"/> used to make all API calls.</param>
/// <param name="baseUri"><see cref="Uri"/> of the endpoint to use in making API calls.</param>
/// <param name="defaultHeaders">Dictionary containing default headers included with every request this client makes.</param>
public FormsClient(IManagementConnection connection, Uri baseUri, IDictionary<string, string> defaultHeaders) : base(connection, baseUri, defaultHeaders)
{

}

/// <inheritdoc />
public Task<IPagedList<FormBase>> GetAllAsync(FormsGetRequest request, CancellationToken cancellationToken = default)
{
if (request == null)
throw new ArgumentNullException(nameof(request));

var queryStrings = new Dictionary<string, string>();

if (request.Hydrate != null && request.Hydrate.Any())
{
var hydrateValues = string.Join(",", request.Hydrate.Select( x => x.ToString().ToLower()));
queryStrings["hydrate"] = hydrateValues;
}

if (request.PaginationInfo != null)
{
queryStrings["page"] = request.PaginationInfo.PageNo.ToString();
queryStrings["per_page"] = request.PaginationInfo.PerPage.ToString();
queryStrings["include_totals"] = request.PaginationInfo.IncludeTotals.ToString().ToLower();
}

return Connection.GetAsync<IPagedList<FormBase>>(BuildUri("forms", queryStrings), DefaultHeaders, formsConverters, cancellationToken);
}

/// <inheritdoc />
public Task<Form> GetAsync(FormsGetRequest request, CancellationToken cancellationToken = default)
{
if (request == null)
throw new ArgumentNullException(nameof(request));
var queryStrings = new Dictionary<string, string>();

if (request.Hydrate != null && request.Hydrate.Any())
{
var hydrateValues = string.Join(",", request.Hydrate.Select( x => x.ToString().ToLower()));
queryStrings["hydrate"] = hydrateValues;
}

return Connection.GetAsync<Form>(BuildUri($"forms/{EncodePath(request.Id)}", queryStrings), DefaultHeaders, null, cancellationToken);
}

/// <inheritdoc />
public Task<Form> CreateAsync(FormCreateRequest request, CancellationToken cancellationToken = default)
{
return Connection.SendAsync<Form>(HttpMethod.Post, BuildUri("forms"), request, DefaultHeaders, cancellationToken: cancellationToken);
}

/// <inheritdoc />
public Task<Form> UpdateAsync(string id, FormUpdateRequest request, CancellationToken cancellationToken = default)
{
if (string.IsNullOrEmpty(id))
{
throw new ArgumentNullException(nameof(id));
}
return Connection.SendAsync<Form>(new HttpMethod("PATCH"), BuildUri($"forms/{EncodePath(id)}"), request, DefaultHeaders, cancellationToken: cancellationToken);
}

/// <inheritdoc />
public Task DeleteAsync(string id, CancellationToken cancellationToken = default)
{
if (string.IsNullOrEmpty(id))
{
throw new ArgumentNullException(nameof(id));
}
return Connection.SendAsync<object>(HttpMethod.Delete, BuildUri($"forms/{EncodePath(id)}"), null, DefaultHeaders, cancellationToken: cancellationToken);
}
}
}
53 changes: 53 additions & 0 deletions src/Auth0.ManagementApi/Clients/IFormsClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Auth0.ManagementApi.Models.Forms;
using Auth0.ManagementApi.Paging;

namespace Auth0.ManagementApi.Clients
{
public interface IFormsClient
{
/// <summary>
/// Get all Forms
/// </summary>
/// <param name="request"><see cref="FormsGetRequest"/></param>
/// <param name="cancellationToken"><see cref="CancellationToken"/></param>
/// <returns>All available <see cref="Form" />s.</returns>
Task<IPagedList<FormBase>> GetAllAsync(FormsGetRequest request, CancellationToken cancellationToken = default);

/// <summary>
/// Get A form.
/// </summary>
/// <param name="request"><see cref="FormsGetRequest"/></param>
/// <param name="cancellationToken"><see cref="CancellationToken"/></param>
/// <returns>All available <see cref="Form" />s.</returns>
Task<Form> GetAsync(FormsGetRequest request, CancellationToken cancellationToken = default);

/// <summary>
/// Create a form.
/// </summary>
/// <param name="request"><see cref="FormCreateRequest"/></param>
/// <param name="cancellationToken"><see cref="CancellationToken"/></param>
/// <returns></returns>
Task<Form> CreateAsync(FormCreateRequest request, CancellationToken cancellationToken = default);

/// <summary>
/// Update a form.
/// </summary>
/// <param name="id">ID of the form to be updated</param>
/// <param name="request"><see cref="FormUpdateRequest"/></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task<Form> UpdateAsync(string id, FormUpdateRequest request, CancellationToken cancellationToken = default);

/// <summary>
/// Delete a form.
/// </summary>
/// <param name="id">ID of the form to be deleted</param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task DeleteAsync(string id, CancellationToken cancellationToken = default);
}
}

5 changes: 5 additions & 0 deletions src/Auth0.ManagementApi/IManagementApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,11 @@ public interface IManagementApiClient : IDisposable
/// Contains all the methods to call the /self-service-profile endpoints.
/// </summary>
ISelfServiceProfilesClient SelfServiceProfilesClient { get; }

/// <summary>
/// Contains all the methods to call the /forms endpoints.
/// </summary>
IFormsClient FormsClient { get; }

/// <summary>
/// Update the Access Token used with every request.
Expand Down
4 changes: 4 additions & 0 deletions src/Auth0.ManagementApi/ManagementApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,9 @@ public class ManagementApiClient : IManagementApiClient

/// <inheritdoc cref="Auth0.ManagementApi.IManagementApiClient.SelfServiceProfilesClient"/>
public ISelfServiceProfilesClient SelfServiceProfilesClient { get; }

/// <inheritdoc cref="Auth0.ManagementApi.IManagementApiClient.FormsClient"/>
public IFormsClient FormsClient { get; }

private Dictionary<string, string> DefaultHeaders { get; set; }

Expand Down Expand Up @@ -226,6 +229,7 @@ public ManagementApiClient(string token, Uri baseUri, IManagementConnection mana
RefreshTokens = new RefreshTokenClient(managementConnection, baseUri, DefaultHeaders);
Sessions = new SessionsClient(managementConnection, baseUri, DefaultHeaders);
SelfServiceProfilesClient = new SelfServiceProfilesClient(managementConnection, baseUri, DefaultHeaders);
FormsClient = new FormsClient(managementConnection, baseUri, DefaultHeaders);
}

/// <summary>
Expand Down
10 changes: 10 additions & 0 deletions src/Auth0.ManagementApi/Models/Forms/AfterSubmit.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Newtonsoft.Json;

namespace Auth0.ManagementApi.Models.Forms
{
public class AfterSubmit
{
[JsonProperty("flow_id")]
public string FlowId { get; set; }
}
}
13 changes: 13 additions & 0 deletions src/Auth0.ManagementApi/Models/Forms/Coordinates.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Newtonsoft.Json;

namespace Auth0.ManagementApi.Models.Forms
{
public class Coordinates
{
[JsonProperty("x")]
public int X { get; set; }

[JsonProperty("y")]
public int Y { get; set; }
}
}
19 changes: 19 additions & 0 deletions src/Auth0.ManagementApi/Models/Forms/Ending.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Newtonsoft.Json;

namespace Auth0.ManagementApi.Models.Forms
{
public class Ending
{
[JsonProperty("redirection")]
public Redirection Redirection { get; set; }

[JsonProperty("after_submit")]
public AfterSubmit AfterSubmit { get; set; }

[JsonProperty("coordinates")]
public Coordinates Coordinates { get; set; }

[JsonProperty("resume_flow")]
public bool? ResumeFlow { get; set; }
}
}
33 changes: 33 additions & 0 deletions src/Auth0.ManagementApi/Models/Forms/Form.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using Newtonsoft.Json;
using Auth0.ManagementApi.Models.Forms;

namespace Auth0.ManagementApi.Models.Forms
{
/// <summary>
/// Represents a Form
/// </summary>
public class Form : FormBase
{
[JsonProperty("messages")]
public Messages Messages { get; set; }

[JsonProperty("languages")]
public Languages Languages { get; set; }

[JsonProperty("translations")]
public dynamic Translations { get; set; }

[JsonProperty("nodes")]
public Node[] Nodes { get; set; }

[JsonProperty("start")]
public Start Start { get; set; }

[JsonProperty("ending")]
public Ending Ending { get; set; }

[JsonProperty("style")]
public Style Style { get; set; }
}
}
26 changes: 26 additions & 0 deletions src/Auth0.ManagementApi/Models/Forms/FormBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using Newtonsoft.Json;

namespace Auth0.ManagementApi.Models.Forms
{
public class FormBase
{
[JsonProperty("id")]
public string Id { get; set; }

[JsonProperty("name")]
public string Name { get; set; }

[JsonProperty("created_at")]
public DateTime CreatedAt { get; set; }

[JsonProperty("updated_at")]
public DateTime UpdatedAt { get; set; }

[JsonProperty("embedded_at")]
public DateTime EmbeddedAt { get; set; }

[JsonProperty("submitted_at")]
public DateTime SubmittedAt { get; set; }
}
}
34 changes: 34 additions & 0 deletions src/Auth0.ManagementApi/Models/Forms/FormCreateRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using Newtonsoft.Json;

namespace Auth0.ManagementApi.Models.Forms
{
/// <summary>
/// Contains information required to create a new form.
/// </summary>
public class FormCreateRequest
{
[JsonProperty("name")]
public string Name { get; set; }

[JsonProperty("messages")]
public Messages Messages { get; set; }

[JsonProperty("languages")]
public Languages Languages { get; set; }

[JsonProperty("translations")]
public dynamic Translations { get; set; }

[JsonProperty("nodes")]
public Node[] Nodes { get; set; }

[JsonProperty("start")]
public Start Start { get; set; }

[JsonProperty("ending")]
public Ending Ending { get; set; }

[JsonProperty("style")]
public Style Style { get; set; }
}
}
7 changes: 7 additions & 0 deletions src/Auth0.ManagementApi/Models/Forms/FormUpdateRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Auth0.ManagementApi.Models.Forms
{
public class FormUpdateRequest : FormCreateRequest
{

}
}
25 changes: 25 additions & 0 deletions src/Auth0.ManagementApi/Models/Forms/FormsGetRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Collections.Specialized;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

using Auth0.ManagementApi.Paging;

namespace Auth0.ManagementApi.Models.Forms
{
public class FormsGetRequest
{
public PaginationInfo PaginationInfo { get; set; }

/// <summary>
/// Hydration parameter
/// </summary>
[JsonConverter(typeof(StringEnumConverter))]
public Hydrate[] Hydrate { get; set; }

/// <summary>
/// Form Identifier
/// </summary>
public string Id { get; set; }
}
}
13 changes: 13 additions & 0 deletions src/Auth0.ManagementApi/Models/Forms/HiddenFields.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Newtonsoft.Json;

namespace Auth0.ManagementApi.Models.Forms
{
public class HiddenFields
{
[JsonProperty("key")]
public string Key { get; set; }

[JsonProperty("value")]
public string Value { get; set; }
}
}
13 changes: 13 additions & 0 deletions src/Auth0.ManagementApi/Models/Forms/Hydrate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.Runtime.Serialization;

namespace Auth0.ManagementApi.Models.Forms
{
public enum Hydrate
{
[EnumMember(Value = "flow_count")]
FLOW_COUNT,

[EnumMember(Value = "links")]
LINKS
}
}
Loading

0 comments on commit 49c42d6

Please sign in to comment.