|
1 | 1 | using System; |
2 | 2 | using System.Collections.Generic; |
| 3 | +using System.IO; |
3 | 4 | using System.Net.Http; |
4 | 5 | using System.Net.Http.Headers; |
5 | 6 | using System.Text; |
@@ -91,6 +92,52 @@ public async Task DeleteAsync( |
91 | 92 | await SendAsync(uri, HttpMethod.Delete, queryParams, headers, null, cancellationToken); |
92 | 93 | } |
93 | 94 |
|
| 95 | + public async Task<UploadResponse> Upload(string filePath, JsonSerializerSettings serializerSettings = null) |
| 96 | + { |
| 97 | + var response = await this.PostAsync<UploadResponse>("https://api.notion.com/v1/file_uploads", |
| 98 | + new UploadRequest {Mode = "single_part"}); |
| 99 | + |
| 100 | + using (var formData = new MultipartFormDataContent()) |
| 101 | + { |
| 102 | + var fileStream = File.OpenRead(filePath); |
| 103 | + var fileContent = new StreamContent(fileStream); |
| 104 | + formData.Add(fileContent, "file", Path.GetFileName(filePath)); |
| 105 | + |
| 106 | + var uploadResponse = await this.SendAsync(response.UploadUrl, HttpMethod.Post, attachContent: message => |
| 107 | + { |
| 108 | + message.Content = formData; |
| 109 | + }); |
| 110 | + return await uploadResponse.ParseStreamAsync<UploadResponse>(serializerSettings); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + public class UploadRequest |
| 115 | + { |
| 116 | + public string Mode { get; set; } |
| 117 | + } |
| 118 | + |
| 119 | + public class UploadResponse |
| 120 | + { |
| 121 | + public Guid Id { get; set; } |
| 122 | + public string Object { get; set; } |
| 123 | + [JsonProperty("created_time")] |
| 124 | + public DateTime CreatedTime { get; set; } |
| 125 | + [JsonProperty("last_edited_time")] |
| 126 | + public DateTime LastEditedTime { get; set; } |
| 127 | + [JsonProperty("expiry_time")] |
| 128 | + public DateTime ExpiryTime { get; set; } |
| 129 | + [JsonProperty("upload_url")] |
| 130 | + public string UploadUrl { get; set; } |
| 131 | + public bool Archived { get; set; } |
| 132 | + public string Status { get; set; } |
| 133 | + public string Filename { get; set; } |
| 134 | + [JsonProperty("content_type")] |
| 135 | + public string ContentType { get; set; } |
| 136 | + [JsonProperty("request_id")] |
| 137 | + public Guid RequestId { get; set; } |
| 138 | + } |
| 139 | + |
| 140 | + |
94 | 141 | private static ClientOptions MergeOptions(ClientOptions options) |
95 | 142 | { |
96 | 143 | return new ClientOptions |
|
0 commit comments