Skip to content

Commit a7a04dd

Browse files
authored
Merge pull request #60 from Zechiax/refactor/endpoints
Refactor endpoints, add more documentation
2 parents 0797e6e + e0beb7b commit a7a04dd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+426
-155
lines changed

Modrinth.Net.Test/ModrinthApiTests/EndpointTests.cs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,34 @@ public class EndpointTests
88
{
99
protected const string ModrinthNetTestProjectId = "jKePI2WR";
1010
protected const string ModrinthNetTestUploadedVersionId = "dJIVHDfy";
11-
12-
/// <summary>
13-
/// Must be users that have at least one project
14-
/// The first ID should be the ID of the user that will be authenticated
15-
/// </summary>
16-
protected readonly string[] TestUserIds = {"MaovZxtD", "5XoMa0C4"};
17-
18-
protected static readonly string[] ValidSha512Hashes = {"f62b94dbdb7ec79c1cc9f7f01a07b72828e77d22426552cd876d0fa8ba2a446efaecaea262ed481b2f77fa57063a3bdcd1c5febb8ae97a766d82abf3eb9ee198", "88014dd2d5fe7a259648eb716690f282e220556665ff20746bfa1f1d5a5f480e92fa94e07ea64db700c66720977d0c2147954efbf2c0c48bf38e419f04820453" };
19-
protected static readonly string[] ValidSha1Hashes = { "7e64ba677dbae046389b63a4db324284355987db", "fde0d8156d8d46b2b0f9d09906ef7f83ce712517" };
20-
21-
protected string TestUserId => TestUserIds[0];
2211

2312
protected const string TestProjectSlug = "gravestones";
2413

14+
protected static readonly string[] ValidSha512Hashes =
15+
{
16+
"f62b94dbdb7ec79c1cc9f7f01a07b72828e77d22426552cd876d0fa8ba2a446efaecaea262ed481b2f77fa57063a3bdcd1c5febb8ae97a766d82abf3eb9ee198",
17+
"88014dd2d5fe7a259648eb716690f282e220556665ff20746bfa1f1d5a5f480e92fa94e07ea64db700c66720977d0c2147954efbf2c0c48bf38e419f04820453"
18+
};
19+
20+
protected static readonly string[] ValidSha1Hashes =
21+
{"7e64ba677dbae046389b63a4db324284355987db", "fde0d8156d8d46b2b0f9d09906ef7f83ce712517"};
22+
2523
private static readonly IConfigurationRoot Configuration =
2624
new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();
2725

2826
protected readonly FileInfo Icon = new("../../../../Modrinth.Net.Test/Assets/Icons/ModrinthNet.png");
2927

28+
/// <summary>
29+
/// Must be users that have at least one project
30+
/// The first ID should be the ID of the user that will be authenticated
31+
/// </summary>
32+
protected readonly string[] TestUserIds = {"MaovZxtD", "5XoMa0C4"};
33+
3034
protected IModrinthClient Client = null!;
3135
protected IModrinthClient NoAuthClient = null!;
3236

37+
protected string TestUserId => TestUserIds[0];
38+
3339
private static string GetToken()
3440
{
3541
var token = Environment.GetEnvironmentVariable("MODRINTH_TOKEN");

Modrinth.Net.Test/ModrinthApiTests/UserEndpointTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public async Task ChangeIcon_WithValidId_ShouldSucceed()
6363
public async Task GetUsersProjects()
6464
{
6565
var projects = await Client.User.GetProjectsAsync(TestUserId);
66-
66+
6767
Assert.That(projects, Is.Not.Null);
6868
Assert.That(projects, Is.Not.Empty);
6969
}
@@ -72,10 +72,10 @@ public async Task GetUsersProjects()
7272
public async Task GetMultipleUsers()
7373
{
7474
var users = await Client.User.GetMultipleAsync(TestUserIds);
75-
75+
7676
Assert.That(users, Is.Not.Null);
7777
Assert.That(users, Is.Not.Empty);
78-
78+
7979
foreach (var user in users)
8080
{
8181
Assert.That(user, Is.Not.Null);

Modrinth.Net.Test/ModrinthApiTests/VersionFileTests.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,31 @@ public class TestVersionFile : EndpointTests
1111
public async Task GetVersionFromHashSha512(int index)
1212
{
1313
var hash = ValidSha512Hashes[index];
14-
14+
1515
var version = await Client.VersionFile.GetVersionByHashAsync(hash, HashAlgorithm.Sha512);
16-
16+
1717
Assert.That(version, Is.Not.Null);
1818
// Check that one of the files has the correct hash
19-
19+
2020
var file = version.Files.FirstOrDefault(f => f.Hashes.Sha512 == hash);
21-
21+
2222
Assert.That(file, Is.Not.Null);
2323
}
24-
24+
2525
[Test]
2626
[TestCase(0)]
2727
[TestCase(1)]
2828
public async Task GetVersionFromHashSha1(int index)
2929
{
3030
var hash = ValidSha1Hashes[index];
31-
32-
var version = await Client.VersionFile.GetVersionByHashAsync(hash, HashAlgorithm.Sha1);
33-
31+
32+
var version = await Client.VersionFile.GetVersionByHashAsync(hash);
33+
3434
Assert.That(version, Is.Not.Null);
3535
// Check that one of the files has the correct hash
36-
36+
3737
var file = version.Files.FirstOrDefault(f => f.Hashes.Sha1 == hash);
38-
38+
3939
Assert.That(file, Is.Not.Null);
4040
}
4141
}

Modrinth.Net/Endpoints/Endpoint.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using Modrinth.Http;
2+
3+
namespace Modrinth.Endpoints;
4+
5+
/// <summary>
6+
/// Base class for all endpoints
7+
/// </summary>
8+
public abstract class Endpoint
9+
{
10+
/// <summary>
11+
/// Creates a new <see cref="Endpoint" /> with the given <see cref="IRequester" />
12+
/// </summary>
13+
/// <param name="requester"> The <see cref="IRequester" /> to use for requests </param>
14+
protected Endpoint(IRequester requester)
15+
{
16+
Requester = requester;
17+
}
18+
19+
/// <summary>
20+
/// The requester used to make requests
21+
/// </summary>
22+
protected IRequester Requester { get; }
23+
}

Modrinth.Net/Endpoints/Miscellaneous/IMiscellaneousEndpoint.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace Modrinth.Endpoints.Miscellaneous;
44

5+
/// <summary>
6+
/// Miscellaneous endpoints
7+
/// </summary>
58
public interface IMiscellaneousEndpoint
69
{
710
/// <summary>

Modrinth.Net/Endpoints/Miscellaneous/MiscellaneousEndpoint.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22

33
namespace Modrinth.Endpoints.Miscellaneous;
44

5-
public class MiscellaneousEndpoint : IMiscellaneousEndpoint
5+
/// <inheritdoc cref="Modrinth.Endpoints.Miscellaneous.IMiscellaneousEndpoint" />
6+
public class MiscellaneousEndpoint : Endpoint, IMiscellaneousEndpoint
67
{
7-
private readonly IRequester _client;
8-
9-
public MiscellaneousEndpoint(IRequester client)
8+
/// <inheritdoc />
9+
public MiscellaneousEndpoint(IRequester requester) : base(requester)
1010
{
11-
_client = client;
1211
}
1312

1413
/// <inheritdoc />
@@ -18,6 +17,6 @@ public async Task<ModrinthStatistics> GetStatisticsAsync()
1817
reqMsg.Method = HttpMethod.Get;
1918
reqMsg.RequestUri = new Uri("statistics", UriKind.Relative);
2019

21-
return await _client.GetJsonAsync<ModrinthStatistics>(reqMsg).ConfigureAwait(false);
20+
return await Requester.GetJsonAsync<ModrinthStatistics>(reqMsg).ConfigureAwait(false);
2221
}
2322
}

Modrinth.Net/Endpoints/Miscellaneous/ModrinthStatistics.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
namespace Modrinth.Endpoints.Miscellaneous;
22

3+
/// <summary>
4+
/// Statistics about a Modrinth instance
5+
/// </summary>
36
public class ModrinthStatistics
47
{
58
/// <summary>

Modrinth.Net/Endpoints/Project/IProjectEndpoint.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
namespace Modrinth.Endpoints.Project;
66

7+
/// <summary>
8+
/// Project endpoints
9+
/// </summary>
710
public interface IProjectEndpoint
811
{
912
/// <summary>

Modrinth.Net/Endpoints/Project/ModifiedProject.cs

Lines changed: 0 additions & 48 deletions
This file was deleted.

Modrinth.Net/Endpoints/Project/ProjectEndpoint.cs

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@
66

77
namespace Modrinth.Endpoints.Project;
88

9-
public class ProjectEndpoint : IProjectEndpoint
9+
/// <inheritdoc cref="Modrinth.Endpoints.Project.IProjectEndpoint" />
10+
public class ProjectEndpoint : Endpoint, IProjectEndpoint
1011
{
1112
private const string ProjectPathSegment = "project";
1213

13-
private readonly IRequester _client;
14-
15-
public ProjectEndpoint(IRequester client)
14+
/// <inheritdoc />
15+
public ProjectEndpoint(IRequester requester) : base(requester)
1616
{
17-
_client = client;
1817
}
1918

2019
/// <inheritdoc />
@@ -24,7 +23,7 @@ public ProjectEndpoint(IRequester client)
2423
reqMsg.Method = HttpMethod.Get;
2524
reqMsg.RequestUri = new Uri(ProjectPathSegment + "/" + slugOrId, UriKind.Relative);
2625

27-
return await _client.GetJsonAsync<Models.Project>(reqMsg).ConfigureAwait(false);
26+
return await Requester.GetJsonAsync<Models.Project>(reqMsg).ConfigureAwait(false);
2827
}
2928

3029
/// <inheritdoc />
@@ -41,7 +40,7 @@ public ProjectEndpoint(IRequester client)
4140

4241
parameters.AddToRequest(reqMsg);
4342

44-
return await _client.GetJsonAsync<Models.Project[]>(reqMsg).ConfigureAwait(false);
43+
return await Requester.GetJsonAsync<Models.Project[]>(reqMsg).ConfigureAwait(false);
4544
}
4645

4746
/// <inheritdoc />
@@ -51,7 +50,7 @@ public async Task DeleteAsync(string slugOrId)
5150
reqMsg.Method = HttpMethod.Delete;
5251
reqMsg.RequestUri = new Uri(ProjectPathSegment + "/" + slugOrId, UriKind.Relative);
5352

54-
await _client.SendAsync(reqMsg).ConfigureAwait(false);
53+
await Requester.SendAsync(reqMsg).ConfigureAwait(false);
5554
}
5655

5756
/// <inheritdoc />
@@ -68,7 +67,7 @@ public async Task DeleteAsync(string slugOrId)
6867

6968
parameters.AddToRequest(reqMsg);
7069

71-
return await _client.GetJsonAsync<Models.Project[]>(reqMsg).ConfigureAwait(false);
70+
return await Requester.GetJsonAsync<Models.Project[]>(reqMsg).ConfigureAwait(false);
7271
}
7372

7473
/// <inheritdoc />
@@ -78,7 +77,7 @@ public async Task<SlugIdValidity> CheckIdSlugValidityAsync(string slugOrId)
7877
reqMsg.Method = HttpMethod.Get;
7978
reqMsg.RequestUri = new Uri(ProjectPathSegment + "/" + slugOrId + "/check", UriKind.Relative);
8079

81-
return await _client.GetJsonAsync<SlugIdValidity>(reqMsg).ConfigureAwait(false);
80+
return await Requester.GetJsonAsync<SlugIdValidity>(reqMsg).ConfigureAwait(false);
8281
}
8382

8483
/// <inheritdoc />
@@ -88,7 +87,7 @@ public async Task<Dependencies> GetDependenciesAsync(string slugOrId)
8887
reqMsg.Method = HttpMethod.Get;
8988
reqMsg.RequestUri = new Uri(ProjectPathSegment + "/" + slugOrId + "/dependencies", UriKind.Relative);
9089

91-
return await _client.GetJsonAsync<Dependencies>(reqMsg).ConfigureAwait(false);
90+
return await Requester.GetJsonAsync<Dependencies>(reqMsg).ConfigureAwait(false);
9291
}
9392

