Skip to content

Commit 9737281

Browse files
authored
Merge pull request #10 from Zechiax/feature_urlMethods
Feature url methods
2 parents 17cc2c3 + 075e56c commit 9737281

File tree

9 files changed

+131
-5
lines changed

9 files changed

+131
-5
lines changed

Modrinth.RestClient.Test/Modrinth.RestClient.Test.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
</PropertyGroup>
1010

1111
<ItemGroup>
12-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.1" />
12+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
1313
<PackageReference Include="NUnit" Version="3.13.3" />
1414
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
1515
<PackageReference Include="NUnit.Analyzers" Version="3.3.0" />
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using Modrinth.RestClient.Models;
2+
using Version = Modrinth.RestClient.Models.Version;
3+
4+
namespace Modrinth.RestClient.Extensions;
5+
6+
public static class ProjectExtensions
7+
{
8+
/// <summary>
9+
/// Returns a direct link to version details of specific version
10+
/// </summary>
11+
/// <param name="project"></param>
12+
/// <param name="version">Version for which the URL should be returned</param>
13+
/// <returns></returns>
14+
public static string GetVersionUrl(this Project project, Version version)
15+
{
16+
return project.GetVersionUrl(version.Id);
17+
}
18+
19+
/// <summary>
20+
/// Returns a direct link to version details of specific version
21+
/// </summary>
22+
/// <param name="project"></param>
23+
/// <param name="versionId">ID of the version</param>
24+
/// <returns></returns>
25+
public static string GetVersionUrl(this Project project, string versionId)
26+
{
27+
return $"{project.Url}/version/{versionId}";
28+
}
29+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using Modrinth.RestClient.Models;
2+
using Version = Modrinth.RestClient.Models.Version;
3+
4+
namespace Modrinth.RestClient.Extensions;
5+
6+
public static class VersionExtensions
7+
{
8+
/// <summary>
9+
/// Returns a direct link to this version details
10+
/// </summary>
11+
/// <param name="version"></param>
12+
/// <param name="project">The project to which this version belongs</param>
13+
/// <returns></returns>
14+
public static string GetUrl(this Version version, Project project)
15+
{
16+
return project.GetVersionUrl(version);
17+
}
18+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using Modrinth.RestClient.Models;
2+
using Modrinth.RestClient.Models.Enums;
3+
4+
namespace Modrinth.RestClient.Helpers;
5+
6+
/// <summary>
7+
/// Class for creating direct links to Modrinth
8+
/// </summary>
9+
public static class UrlCreatorHelper
10+
{
11+
/// <summary>
12+
/// Url of the normal Modrinth web
13+
/// </summary>
14+
public const string ModrinthUrl = "https://modrinth.com";
15+
16+
/// <summary>
17+
/// Returns formatted type used in Modrinth links to specific project
18+
/// </summary>
19+
/// <param name="projectType"></param>
20+
/// <returns></returns>
21+
private static string GetProjectUrlType(ProjectType projectType)
22+
{
23+
return projectType switch
24+
{
25+
ProjectType.Mod => "mod",
26+
ProjectType.Modpack => "modpack",
27+
ProjectType.Resourcepack => "resourcepack",
28+
// Return lower string, this should work for all, but it is not guaranteed
29+
_ => projectType.ToString().ToLower()
30+
};
31+
}
32+
33+
/// <summary>
34+
/// Return direct link to the user on Modrinth
35+
/// </summary>
36+
/// <param name="project"></param>
37+
/// <returns></returns>
38+
public static string GetDirectUrl(this Project project)
39+
{
40+
return $"{ModrinthUrl}/{GetProjectUrlType(project.ProjectType)}/{project.Id}";
41+
}
42+
43+
/// <summary>
44+
/// Return direct link to the user on Modrinth
45+
/// </summary>
46+
/// <param name="user"></param>
47+
/// <returns></returns>
48+
public static string GetDirectUrl(this User user)
49+
{
50+
return $"{ModrinthUrl}/user/{user.Id}";
51+
}
52+
53+
/// <summary>
54+
/// Return direct link to the project of this search result on Modrinth
55+
/// </summary>
56+
/// <param name="searchResult"></param>
57+
/// <returns></returns>
58+
public static string GetDirectUrl(this SearchResult searchResult)
59+
{
60+
return $"{ModrinthUrl}/{GetProjectUrlType(searchResult.ProjectType)}/{searchResult.ProjectId}";
61+
}
62+
}

Modrinth.RestClient/Models/Project.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
#pragma warning disable CS8618
2+
using Modrinth.RestClient.Helpers;
23
using Modrinth.RestClient.Models.Enums;
34
using Newtonsoft.Json;
45

56
namespace Modrinth.RestClient.Models;
67

78
public class Project
89
{
10+
/// <summary>
11+
/// A direct link to this project
12+
/// </summary>
13+
public string Url => this.GetDirectUrl();
14+
915
/// <summary>
1016
/// The slug of a project, used for vanity URLs
1117
/// </summary>

Modrinth.RestClient/Models/SearchResult.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#pragma warning disable CS8618
2+
using Modrinth.RestClient.Helpers;
23
using Modrinth.RestClient.Models.Enums;
34
using Newtonsoft.Json;
45

@@ -9,6 +10,11 @@ namespace Modrinth.RestClient.Models;
910
/// </summary>
1011
public class SearchResult
1112
{
13+
/// <summary>
14+
/// A direct link to the project of this search result
15+
/// </summary>
16+
public string Url => this.GetDirectUrl();
17+
1218
/// <summary>
1319
/// The slug of a project, used for vanity URLs
1420
/// </summary>

Modrinth.RestClient/Models/User.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
#pragma warning disable CS8618
2+
using Modrinth.RestClient.Helpers;
23
using Modrinth.RestClient.Models.Enums;
34
using Newtonsoft.Json;
45

56
namespace Modrinth.RestClient.Models;
67

78
public class User
89
{
10+
/// <summary>
11+
/// A direct link to this user
12+
/// </summary>
13+
public string Url => this.GetDirectUrl();
14+
915
/// <summary>
1016
/// The user's username
1117
/// </summary>

Modrinth.RestClient/Modrinth.RestClient.csproj

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,11 @@
99
<RepositoryUrl>https://github.com/Zechiax/Modrinth.RestClient</RepositoryUrl>
1010
<PackageTags>modrinth;api;labrinth;restease</PackageTags>
1111
<Authors>Zechiax</Authors>
12-
<PackageReleaseNotes>- Allow index in search
13-
- Fix getting multiple ids with only 1 provided
14-
- Rename method for getting user's projects</PackageReleaseNotes>
12+
<PackageReleaseNotes>- Added URL properties to Project, User, SearchResult and extension to help create version URL</PackageReleaseNotes>
1513
<PackageLicenseExpression>MIT</PackageLicenseExpression>
1614
<GenerateDocumentationFile>true</GenerateDocumentationFile>
1715
<PackageReadmeFile>README.md</PackageReadmeFile>
18-
<PackageVersion>2.4.0</PackageVersion>
16+
<PackageVersion>2.5.0</PackageVersion>
1917
</PropertyGroup>
2018

2119
<ItemGroup>

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ Console.WriteLine(project.Description);
1919
- As of right now, this does not cover every endpoint, but rather most of the common `GET` endpoints
2020
- So it can only get information, not upload
2121
- For API specification [see here](https://docs.modrinth.com/api-spec/)
22+
- For getting response status code, which do not indicate success, [see here](https://github.com/canton7/RestEase#response-status-codes)

0 commit comments

Comments
 (0)