Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ catch (ModrinthApiException e)
| Modify a version | PATCH | ❌ |
| Delete a version | DELETE | ⚠️ |
| Get a version given a version number or ID | GET | ❌ |
| Create a version | POST | |
| Create a version | POST | |
| Schedule a version | POST | ⚠️ |
| Get multiple versions | GET | ✅ |
| Add files to version | POST | ❌ |
Expand Down
27 changes: 27 additions & 0 deletions src/Modrinth.Net/Endpoints/Version/IVersionEndpoint.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Modrinth.Exceptions;
using Modrinth.Models;
using Modrinth.Models.Enums.Project;
using Modrinth.Models.Enums.Version;

namespace Modrinth.Endpoints.Version;
Expand Down Expand Up @@ -53,6 +55,31 @@ public interface IVersionEndpoint
Task<Models.Version> GetByVersionNumberAsync(string slugOrId, string versionNumber,
CancellationToken cancellationToken = default);

/// <summary>
/// Creates a version
/// </summary>
/// <param name="projectId">Project ID to create the version on (does not support slugs)</param>
/// <param name="files">List of files to add to this version (must include atleast 1 file)</param>
/// <param name="primaryFile">Primary (featured) filename</param>
/// <param name="name">Version name</param>
/// <param name="versionNumber">Version number</param>
/// <param name="changelog">Changelog</param>
/// <param name="dependencies">Modrinth project dependencies</param>
/// <param name="gameVersions">Supported Minecraft versions</param>
/// <param name="versionType">Version type</param>
/// <param name="loaders">Supported Minecraft modloaders</param>
/// <param name="featured">Whether to feature it on the mod page</param>
/// <param name="status">Unknown, but the mod seemingly doesn't show up if this is not VersionStatus.Listed.</param>
/// <param name="requestedStatus">Unknown</param>
/// <param name="cancellationToken"></param>
/// <returns>The newly created Version</returns>
/// <exception cref="ModrinthApiException"> Thrown when the API returns an error or the request fails </exception>
Task<Models.Version> CreateAsync(string projectId, IEnumerable<UploadableFile> files, string primaryFile, string name, string versionNumber,
string? changelog, IEnumerable<Dependency> dependencies, IEnumerable<string> gameVersions,
ProjectVersionType versionType, IEnumerable<string> loaders, bool featured,
VersionStatus status, VersionStatus? requestedStatus,
CancellationToken cancellationToken = default);

/// <summary>
/// Deletes a version by its ID
/// </summary>
Expand Down
61 changes: 60 additions & 1 deletion src/Modrinth.Net/Endpoints/Version/VersionEndpoint.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
using Modrinth.Extensions;
using System.Text;
using System.Text.Json;
using Modrinth.Endpoints.Project;
using Modrinth.Extensions;
using Modrinth.Helpers;
using Modrinth.Http;
using Modrinth.Models;
using Modrinth.Models.Enums.Project;
using Modrinth.Models.Enums.Version;
using File = System.IO.File;

namespace Modrinth.Endpoints.Version;

Expand All @@ -25,6 +31,59 @@ public VersionEndpoint(IRequester requester, ModrinthClientConfig config) : base
return await Requester.GetJsonAsync<Models.Version>(reqMsg, cancellationToken).ConfigureAwait(false);
}

/// <inheritdoc />
public async Task<Models.Version> CreateAsync(string projectId, IEnumerable<UploadableFile> files, string primaryFile, string name, string versionNumber,
string? changelog, IEnumerable<Dependency> dependencies, IEnumerable<string> gameVersions,
ProjectVersionType versionType, IEnumerable<string> loaders, bool featured,
VersionStatus status, VersionStatus? requestedStatus,
CancellationToken cancellationToken = default) {
// todo this is really messy, imo should do builder or maybe take struct/class directly and have serializer function in it
var reqMsg = new HttpRequestMessage();
reqMsg.Method = HttpMethod.Post;
reqMsg.RequestUri = new Uri(VersionsPath, UriKind.Relative);

// transform for web
// might be better to make some kind of base class like ModrinthApiSerializer and ModrinthApiDeserializer
// and then extend those
Comment on lines +46 to +47

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tracking in #196, please remove the comments, thanks

var deps = dependencies.Select(d => new
{
version_id = d.VersionId,
project_id = d.ProjectId,
file_name = d.FileName,
dependency_type = d.DependencyType
}).ToList();

// data
var uploadableFiles = files as UploadableFile[] ?? files.ToArray();
string j = JsonSerializer.Serialize(new
{
name = name,
version_number = versionNumber,
changelog = changelog,
dependencies = deps,
game_versions = gameVersions,
version_type = versionType.ToModrinthString(),
loaders = loaders,
featured = featured,
status = status.ToModrinthString(),
requested_status = requestedStatus?.ToModrinthString(),
project_id = projectId,
file_parts = uploadableFiles.Select(f => f.FileName),
primary_file = primaryFile
});

MultipartFormDataContent c = new();

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add using to the MultipartFormDataContent variable so that it correctly disposes of the Streams

(this is bad in the whole codebase, I need to go through my code and fix it later)

c.Add(new StringContent(j, Encoding.UTF8, "application/json"), "data");
foreach (UploadableFile file in uploadableFiles)
{
c.Add(new StreamContent(file.Stream), file.FileName, file.FileName);
}

reqMsg.Content = c;

return await Requester.GetJsonAsync<Models.Version>(reqMsg, cancellationToken).ConfigureAwait(false);
}

