Skip to content

Commit

Permalink
Boilerplate attachments API support
Browse files Browse the repository at this point in the history
  • Loading branch information
Nevett committed Aug 9, 2024
1 parent 2673f54 commit b9275ca
Show file tree
Hide file tree
Showing 6 changed files with 268 additions and 0 deletions.
98 changes: 98 additions & 0 deletions src/Cronofy/Attachment.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
namespace Cronofy
{
using Newtonsoft.Json;

/// <summary>
/// Class for the deserialization of an attachment summary
/// response.
/// </summary>
public sealed class Attachment
{
/// <summary>
/// Gets or sets the attachment ID.
/// </summary>
/// <value>
/// The ID of the attachment.
/// </value>
[JsonProperty("attachment_id")]
public string AttachmentId { get; set; }

/// <summary>
/// Gets or sets the file name for the attachment.
/// </summary>
/// <value>
/// The file name for the attachment.
/// </value>
[JsonProperty("file_name")]
public string FileName { get; set; }

/// <summary>
/// Gets or sets the MIME content type for the attachment.
/// </summary>
/// <value>
/// The MIME content type for the attachment.
/// </value>
[JsonProperty("content_type")]
public string ContentType { get; set; }

/// <summary>
/// Gets or sets the MD5 hash of the attachment content.
/// </summary>
/// <value>
/// The MD5 hash of the attachment content.
/// </value>
[JsonProperty("md5")]
public string MD5 { get; set; }

/// <inheritdoc/>
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj))
{
return false;
}

if (ReferenceEquals(this, obj))
{
return true;
}

return obj is Attachment && this.Equals((Attachment)obj);
}

/// <inheritdoc/>
public override int GetHashCode()
{
return this.AttachmentId.GetHashCode();
}

/// <summary>
/// Determines whether the specified <see cref="Cronofy.Attachment"/>
/// is equal to the current <see cref="Cronofy.Attachment"/>.
/// </summary>
/// <param name="other">
/// The <see cref="Cronofy.Attachment"/> to compare with the current
/// <see cref="Cronofy.Attachment"/>.
/// </param>
/// <returns>
/// <c>true</c> if the specified <see cref="Cronofy.Attachment"/> is
/// equal to the current <see cref="Cronofy.Attachment"/>; otherwise,
/// <c>false</c>.
/// </returns>
public bool Equals(Attachment other)
{
return this.AttachmentId == other.AttachmentId && this.FileName == other.FileName
&& this.ContentType == other.ContentType && this.MD5 == other.MD5;
}

/// <inheritdoc/>
public override string ToString()
{
return string.Format(
"<{0} AttachmentId={1}, FileName={2}>",
this.GetType(),
this.AttachmentId,
this.FileName);
}
}
}
22 changes: 22 additions & 0 deletions src/Cronofy/CronofyOAuthClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,28 @@ public ElementToken GetElementToken(ElementTokenRequest elementTokenRequest)
return elementTokenResponse.ToElementToken();
}

/// <inheritdoc/>
public Attachment CreateAttachment(CreateAttachmentRequest createAttachmentRequest)
{
Preconditions.NotNull(nameof(createAttachmentRequest), createAttachmentRequest);
Preconditions.NotEmpty(nameof(createAttachmentRequest.FileName), createAttachmentRequest.FileName);
Preconditions.NotEmpty(nameof(createAttachmentRequest.ContentType), createAttachmentRequest.ContentType);
Preconditions.NotEmpty(nameof(createAttachmentRequest.Base64Content), createAttachmentRequest.Base64Content);

var request = new HttpRequest
{
Method = "POST",
Url = this.urlProvider.AttachmentsUrl,
};

request.AddOAuthAuthorization(this.clientSecret);
request.SetJsonBody(createAttachmentRequest);

var attachmentResponse = this.HttpClient.GetJsonResponse<AttachmentResponse>(request);

return attachmentResponse.ToAttachment();
}

/// <summary>
/// Builder class for authorization URLs.
/// </summary>
Expand Down
2 changes: 2 additions & 0 deletions src/Cronofy/ICronofyOAuthClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -396,5 +396,7 @@ public interface ICronofyOAuthClient
/// Thrown if <paramref name="redirectUri"/> is null or empty.
/// </exception>
IAuthorizationUrlBuilder GetEnterpriseConnectAuthorizationUrlBuilder(string redirectUri);

Attachment CreateAttachment(CreateAttachmentRequest createAttachmentRequest);

Check warning on line 400 in src/Cronofy/ICronofyOAuthClient.cs

View workflow job for this annotation

GitHub Actions / Run tests

Missing XML comment for publicly visible type or member 'ICronofyOAuthClient.CreateAttachment(CreateAttachmentRequest)'

Check warning on line 400 in src/Cronofy/ICronofyOAuthClient.cs

View workflow job for this annotation

GitHub Actions / Run tests

