Skip to content
This repository was archived by the owner on Sep 6, 2025. It is now read-only.

Commit a00afa3

Browse files
committed
Added Certificates api calls
1 parent b91086e commit a00afa3

7 files changed

Lines changed: 237 additions & 0 deletions

File tree

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using System.Collections.Generic;
2+
using DigitalOcean.API.Clients;
3+
using DigitalOcean.API.Http;
4+
using DigitalOcean.API.Models.Responses;
5+
using NSubstitute;
6+
using RestSharp;
7+
using Xunit;
8+
9+
namespace DigitalOcean.API.Tests.Clients {
10+
public class CertificatesClientTest {
11+
[Fact]
12+
public void CorrectRequestForGetAll() {
13+
var factory = Substitute.For<IConnection>();
14+
var client = new CertificatesClient(factory);
15+
16+
client.GetAll();
17+
18+
factory.Received().GetPaginated<Certificate>("certificates", null, "certificates");
19+
}
20+
21+
[Fact]
22+
public void CorrectRequestForGet() {
23+
var factory = Substitute.For<IConnection>();
24+
var client = new CertificatesClient(factory);
25+
26+
client.Get("certificate:abc123");
27+
28+
var parameters = Arg.Is<List<Parameter>>(list => (string)list[0].Value == "certificate:abc123");
29+
factory.Received().ExecuteRequest<Certificate>("certificates/{certificate_id}", parameters, null, "certificate");
30+
}
31+
32+
[Fact]
33+
public void CorrectRequestForCreate() {
34+
var factory = Substitute.For<IConnection>();
35+
var client = new CertificatesClient(factory);
36+
37+
var body = new Models.Requests.Certificate();
38+
client.Create(body);
39+
40+
factory.Received().ExecuteRequest<Certificate>("certificates", null, body, "certificate", Method.POST);
41+
}
42+
43+
[Fact]
44+
public void CorrectRequestForDelete() {
45+
var factory = Substitute.For<IConnection>();
46+
var client = new CertificatesClient(factory);
47+
48+
client.Delete("certificate:abc123");
49+
50+
var parameters = Arg.Is<List<Parameter>>(list => (string)list[0].Value == "certificate:abc123");
51+
factory.Received().ExecuteRaw("certificates/{certificate_id}", parameters, null, Method.DELETE);
52+
}
53+
}
54+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System.Collections.Generic;
2+
using System.Threading.Tasks;
3+
using DigitalOcean.API.Http;
4+
using DigitalOcean.API.Models.Responses;
5+
using RestSharp;
6+
7+
namespace DigitalOcean.API.Clients {
8+
public class CertificatesClient : ICertificatesClient {
9+
private readonly IConnection _connection;
10+
11+
public CertificatesClient(IConnection connection) {
12+
_connection = connection;
13+
}
14+
15+
/// <summary>
16+
/// To list all of the certificates available on your account.
17+
/// </summary>
18+
public Task<IReadOnlyList<Certificate>> GetAll() {
19+
return _connection.GetPaginated<Certificate>("certificates", null, "certificates");
20+
}
21+
22+
/// <summary>
23+
/// To show information about an existing certificate.
24+
/// </summary>
25+
public Task<Certificate> Get(string certificateId) {
26+
var parameters = new List<Parameter> {
27+
new Parameter { Name = "certificate_id", Value = certificateId, Type = ParameterType.UrlSegment }
28+
};
29+
return _connection.ExecuteRequest<Certificate>("certificates/{certificate_id}", parameters, null, "certificate");
30+
}
31+
32+
/// <summary>
33+
/// Create a new Let's Encrypt Certificate:
34+
/// When using Let's Encrypt to create a certificate, the dns_names attribute must be provided,
35+
/// and the type must be set to "lets_encrypt".
36+
///
37+
/// Create a new custom Certificate:
38+
/// When uploading a user-generated certificate, the private_key, leaf_certificate, and optionally the certificate_chain
39+
/// attributes should be provided. The type must be set to "custom".
40+
/// </summary>
41+
public Task<Certificate> Create(Models.Requests.Certificate certificate) {
42+
return _connection.ExecuteRequest<Certificate>("certificates", null, certificate, "certificate", Method.POST);
43+
}
44+
45+
/// <summary>
46+
/// To delete a specific certificate.
47+
/// </summary>
48+
public Task Delete(string certificateId) {
49+
var parameters = new List<Parameter> {
50+
new Parameter { Name = "certificate_id", Value = certificateId, Type = ParameterType.UrlSegment }
51+
};
52+
return _connection.ExecuteRaw("certificates/{certificate_id}", parameters, null, Method.DELETE);
53+
}
54+
}
55+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System.Collections.Generic;
2+
using System.Threading.Tasks;
3+
using DigitalOcean.API.Models.Responses;
4+
5+
namespace DigitalOcean.API.Clients {
6+
public interface ICertificatesClient {
7+
/// <summary>
8+
/// To list all of the certificates available on your account.
9+
/// </summary>
10+
Task<IReadOnlyList<Certificate>> GetAll();
11+
12+
/// <summary>
13+
/// To show information about an existing certificate.
14+
/// </summary>
15+
Task<Certificate> Get(string certificateId);
16+
17+
/// <summary>
18+
/// Create a new Let's Encrypt Certificate:
19+
/// When using Let's Encrypt to create a certificate, the dns_names attribute must be provided,
20+
/// and the type must be set to "lets_encrypt".
21+
///
22+
/// Create a new custom Certificate:
23+
/// When uploading a user-generated certificate, the private_key, leaf_certificate, and optionally the certificate_chain
24+
/// attributes should be provided. The type must be set to "custom".
25+
/// </summary>
26+
Task<Certificate> Create(Models.Requests.Certificate certificate);
27+
28+
/// <summary>
29+
/// To delete a specific certificate.
30+
/// </summary>
31+
Task Delete(string certificateId);
32+
}
33+
}

DigitalOcean.API/DigitalOceanClient.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public DigitalOceanClient(string token) {
1717

1818
Actions = new ActionsClient(_connection);
1919
CdnEndpoints = new CdnEndpointsClient(_connection);
20+
Certificates = new CertificatesClient(_connection);
2021
DomainRecords = new DomainRecordsClient(_connection);
2122
Domains = new DomainsClient(_connection);
2223
DropletActions = new DropletActionsClient(_connection);
@@ -38,6 +39,7 @@ public IRateLimit Rates {
3839

3940
public IActionsClient Actions { get; private set; }
4041
public ICdnEndpointsClient CdnEndpoints { get; private set; }
42+
public ICertificatesClient Certificates { get; private set; }
4143
public IDomainRecordsClient DomainRecords { get; private set; }
4244
public IDomainsClient Domains { get; private set; }
4345
public IDropletActionsClient DropletActions { get; private set; }

DigitalOcean.API/IDigitalOceanClient.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ namespace DigitalOcean.API {
55
public interface IDigitalOceanClient {
66
IActionsClient Actions { get; }
77
ICdnEndpointsClient CdnEndpoints { get; }
8+
ICertificatesClient Certificates { get; }
89
IDomainRecordsClient DomainRecords { get; }
910
IDomainsClient Domains { get; }
1011
IDropletActionsClient DropletActions { get; }
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System.Collections.Generic;
2+
using Newtonsoft.Json;
3+
4+
namespace DigitalOcean.API.Models.Requests {
5+
public class Certificate {
6+
/// <summary>
7+
/// A unique human-readable name referring to a certificate.
8+
/// </summary>
9+
[JsonProperty("name")]
10+
public string Name { get; set; }
11+
12+
/// <summary>
13+
/// A string representing the type of the certificate. The value should be "custom" for a user-uploaded certificate or
14+
/// "lets_encrypt" for one automatically generated with Let's Encrypt.
15+
/// If not provided, "custom" will be assumed by default.
16+
/// </summary>
17+
[JsonProperty("type")]
18+
public string Type { get; set; }
19+
20+
/// <summary>
21+
/// An array of fully qualified domain names (FQDNs) for which the certificate will be issued.
22+
/// The domains must be managed using DigitalOcean's DNS.
23+
/// </summary>
24+
[JsonProperty("dns_names")]
25+
public List<string> DnsNames { get; set; }
26+
27+
/// <summary>
28+
/// The contents of a PEM-formatted private-key corresponding to the SSL certificate.
29+
/// </summary>
30+
[JsonProperty("private_key")]
31+
public string PrivateKey { get; set; }
32+
33+
/// <summary>
34+
/// The contents of a PEM-formatted public SSL certificate.
35+
/// </summary>
36+
[JsonProperty("leaf_certificate")]
37+
public string LeafCertificate { get; set; }
38+
39+
/// <summary>
40+
/// The full PEM-formatted trust chain between the certificate authority's certificate and your domain's SSL certificate.
41+
/// </summary>
42+
[JsonProperty("certificate_chain")]
43+
public string CertificateChain { get; set; }
44+
}
45+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace DigitalOcean.API.Models.Responses {
5+
public class Certificate {
6+
/// <summary>
7+
/// A unique ID that can be used to identify and reference a certificate.
8+
/// </summary>
9+
public string Id { get; set; }
10+
11+
/// <summary>
12+
/// A unique human-readable name referring to a certificate.
13+
/// </summary>
14+
public string Name { get; set; }
15+
16+
/// <summary>
17+
/// A time value given in ISO8601 combined date and time format that represents the certificate's expiration date.
18+
/// </summary>
19+
public DateTime NotAfter { get; set; }
20+
21+
/// <summary>
22+
/// A unique identifier generated from the SHA-1 fingerprint of the certificate.
23+
/// </summary>
24+
public string Sha1Fingerprint { get; set; }
25+
26+
/// <summary>
27+
/// A time value given in ISO8601 combined date and time format that represents when the certificate was created.
28+
/// </summary>
29+
public DateTime CreatedAt { get; set; }
30+
31+
/// <summary>
32+
/// An array of fully qualified domain names (FQDNs) for which the certificate was issued.
33+
/// </summary>
34+
public List<string> DnsNames { get; set; }
35+
36+
/// <summary>
37+
/// A string representing the current state of the certificate. It may be "pending", "verified", or "error".
38+
/// </summary>
39+
public string State { get; set; }
40+
41+
/// <summary>
42+
/// A string representing the type of the certificate. The value will be "custom" for a user-uploaded certificate or
43+
/// "lets_encrypt" for one automatically generated with Let's Encrypt.
44+
/// </summary>
45+
public string Type { get; set; }
46+
}
47+
}

0 commit comments

Comments
 (0)