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

Commit ad43cff

Browse files
authored
Merge pull request #35 from Nicholi:other_apis
Added Account, Projects, and Snapshots api calls
2 parents 7a68350 + 183db82 commit ad43cff

17 files changed

Lines changed: 719 additions & 0 deletions
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using DigitalOcean.API.Clients;
2+
using DigitalOcean.API.Http;
3+
using DigitalOcean.API.Models.Responses;
4+
using NSubstitute;
5+
using Xunit;
6+
7+
namespace DigitalOcean.API.Tests.Clients {
8+
public class AccountClientTest {
9+
[Fact]
10+
public void CorrectRequestForGet() {
11+
var factory = Substitute.For<IConnection>();
12+
var client = new AccountClient(factory);
13+
14+
client.Get();
15+
16+
factory.Received().ExecuteRequest<Account>("account", null, null, "account");
17+
}
18+
}
19+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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 ProjectsClientTest {
11+
[Fact]
12+
public void CorrectRequestForGetAll() {
13+
var factory = Substitute.For<IConnection>();
14+
var client = new ProjectsClient(factory);
15+
16+
client.GetAll();
17+
factory.Received().GetPaginated<Project>("projects", null, "projects");
18+
}
19+
20+
[Fact]
21+
public void CorrectRequireForCreate() {
22+
var factory = Substitute.For<IConnection>();
23+
var client = new ProjectsClient(factory);
24+
25+
var data = new Models.Requests.Project();
26+
client.Create(data);
27+
28+
factory.Received().ExecuteRequest<Project>("projects", null, data, "project", Method.POST);
29+
}
30+
31+
[Fact]
32+
public void CorrectRequestForGet() {
33+
var factory = Substitute.For<IConnection>();
34+
var client = new ProjectsClient(factory);
35+
36+
client.Get("project:abc123");
37+
38+
var parameters = Arg.Is<List<Parameter>>(list => (string)list[0].Value == "project:abc123");
39+
factory.Received().ExecuteRequest<Project>("projects/{project_id}", parameters, null, "project");
40+
}
41+
42+
[Fact]
43+
public void CorrectRequestForGetDefault() {
44+
var factory = Substitute.For<IConnection>();
45+
var client = new ProjectsClient(factory);
46+
47+
client.GetDefault();
48+
49+
factory.Received().ExecuteRequest<Project>("projects/default", null, null, "project");
50+
}
51+
52+
[Fact]
53+
public void CorrectRequestForUpdate() {
54+
var factory = Substitute.For<IConnection>();
55+
var client = new ProjectsClient(factory);
56+
57+
var data = new Models.Requests.UpdateProject();
58+
client.Update("project:abc123", data);
59+
60+
var parameters = Arg.Is<List<Parameter>>(list => (string)list[0].Value == "project:abc123");
61+
factory.Received().ExecuteRequest<Project>("projects/{project_id}", parameters, data, "project", Method.PUT);
62+
}
63+
64+
[Fact]
65+
public void CorrectRequestForUpdateDefault() {
66+
var factory = Substitute.For<IConnection>();
67+
var client = new ProjectsClient(factory);
68+
69+
var data = new Models.Requests.UpdateProject();
70+
client.UpdateDefault(data);
71+
72+
factory.Received().ExecuteRequest<Project>("projects/default", null, data, "project", Method.PUT);
73+
}
74+
75+
[Fact]
76+
public void CorrectRequestForPatch() {
77+
var factory = Substitute.For<IConnection>();
78+
var client = new ProjectsClient(factory);
79+
80+
var data = new Models.Requests.PatchProject();
81+
client.Patch("project:abc123", data);
82+
83+
var parameters = Arg.Is<List<Parameter>>(list => (string)list[0].Value == "project:abc123");
84+
factory.Received().ExecuteRequest<Project>("projects/{project_id}", parameters, data, "project", Method.PATCH);
85+
}
86+
87+
[Fact]
88+
public void CorrectRequestForPatchDefault() {
89+
var factory = Substitute.For<IConnection>();
90+
var client = new ProjectsClient(factory);
91+
92+
var data = new Models.Requests.PatchProject();
93+
client.PatchDefault(data);
94+
95+
factory.Received().ExecuteRequest<Project>("projects/default", null, data, "project", Method.PATCH);
96+
}
97+
}
98+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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 SnapshotsClientTest {
11+
[Fact]
12+
public void CorrectRequestForGetAll() {
13+
var factory = Substitute.For<IConnection>();
14+
var client = new SnapshotsClient(factory);
15+
16+
client.GetAll();
17+
factory.Received().GetPaginated<Snapshot>("snapshots", null, "snapshots");
18+
}
19+
20+
[Fact]
21+
public void CorrectRequestForGetAllDroplet() {
22+
var factory = Substitute.For<IConnection>();
23+
var client = new SnapshotsClient(factory);
24+
25+
client.GetAllDroplet();
26+
factory.Received().GetPaginated<Snapshot>("snapshots?resource_type=droplet", null, "snapshots");
27+
}
28+
29+
[Fact]
30+
public void CorrectRequestForGetAllVolume() {
31+
var factory = Substitute.For<IConnection>();
32+
var client = new SnapshotsClient(factory);
33+
34+
client.GetAllVolume();
35+
factory.Received().GetPaginated<Snapshot>("snapshots?resource_type=volume", null, "snapshots");
36+
}
37+
38+
[Fact]
39+
public void CorrectRequestForGet() {
40+
var factory = Substitute.For<IConnection>();
41+
var client = new SnapshotsClient(factory);
42+
43+
client.Get("snapshot:abc123");
44+
45+
var parameters = Arg.Is<List<Parameter>>(list => (string)list[0].Value == "snapshot:abc123");
46+
factory.Received().ExecuteRequest<Snapshot>("snapshots/{snapshot_id}", parameters, null, "snapshot");
47+
}
48+
49+
[Fact]
50+
public void CorrectRequestForDelete() {
51+
var factory = Substitute.For<IConnection>();
52+
var client = new SnapshotsClient(factory);
53+
54+
client.Delete("snapshot:abc123");
55+
56+
var parameters = Arg.Is<List<Parameter>>(list => (string)list[0].Value == "snapshot:abc123");
57+
factory.Received().ExecuteRaw("snapshots/{snapshot_id}", parameters, null, Method.DELETE);
58+
}
59+
60+
}
61+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System.Threading.Tasks;
2+
using DigitalOcean.API.Http;
3+
using DigitalOcean.API.Models.Responses;
4+
5+
namespace DigitalOcean.API.Clients {
6+
public class AccountClient : IAccountClient {
7+
private readonly IConnection _connection;
8+
9+
public AccountClient(IConnection connection) {
10+
_connection = connection;
11+
}
12+
13+
/// <summary>
14+
/// To show information about the current user's account.
15+
/// </summary>
16+
public Task<Account> Get() {
17+
return _connection.ExecuteRequest<Account>("account", null, null, "account");
18+
}
19+
}
20+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System.Threading.Tasks;
2+
using DigitalOcean.API.Models.Responses;
3+
4+
namespace DigitalOcean.API.Clients {
5+
public interface IAccountClient {
6+
/// <summary>
7+
/// To show information about the current user's account.
8+
/// </summary>
9+
Task<Account> Get();
10+
}
11+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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 IProjectsClient {
7+
/// <summary>
8+
/// To list all your projects.
9+
/// </summary>
10+
Task<IReadOnlyList<Project>> GetAll();
11+
12+
/// <summary>
13+
/// To create a project.
14+
/// </summary>
15+
Task<Project> Create(Models.Requests.Project project);
16+
17+
/// <summary>
18+
/// To get a project.
19+
/// </summary>
20+
Task<Project> Get(string projectId);
21+
22+
/// <summary>
23+
/// To get the default project.
24+
/// </summary>
25+
Task<Project> GetDefault();
26+
27+
/// <summary>
28+
/// To update a project.
29+
/// </summary>
30+
Task<Project> Update(string projectId, Models.Requests.UpdateProject updateProject);
31+
32+
/// <summary>
33+
/// To update the default project.
34+
/// </summary>
35+
Task<Project> UpdateDefault(Models.Requests.UpdateProject updateProject);
36+
37+
/// <summary>
38+
/// To update only specific attributes of a project.
39+
/// </summary>
40+
Task<Project> Patch(string projectId, Models.Requests.PatchProject patchProject);
41+
42+
/// <summary>
43+
/// To update only specific attributes of the default project.
44+
/// </summary>
45+
Task<Project> PatchDefault(Models.Requests.PatchProject patchProject);
46+
}
47+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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 ISnapshotsClient {
7+
/// <summary>
8+
/// To list all of the snapshots available on your account.
9+
/// </summary>
10+
Task<IReadOnlyList<Snapshot>> GetAll();
11+
12+
/// <summary>
13+
/// To retrieve only snapshots based on Droplets.
14+
/// </summary>
15+
Task<IReadOnlyList<Snapshot>> GetAllDroplet();
16+
17+
/// <summary>
18+
/// To retrieve only snapshots based on volumes.
19+
/// </summary>
20+
Task<IReadOnlyList<Snapshot>> GetAllVolume();
21+
22+
/// <summary>
23+
/// To retrieve information about a snapshot,
24+
/// </summary>
25+
Task<Snapshot> Get(string snapshotId);
26+
27+
/// <summary>
28+
/// To delete a snapshot.
29+
/// </summary>
30+
Task Delete(string snapshotId);
31+
}
32+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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 ProjectsClient : IProjectsClient {
9+
private readonly IConnection _connection;
10+
11+
public ProjectsClient(IConnection connection) {
12+
_connection = connection;
13+
}
14+
15+
/// <summary>
16+
/// To list all your projects.
17+
/// </summary>
18+
public Task<IReadOnlyList<Project>> GetAll() {
19+
return _connection.GetPaginated<Project>("projects", null, "projects");
20+
}
21+
22+
/// <summary>
23+
/// To create a project.
24+
/// </summary>
25+
public Task<Project> Create(Models.Requests.Project project) {
26+
return _connection.ExecuteRequest<Project>("projects", null, project, "project", Method.POST);
27+
}
28+
29+
/// <summary>
30+
/// To get a project.
31+
/// </summary>
32+
public Task<Project> Get(string projectId) {
33+
var parameters = new List<Parameter> {
34+
new Parameter { Name = "project_id", Value = projectId, Type = ParameterType.UrlSegment }
35+
};
36+
return _connection.ExecuteRequest<Project>("projects/{project_id}", parameters, null, "project");
37+
}
38+
39+
/// <summary>
40+
/// To get the default project.
41+
/// </summary>
42+
public Task<Project> GetDefault() {
43+
return _connection.ExecuteRequest<Project>("projects/default", null, null, "project");
44+
}
45+
46+
/// <summary>
47+
/// To update a project.
48+
/// </summary>
49+
public Task<Project> Update(string projectId, Models.Requests.UpdateProject updateProject) {
50+
var parameters = new List<Parameter> {
51+
new Parameter { Name = "project_id", Value = projectId, Type = ParameterType.UrlSegment }
52+
};
53+
return _connection.ExecuteRequest<Project>("projects/{project_id}", parameters, updateProject, "project", Method.PUT);
54+
}
55+
56+
/// <summary>
57+
/// To update the default project.
58+
/// </summary>
59+
public Task<Project> UpdateDefault(Models.Requests.UpdateProject updateProject) {
60+
// implied to always be true
61+
updateProject.IsDefault = true;
62+
return _connection.ExecuteRequest<Project>("projects/default", null, updateProject, "project", Method.PUT);
63+
}
64+
65+
/// <summary>
66+
/// To update only specific attributes of a project.
67+
/// </summary>
68+
public Task<Project> Patch(string projectId, Models.Requests.PatchProject patchProject) {
69+
var parameters = new List<Parameter> {
70+
new Parameter { Name = "project_id", Value = projectId, Type = ParameterType.UrlSegment }
71+
};
72+
return _connection.ExecuteRequest<Project>("projects/{project_id}", parameters, patchProject, "project", Method.PATCH);
73+
}
74+
75+
/// <summary>
76+
/// To update only specific attributes of the default project.
77+
/// </summary>
78+
public Task<Project> PatchDefault(Models.Requests.PatchProject patchProject) {
79+
// implied to always be true
80+
patchProject.IsDefault = true;
81+
return _connection.ExecuteRequest<Project>("projects/default", null, patchProject, "project", Method.PATCH);
82+
}
83+
}
84+
}
85+

0 commit comments

Comments
 (0)