Skip to content

Commit dd339be

Browse files
Merge pull request #487 from notion-dotnet/477-add-support-for-create-file-upload
Add support for create file upload API endpoint
2 parents 975a175 + b7ea9f0 commit dd339be

18 files changed

+301
-3
lines changed

Src/Notion.Client/Api/ApiEndpoints.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,5 +139,10 @@ public static class AuthenticationUrls
139139
public static string IntrospectToken() => "/v1/oauth/introspect";
140140
public static string RefreshToken() => "/v1/oauth/token";
141141
}
142+
143+
public static class FileUploadsApiUrls
144+
{
145+
public static string Create() => "/v1/file_uploads";
146+
}
142147
}
143148
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
using System.Threading;
3+
using System.Threading.Tasks;
4+
5+
namespace Notion.Client
6+
{
7+
public sealed partial class FileUploadsClient : IFileUploadsClient
8+
{
9+
public async Task<CreateFileUploadResponse> CreateAsync(
10+
CreateFileUploadRequest fileUploadObjectRequest,
11+
CancellationToken cancellationToken = default)
12+
{
13+
if (fileUploadObjectRequest == null)
14+
{
15+
throw new ArgumentNullException(nameof(fileUploadObjectRequest));
16+
}
17+
18+
ICreateFileUploadBodyParameters body = fileUploadObjectRequest;
19+
20+
return await _restClient.PostAsync<CreateFileUploadResponse>(
21+
ApiEndpoints.FileUploadsApiUrls.Create(),
22+
body,
23+
cancellationToken: cancellationToken
24+
);
25+
}
26+
}
27+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace Notion.Client
2+
{
3+
public class CreateFileUploadRequest : ICreateFileUploadBodyParameters
4+
{
5+
public FileUploadMode Mode { get; set; }
6+
7+
public string Filename { get; set; }
8+
9+
public string ContentType { get; set; }
10+
11+
public int? NumberOfParts { get; set; }
12+
13+
public string ExternalUrl { get; set; }
14+
}
15+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System.Runtime.Serialization;
2+
3+
namespace Notion.Client
4+
{
5+
public enum FileUploadMode
6+
{
7+
[EnumMember(Value = "single_part")]
8+
SinglePart,
9+
10+
[EnumMember(Value = "multi_part")]
11+
MultiPart,
12+
13+
[EnumMember(Value = "external_url")]
14+
ExternalUrl
15+
}
16+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using Newtonsoft.Json;
2+
using Newtonsoft.Json.Converters;
3+
4+
namespace Notion.Client
5+
{
6+
public interface ICreateFileUploadBodyParameters
7+
{
8+
/// <summary>
9+
/// How the file is being sent. Use multi_part for files larger than 20MB.
10+
/// Use external_url for files that are temporarily hosted publicly elsewhere.
11+
/// Default is single_part.
12+
/// </summary>
13+
[JsonProperty("mode")]
14+
[JsonConverter(typeof(StringEnumConverter))]
15+
FileUploadMode Mode { get; }
16+
17+
/// <summary>
18+
/// Name of the file to be created. Required when mode is multi_part or external_url.
19+
/// Otherwise optional, and used to override the filename. Must include an extension, or have one inferred
20+
/// from the content_type parameter.
21+
/// </summary>
22+
[JsonProperty("filename")]
23+
string Filename { get; }
24+
25+
/// <summary>
26+
/// MIME type of the file to be created. Recommended when sending the file in multiple parts.
27+
/// Must match the content type of the file that's sent, and the extension of the filename parameter if any.
28+
/// </summary>
29+
[JsonProperty("content_type")]
30+
string ContentType { get; }
31+
32+
/// <summary>
33+
/// When mode is multi_part, the number of parts you are uploading.
34+
/// Must be between 1 and 1,000. This must match the number of parts as well as the final part_number you send.
35+
/// </summary>
36+
[JsonProperty("number_of_parts")]
37+
int? NumberOfParts { get; }
38+
39+
/// <summary>
40+
/// When mode is external_url, provide the HTTPS URL of a publicly accessible file to import into your workspace.
41+
/// </summary>
42+
[JsonProperty("external_url")]
43+
string ExternalUrl { get; }
44+
}
45+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace Notion.Client
2+
{
3+
public class CreateFileUploadResponse : FileObjectResponse
4+
{
5+
}
6+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using Newtonsoft.Json;
2+
3+
namespace Notion.Client
4+
{
5+
public class FileImportError
6+
{
7+
[JsonProperty("type")]
8+
public string Type { get; set; }
9+
10+
[JsonProperty("code")]
11+
public string Code { get; set; }
12+
13+
[JsonProperty("message")]
14+
public string Message { get; set; }
15+
16+
[JsonProperty("parameter")]
17+
public string Parameter { get; set; }
18+
19+
[JsonProperty("status_code")]
20+
public int? StatusCode { get; set; }
21+
}
22+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using Newtonsoft.Json;
2+
3+
namespace Notion.Client
4+
{
5+
public class FileImportErrorResult : FileImportResult
6+
{
7+
public override string Type => "error";
8+
9+
[JsonProperty("error")]
10+
public FileImportError Error { get; set; }
11+
}
12+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
using JsonSubTypes;
3+
using Newtonsoft.Json;
4+
using Newtonsoft.Json.Converters;
5+
6+
namespace Notion.Client
7+
{
8+
[JsonConverter(typeof(JsonSubtypes), "type")]
9+
[JsonSubtypes.KnownSubTypeAttribute(typeof(FileImportSuccessResult), "success")]
10+
[JsonSubtypes.KnownSubTypeAttribute(typeof(FileImportErrorResult), "error")]
11+
[JsonSubtypes.FallBackSubTypeAttribute(typeof(FileImportResult))]
12+
public abstract class FileImportResult
13+
{
14+
[JsonProperty("type")]
15+
public virtual string Type { get; set; }
16+
17+
[JsonProperty("imported_time")]
18+
[JsonConverter(typeof(IsoDateTimeConverter))]
19+
public DateTime ImportedTime { get; set; }
20+
}
21+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System.Collections.Generic;
2+
using Newtonsoft.Json;
3+
4+
namespace Notion.Client
5+
{
6+
public class FileImportSuccessResult : FileImportResult
7+
{
8+
public override string Type => "success";
9+
10+
[JsonProperty("success")]
11+
public Dictionary<string, object> Success { get; set; }
12+
}
13+
}

0 commit comments

Comments
 (0)