/// <inheritdoc />
public async Task<Models.Version[]> GetProjectVersionListAsync(string slugOrId, string[]? loaders = null,
string[]? gameVersions = null, bool? featured = null, CancellationToken cancellationToken = default)
Expand Down
26 changes: 26 additions & 0 deletions src/Modrinth.Net/Extensions/ProjectVersionTypeExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Modrinth.Models.Enums.Project;

namespace Modrinth.Extensions;

/// <summary>
/// Extensions for <see cref="ProjectVersionType" />
/// </summary>
public static class ProjectVersionTypeExtensions
{
/// <summary>
/// Converts ProjectVersionType to a string fit for the Modrinth API
/// </summary>
/// <param name="projectVersionType"></param>
/// <returns></returns>
public static string ToModrinthString(this ProjectVersionType projectVersionType)
{
return projectVersionType switch
{
ProjectVersionType.Alpha => "alpha",
ProjectVersionType.Beta => "beta",
ProjectVersionType.Release => "release",
// Return lower string, this should work for all, but it is not guaranteed
_ => projectVersionType.ToString().ToLower()
};
}
}
30 changes: 30 additions & 0 deletions src/Modrinth.Net/Extensions/VersionStatusExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Modrinth.Models.Enums.Project;
using Modrinth.Models.Enums.Version;

namespace Modrinth.Extensions;

/// <summary>
/// Extensions for <see cref="VersionStatus" />
/// </summary>
public static class VersionStatusExtensions
{
/// <summary>
/// Converts VersionStatus to a string fit for the Modrinth API
/// </summary>
/// <param name="versionStatus"></param>
/// <returns></returns>
public static string ToModrinthString(this VersionStatus versionStatus)
{
return versionStatus switch
{
VersionStatus.Archived => "archived",
VersionStatus.Draft => "draft",
VersionStatus.Listed => "listed",
VersionStatus.Scheduled => "scheduled",
VersionStatus.Unknown => "unknown",
VersionStatus.Unlisted => "unlisted",
// Return lower string, this should work for all, but it is not guaranteed
_ => versionStatus.ToString().ToLower()
};
}
}
2 changes: 1 addition & 1 deletion src/Modrinth.Net/Modrinth.Net.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageVersion>3.6.0</PackageVersion>

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please revert the version bump?

It's best to leave the it to the maintainers, there is an action that automatically releases new package after the bump, but if I were to publish a version before you PR I would need to update it etc. but I will definitely be release a new version after this PR!

<PackageVersion>3.6.1</PackageVersion>
<Version>$(PackageVersion)</Version>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>Modrinth</RootNamespace>
Expand Down
42 changes: 42 additions & 0 deletions src/Modrinth.Net/UploadableFile.cs

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UploadableFile should implement a IDisposable, if user passes a Stream, he would probably be aware of these resources, but if using the constructor using the file path, it instantly creates the stream and opens the file for read.

Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
namespace Modrinth;

// where do I put this???
/// <summary>
/// Wraps a stream with a filename, used for uploading files to Modrinth.
/// </summary>
public class UploadableFile
{
/// <summary>
/// The name of our file
/// </summary>
public string FileName { get; }
/// <summary>
/// The stream to access our file data
/// </summary>
public Stream Stream { get; }

/// <summary>
/// Creates a new Uploadable file by wrapping a stream with a filename
/// </summary>
/// <param name="fileName">The filename to use when uploading to Modrinth</param>
/// <param name="stream">The stream containing file data to upload</param>
public UploadableFile(string fileName, Stream stream)
{
FileName = fileName;
Stream = stream;
}

/// <summary>
/// Creates a new Uploadable file by opening a FileStream with the provided path
/// </summary>
/// <param name="path">The file path to open a stream with</param>
public UploadableFile(string path)
{
FileName = Path.GetFileName(path);

if (!File.Exists(path))
throw new FileNotFoundException();

Stream = new FileStream(path, FileMode.Open);
}
}