9493
/// <inheritdoc />
@@ -98,7 +97,7 @@ public async Task FollowAsync(string slugOrId)
9897
reqMsg.Method = HttpMethod.Post;
9998
reqMsg.RequestUri = new Uri(ProjectPathSegment + "/" + slugOrId + "/follow", UriKind.Relative);
10099

101-
await _client.SendAsync(reqMsg).ConfigureAwait(false);
100+
await Requester.SendAsync(reqMsg).ConfigureAwait(false);
102101
}
103102

104103
/// <inheritdoc />
@@ -108,7 +107,7 @@ public async Task UnfollowAsync(string slugOrId)
108107
reqMsg.Method = HttpMethod.Delete;
109108
reqMsg.RequestUri = new Uri(ProjectPathSegment + "/" + slugOrId + "/follow", UriKind.Relative);
110109

111-
await _client.SendAsync(reqMsg).ConfigureAwait(false);
110+
await Requester.SendAsync(reqMsg).ConfigureAwait(false);
112111
}
113112

114113
/// <inheritdoc />
@@ -118,7 +117,7 @@ public async Task DeleteIconAsync(string slugOrId)
118117
reqMsg.Method = HttpMethod.Delete;
119118
reqMsg.RequestUri = new Uri(ProjectPathSegment + "/" + slugOrId + "/icon", UriKind.Relative);
120119

