-
Notifications
You must be signed in to change notification settings - Fork 6
feat: implement Version Create API #186
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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; | ||
|
|
||
|
|
@@ -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 | ||
| 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(); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's add (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) | ||
|
|
||
| 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() | ||
| }; | ||
| } | ||
| } |
| 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() | ||
| }; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,7 +12,7 @@ | |
| <PackageLicenseExpression>MIT</PackageLicenseExpression> | ||
| <GenerateDocumentationFile>true</GenerateDocumentationFile> | ||
| <PackageReadmeFile>README.md</PackageReadmeFile> | ||
| <PackageVersion>3.6.0</PackageVersion> | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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> | ||
|
|
||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
| 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); | ||
| } | ||
| } |
There was a problem hiding this comment.
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