Skip to content

Commit d7c57d1

Browse files
committed
Adds DevResource endpoints
1 parent aaae376 commit d7c57d1

File tree

9 files changed

+234
-1
lines changed

9 files changed

+234
-1
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ var figmaHttpClient = new FigmaHttpClient(logger, configuration, apiKey: "###",
4141

4242
### Change log
4343

44+
#### v1.4.0:
45+
- Added endpoints for [Dev Resources](https://www.figma.com/developers/api#dev-resources).
46+
4447
#### v1.3.0:
4548
- Added `ServiceCollectionExtensions` to extract http client and made it configurable.
4649
- The configuration key `FIGMA_API_TOKEN` has changed to `FigmaHttpClient:ApiToken`.

Source/FigmaDotNet/FigmaDotNet.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<PackageTags>Figma, Design, Icons</PackageTags>
1515
<SignAssembly>False</SignAssembly>
1616
<PackageReadmeFile>README.md</PackageReadmeFile>
17-
<Version>1.3.2</Version>
17+
<Version>1.4.0</Version>
1818
<PackageLicenseExpression>MIT</PackageLicenseExpression>
1919
</PropertyGroup>
2020

Source/FigmaDotNet/FigmaHttpClient.cs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
using FigmaDotNet.Enums;
2323
using FigmaDotNet.Models.Response;
2424
using FigmaDotNet.Models.Webhook;
25+
using FigmaDotNet.Models;
2526

2627
namespace FigmaDotNet;
2728

@@ -432,6 +433,80 @@ public async Task PostWebhookAsync(WebHook requestPayload, CancellationToken can
432433
_logger.LogInformation($"Webhook was created: {result}");
433434
}
434435

436+
/// <summary>
437+
/// Get dev resources in a file.
438+
/// </summary>
439+
/// <param name="fileKey"></param>
440+
/// <param name="cancellationToken"></param>
441+
/// <returns></returns>
442+
public async Task<GetDevResourceResponse> GetDevResourcesAsync(string fileKey, CancellationToken cancellationToken = default)
443+
{
444+
string fetchUrl = $"/v1/files/{fileKey}/dev_resources";
445+
var result = await RateLimitedFigmaApiCallAsync<GetDevResourceResponse>(fetchUrl, _fileCostRateLimiter, cancellationToken: cancellationToken);
446+
447+
return result;
448+
}
449+
450+
/// <summary>
451+
/// Bulk create dev resources across multiple files.
452+
///
453+
/// Dev resources that are successfully created will show up in the links_created array in the response.
454+
///
455+
/// If there are any dev resources that cannot be created, you may still get a 200 response.These resources will show up in the errors array. Some reasons a dev resource cannot be created include:
456+
/// - Resource points to a file_key that cannot be found.
457+
/// - The node already has the maximum of 10 dev resources.
458+
/// - Another dev resource for the node has the same url.
459+
/// </summary>
460+
/// <param name="fileKey"></param>
461+
/// <param name="devResource"></param>
462+
/// <param name="cancellationToken"></param>
463+
/// <returns></returns>
464+
public async Task<PostDevResourceResponse> PostDevResourceAsync(string fileKey, DevResource devResource, CancellationToken cancellationToken = default)
465+
{
466+
string fetchUrl = $"/v1/files/{fileKey}/dev_resources";
467+
var content = new StringContent(JsonSerializer.Serialize(devResource), Encoding.UTF8, "application/json");
468+
var result = await RateLimitedFigmaApiCallAsync<PostDevResourceResponse>(fetchUrl, _fileCostRateLimiter, HttpMethod.Post, content, cancellationToken);
469+
470+
return result;
471+
}
472+
473+
/// <summary>
474+
/// Bulk update dev resources across multiple files.
475+
///
476+
/// Ids for dev resources that are successfully updated will show up in the links_updated array in the response.
477+
///
478+
/// If there are any dev resources that cannot be updated, you may still get a 200 response.These resources will show up in the errors array.
479+
/// </summary>
480+
/// <param name="fileKey"></param>
481+
/// <param name="devResourceId"></param>
482+
/// <param name="devResource"></param>
483+
/// <param name="cancellationToken"></param>
484+
/// <returns></returns>
485+
public async Task<PutDevResourceResponse> PutDevResourceAsync(string fileKey, string devResourceId, DevResource devResource, CancellationToken cancellationToken = default)
486+
{
487+
string fetchUrl = $"/v1/files/{fileKey}/dev_resources/{devResourceId}";
488+
var content = new StringContent(JsonSerializer.Serialize(devResource), Encoding.UTF8, "application/json");
489+
var result = await RateLimitedFigmaApiCallAsync<PutDevResourceResponse>(fetchUrl, _fileCostRateLimiter, HttpMethod.Put, content, cancellationToken);
490+
491+
return result;
492+
}
493+
494+
/// <summary>
495+
/// Delete a dev resources from a file.
496+
/// </summary>
497+
/// <param name="fileKey"></param>
498+
/// <param name="devResourceId"></param>
499+
/// <param name="cancellationToken"></param>
500+
/// <returns></returns>
501+
public async Task<bool> DeleteDevResourceAsync(string fileKey, string devResourceId, CancellationToken cancellationToken = default)
502+
{
503+
string fetchUrl = $"/v1/files/{fileKey}/dev_resources/{devResourceId}";
504+
var result = await RateLimitedFigmaApiCallAsync<string>(fetchUrl, _fileCostRateLimiter, HttpMethod.Delete, cancellationToken: cancellationToken);
505+
506+
_logger.LogInformation($"Deleted dev resource with ID: '{devResourceId}'");
507+
return true;
508+
}
509+
435510
public void Dispose()
436511
{
437512
_commentCostRateLimiter.Dispose();
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
namespace FigmaDotNet.Models;
2+
3+
/// <summary>
4+
/// A dev resource in a file.
5+
/// </summary>
6+
public class DevResource
7+
{
8+
/// <summary>
9+
/// The unique identifier of the resource.
10+
/// </summary>
11+
public string Id { get; set; }
12+
13+
/// <summary>
14+
/// The name of the resource.
15+
/// </summary>
16+
public string Name { get; set; }
17+
18+
/// <summary>
19+
/// The description of the resource.
20+
/// </summary>
21+
public string Description { get; set; }
22+
23+
/// <summary>
24+
/// The type of the resource.
25+
/// </summary>
26+
public string ResourceType { get; set; }
27+
28+
/// <summary>
29+
/// The URL of the resource.
30+
/// </summary>
31+
public string Url { get; set; }
32+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
namespace FigmaDotNet.Models;
2+
3+
/// <summary>
4+
/// Payload to create a dev resource in a file.
5+
/// </summary>
6+
public class DevResourceCreate
7+
{
8+
/// <summary>
9+
/// The name of the resource.
10+
/// </summary>
11+
public string Name { get; set; }
12+
13+
/// <summary>
14+
/// The description of the resource.
15+
/// </summary>
16+
public string Description { get; set; }
17+
18+
/// <summary>
19+
/// The type of the resource.
20+
/// </summary>
21+
public string ResourceType { get; set; }
22+
23+
/// <summary>
24+
/// The URL of the resource.
25+
/// </summary>
26+
public string Url { get; set; }
27+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System;
2+
3+
namespace FigmaDotNet.Models;
4+
5+
/// <summary>
6+
/// Payload to update a dev resource in a file.
7+
/// </summary>
8+
public class DevResourceUpdate
9+
{
10+
/// <summary>
11+
/// The unique identifier of the resource.
12+
/// </summary>
13+
public string Id { get; set; }
14+
15+
/// <summary>
16+
/// The name of the resource.
17+
/// </summary>
18+
public string Name { get; set; }
19+
20+
/// <summary>
21+
/// The description of the resource.
22+
/// </summary>
23+
public string Description { get; set; }
24+
25+
/// <summary>
26+
/// The type of the resource.
27+
/// </summary>
28+
public string ResourceType { get; set; }
29+
30+
/// <summary>
31+
/// The URL of the resource.
32+
/// </summary>
33+
public string Url { get; set; }
34+
35+
/// <summary>
36+
/// The date and time when the resource was last updated.
37+
/// </summary>
38+
public DateTime UpdatedAt { get; set; }
39+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System.Collections.Generic;
2+
using System.Text.Json.Serialization;
3+
4+
namespace FigmaDotNet.Models.Response;
5+
6+
public class GetDevResourceResponse : FigmaResponse
7+
{
8+
[JsonPropertyName("dev_resources")]
9+
public IEnumerable<DevResource> DevResources { get; set; }
10+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System.Collections.Generic;
2+
using System.Text.Json.Serialization;
3+
4+
namespace FigmaDotNet.Models.Response;
5+
6+
public class PostDevResourceResponse : FigmaResponse
7+
{
8+
[JsonPropertyName("links_created")]
9+
public IEnumerable<DevResource> LinksCreated { get; set; }
10+
11+
[JsonPropertyName("errors")]
12+
public IEnumerable<PostDevResourceError> Errors { get; set; }
13+
}
14+
15+
public class PostDevResourceError
16+
{
17+
[JsonPropertyName("file_key")]
18+
public string FileKey { get; set; }
19+
20+
[JsonPropertyName("node_id")]
21+
public string NodeId { get; set; }
22+
23+
[JsonPropertyName("error")]
24+
public string Error { get; set; }
25+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System.Collections.Generic;
2+
using System.Text.Json.Serialization;
3+
4+
namespace FigmaDotNet.Models.Response;
5+
6+
public class PutDevResourceResponse : FigmaResponse
7+
{
8+
[JsonPropertyName("links_updated")]
9+
public IEnumerable<string> LinksUpdated { get; set; }
10+
11+
[JsonPropertyName("errors")]
12+
public IEnumerable<PutDevResourceError> Errors { get; set; }
13+
}
14+
15+
public class PutDevResourceError
16+
{
17+
[JsonPropertyName("id")]
18+
public string Id { get; set; }
19+
20+
[JsonPropertyName("error")]
21+
public string Error { get; set; }
22+
}

0 commit comments

Comments
 (0)