121-
await _client.SendAsync(reqMsg).ConfigureAwait(false);
120+
await Requester.SendAsync(reqMsg).ConfigureAwait(false);
122121
}
123122

124123
/// <inheritdoc />
@@ -143,7 +142,7 @@ public async Task ChangeIconAsync(string slugOrId, string iconPath)
143142

144143
parameters.AddToRequest(reqMsg);
145144

146-
await _client.SendAsync(reqMsg).ConfigureAwait(false);
145+
await Requester.SendAsync(reqMsg).ConfigureAwait(false);
147146
}
148147

149148
/// <inheritdoc />
@@ -173,7 +172,7 @@ public async Task AddGalleryImageAsync(string slugOrId, string imagePath, bool f
173172

174173
reqMsg.Content = streamContent;
175174

176-
await _client.SendAsync(reqMsg).ConfigureAwait(false);
175+
await Requester.SendAsync(reqMsg).ConfigureAwait(false);
177176
}
178177

179178
/// <inheritdoc />
@@ -196,7 +195,7 @@ public async Task ModifyGalleryImageAsync(string slugOrId, string url, bool? fea
196195

197196
parameters.AddToRequest(reqMsg);
198197

199-
await _client.SendAsync(reqMsg).ConfigureAwait(false);
198+
await Requester.SendAsync(reqMsg).ConfigureAwait(false);
200199
}
201200

202201
/// <inheritdoc />
@@ -214,7 +213,7 @@ public async Task DeleteGalleryImageAsync(string slugOrId, string url)
214213

215214
parameters.AddToRequest(reqMsg);
216215

217-
await _client.SendAsync(reqMsg).ConfigureAwait(false);
216+
await Requester.SendAsync(reqMsg).ConfigureAwait(false);
218217
}
219218

220219
/// <inheritdoc />
@@ -237,6 +236,6 @@ public async Task<SearchResponse> SearchAsync(string query, Index index = Index.
237236

238237
parameters.AddToRequest(reqMsg);
239238

240-
return await _client.GetJsonAsync<SearchResponse>(reqMsg).ConfigureAwait(false);
239+
return await Requester.GetJsonAsync<SearchResponse>(reqMsg).ConfigureAwait(false);
241240
}
242241
}

0 commit comments

Comments
 (0)