Elements should be documented
}
}
86 changes: 86 additions & 0 deletions src/Cronofy/Requests/CreateAttachmentRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
namespace Cronofy.Requests
{
using Newtonsoft.Json;
using System.Collections.Generic;

Check warning on line 4 in src/Cronofy/Requests/CreateAttachmentRequest.cs

View workflow job for this annotation

GitHub Actions / Run tests

Using directive for 'System.Collections.Generic' should appear before directive for 'Newtonsoft.Json'

/// <summary>
/// Class for the serialization of a create attachment request.
/// </summary>
public sealed class CreateAttachmentRequest
{
/// <summary>
/// Gets or sets the file name for the attachment.
/// </summary>
/// <value>
/// The file name for the attachment.
/// </value>
[JsonProperty("file_name")]
public string FileName { get; set; }

/// <summary>
/// Gets or sets the MIME content type for the attachment.
/// </summary>
/// <value>
/// The MIME content type for the attachment.
/// </value>
[JsonProperty("content_type")]
public string ContentType { get; set; }

/// <summary>
/// Gets or sets the Base64-encoded content of the attachment.
/// </summary>
/// <value>
/// The Base64-encoded content of the attachment.
/// </value>
[JsonProperty("base64_content")]
public string Base64Content { get; set; }

/// <summary>
/// Class for the serialization of attachment subscriptions.
/// </summary>
public sealed class RequestSubscription
{
/// <summary>
/// Gets or sets the type of the subscription.
/// </summary>
/// <value>
/// The type of the subscription.
/// </value>
[JsonProperty("type")]
public string Type { get; set; }

/// <summary>
/// Gets or sets the destination URI Cronofy will call when the subscription is triggered.
/// </summary>
/// <value>
/// The destination URI Cronofy will call when the subscription is triggered.
/// </value>
[JsonProperty("uri")]
public string Uri { get; set; }

/// <summary>
/// Gets or sets the interactions subscribed to.
/// </summary>
/// <value>
/// The interactions subscribed to.
/// </value>
[JsonProperty("interactions")]
public IEnumerable<Interaction> Interactions { get; set; }

/// <summary>
/// Class for the serialization of event subscription interactions.
/// </summary>
public sealed class Interaction
{
/// <summary>
/// Gets or sets the type of the interaction.
/// </summary>
/// <value>
/// The type of the interaction.
/// </value>
[JsonProperty("type")]
public string Type { get; set; }
}
}
}
}
48 changes: 48 additions & 0 deletions src/Cronofy/Responses/AttachmentResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
namespace Cronofy.Responses
{
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;

/// <summary>
/// Class for the deserialization of an availability response.
/// </summary>
internal sealed class AttachmentResponse
{
/// <summary>
/// Gets or sets the available periods of the response.
/// </summary>
/// <value>
/// The available periods of the response.
/// </value>
[JsonProperty("attachment")]
public AttachmentSummary Attachment { get; set; }

internal sealed class AttachmentSummary
{
[JsonProperty("attachment_id")]
public string AttachmentId { get; set; }

[JsonProperty("file_name")]
public string FileName { get; set; }

[JsonProperty("content_type")]
public string ContentType { get; set; }

[JsonProperty("md5")]
public string MD5 { get; set; }
}

public Attachment ToAttachment()

Check warning on line 37 in src/Cronofy/Responses/AttachmentResponse.cs

View workflow job for this annotation

GitHub Actions / Run tests

A method should not follow a class
{
return new Attachment
{
AttachmentId = this.Attachment.AttachmentId,
FileName = this.Attachment.FileName,
ContentType = this.Attachment.ContentType,
MD5 = this.Attachment.MD5,
};
}
}
}
12 changes: 12 additions & 0 deletions src/Cronofy/UrlProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@ public sealed class UrlProvider
/// </summary>
private const string ConferencingServiceAuthorizationUrlFormat = "https://api{0}.cronofy.com/v1/conferencing_service_authorizations";

/// <summary>
/// The URL of the Attachments endpoint.
/// </summary>
private const string AttachmentsUrlFormat = "https://api{0}.cronofy.com/v1/attachments";

/// <summary>
/// Initializes a new instance of the <see cref="UrlProvider"/> class.
/// </summary>
Expand Down Expand Up @@ -218,6 +223,7 @@ internal UrlProvider(string dataCenter)
this.ProvisionApplicationUrl = string.Format(ProvisionApplicationUrlFormat, suffix);
this.ElementTokensUrl = string.Format(ElementTokensUrlFormat, suffix);
this.ConferencingServiceAuthorizationUrl = string.Format(ConferencingServiceAuthorizationUrlFormat, suffix);
this.AttachmentsUrl = string.Format(AttachmentsUrlFormat, suffix);
}

/// <summary>
Expand Down Expand Up @@ -567,5 +573,11 @@ public string BatchUrl
/// </summary>
/// <value>The conferencing service authorization URL.</value>
public string ConferencingServiceAuthorizationUrl { get; private set; }

/// <summary>
/// Gets the attachments URL.
/// </summary>
/// <value>The attachments URL.</value>
public string AttachmentsUrl { get; private set; }
}
}

0 comments on commit b9275ca

Please sign in to comment.