Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 7412fcf

Browse files
committedFeb 5, 2025··
Add API request & response structures for beatmap submission
1 parent aaffd72 commit 7412fcf

5 files changed

+235
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
2+
// See the LICENCE file in the repository root for full licence text.
3+
4+
using System.Diagnostics;
5+
using osu.Framework.IO.Network;
6+
7+
namespace osu.Game.Online.API.Requests
8+
{
9+
public abstract class APIUploadRequest : APIRequest
10+
{
11+
protected override WebRequest CreateWebRequest()
12+
{
13+
var request = base.CreateWebRequest();
14+
request.UploadProgress += onUploadProgress;
15+
return request;
16+
}
17+
18+
private void onUploadProgress(long current, long total)
19+
{
20+
Debug.Assert(API != null);
21+
API.Schedule(() => Progressed?.Invoke(current, total));
22+
}
23+
24+
public event APIProgressHandler? Progressed;
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
2+
// See the LICENCE file in the repository root for full licence text.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Net.Http;
7+
using osu.Framework.IO.Network;
8+
9+
namespace osu.Game.Online.API.Requests
10+
{
11+
public class PatchBeatmapPackageRequest : APIUploadRequest
12+
{
13+
protected override string Uri
14+
{
15+
get
16+
{
17+
// can be removed once the service has been successfully deployed to production
18+
if (API!.EndpointConfiguration.BeatmapSubmissionServiceUrl == null)
19+
throw new NotSupportedException("Beatmap submission not supported in this configuration!");
20+
21+
return $@"{API!.EndpointConfiguration.BeatmapSubmissionServiceUrl!}/beatmapsets/{BeatmapSetID}";
22+
}
23+
}
24+
25+
protected override string Target => throw new NotSupportedException();
26+
27+
public uint BeatmapSetID { get; }
28+
public Dictionary<string, byte[]> FilesChanged { get; } = new Dictionary<string, byte[]>();
29+
public HashSet<string> FilesDeleted { get; } = new HashSet<string>();
30+
31+
public PatchBeatmapPackageRequest(uint beatmapSetId)
32+
{
33+
BeatmapSetID = beatmapSetId;
34+
}
35+
36+
protected override WebRequest CreateWebRequest()
37+
{
38+
var request = base.CreateWebRequest();
39+
request.Method = HttpMethod.Patch;
40+
41+
foreach ((string filename, byte[] content) in FilesChanged)
42+
request.AddFile(@"filesChanged", content, filename);
43+
44+
foreach (string filename in FilesDeleted)
45+
request.AddParameter(@"filesDeleted", filename, RequestParameterType.Form);
46+
47+
request.Timeout = 60_000;
48+
return request;
49+
}
50+
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
2+
// See the LICENCE file in the repository root for full licence text.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Net.Http;
8+
using Newtonsoft.Json;
9+
using Newtonsoft.Json.Converters;
10+
using osu.Framework.IO.Network;
11+
using osu.Framework.Localisation;
12+
using osu.Game.Localisation;
13+
using osu.Game.Online.API.Requests.Responses;
14+
using osu.Game.Screens.Edit.Submission;
15+
16+
namespace osu.Game.Online.API.Requests
17+
{
18+
public class PutBeatmapSetRequest : APIRequest<PutBeatmapSetResponse>
19+
{
20+
protected override string Uri
21+
{
22+
get
23+
{
24+
// can be removed once the service has been successfully deployed to production
25+
if (API!.EndpointConfiguration.BeatmapSubmissionServiceUrl == null)
26+
throw new NotSupportedException("Beatmap submission not supported in this configuration!");
27+
28+
return $@"{API!.EndpointConfiguration.BeatmapSubmissionServiceUrl}/beatmapsets";
29+
}
30+
}
31+
32+
protected override string Target => throw new NotSupportedException();
33+
34+
[JsonProperty("beatmapset_id")]
35+
public uint? BeatmapSetID { get; init; }
36+
37+
[JsonProperty("beatmaps_to_create")]
38+
public uint BeatmapsToCreate { get; init; }
39+
40+
[JsonProperty("beatmaps_to_keep")]
41+
public uint[] BeatmapsToKeep { get; init; } = [];
42+
43+
[JsonProperty("target")]
44+
public BeatmapSubmissionTarget SubmissionTarget { get; init; }
45+
46+
private PutBeatmapSetRequest()
47+
{
48+
}
49+
50+
public static PutBeatmapSetRequest CreateNew(uint beatmapCount, BeatmapSubmissionTarget target) => new PutBeatmapSetRequest
51+
{
52+
BeatmapsToCreate = beatmapCount,
53+
SubmissionTarget = target,
54+
};
55+
56+
public static PutBeatmapSetRequest UpdateExisting(uint beatmapSetId, IEnumerable<uint> beatmapsToKeep, uint beatmapsToCreate, BeatmapSubmissionTarget target) => new PutBeatmapSetRequest
57+
{
58+
BeatmapSetID = beatmapSetId,
59+
BeatmapsToKeep = beatmapsToKeep.ToArray(),
60+
BeatmapsToCreate = beatmapsToCreate,
61+
SubmissionTarget = target,
62+
};
63+
64+
protected override WebRequest CreateWebRequest()
65+
{
66+
var req = base.CreateWebRequest();
67+
req.Method = HttpMethod.Put;
68+
req.ContentType = @"application/json";
69+
req.AddRaw(JsonConvert.SerializeObject(this));
70+
return req;
71+
}
72+
}
73+
74+
[JsonConverter(typeof(StringEnumConverter))]
75+
public enum BeatmapSubmissionTarget
76+
{
77+
[LocalisableDescription(typeof(BeatmapSubmissionStrings), nameof(BeatmapSubmissionStrings.BeatmapSubmissionTargetWIP))]
78+
WIP,
79+
80+
[LocalisableDescription(typeof(BeatmapSubmissionStrings), nameof(BeatmapSubmissionStrings.BeatmapSubmissionTargetPending))]
81+
Pending,
82+
}
83+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
2+
// See the LICENCE file in the repository root for full licence text.
3+
4+
using System;
5+
using System.Net.Http;
6+
using osu.Framework.IO.Network;
7+
8+
namespace osu.Game.Online.API.Requests
9+
{
10+
public class ReplaceBeatmapPackageRequest : APIUploadRequest
11+
{
12+
protected override string Uri
13+
{
14+
get
15+
{
16+
// can be removed once the service has been successfully deployed to production
17+
if (API!.EndpointConfiguration.BeatmapSubmissionServiceUrl == null)
18+
throw new NotSupportedException("Beatmap submission not supported in this configuration!");
19+
20+
return $@"{API!.EndpointConfiguration.BeatmapSubmissionServiceUrl}/beatmapsets/{BeatmapSetID}";
21+
}
22+
}
23+
24+
protected override string Target => throw new NotSupportedException();
25+
26+
public uint BeatmapSetID { get; }
27+
28+
private readonly byte[] oszPackage;
29+
30+
public ReplaceBeatmapPackageRequest(uint beatmapSetID, byte[] oszPackage)
31+
{
32+
this.oszPackage = oszPackage;
33+
BeatmapSetID = beatmapSetID;
34+
}
35+
36+
protected override WebRequest CreateWebRequest()
37+
{
38+
var request = base.CreateWebRequest();
39+
request.AddFile(@"beatmapArchive", oszPackage);
40+
request.Method = HttpMethod.Put;
41+
request.Timeout = 60_000;
42+
return request;
43+
}
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
2+
// See the LICENCE file in the repository root for full licence text.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using Newtonsoft.Json;
7+
8+
namespace osu.Game.Online.API.Requests.Responses
9+
{
10+
public class PutBeatmapSetResponse
11+
{
12+
[JsonProperty("beatmapset_id")]
13+
public uint BeatmapSetId { get; set; }
14+
15+
[JsonProperty("beatmap_ids")]
16+
public ICollection<uint> BeatmapIds { get; set; } = Array.Empty<uint>();
17+
18+
[JsonProperty("files")]
19+
public ICollection<BeatmapSetFile> Files { get; set; } = Array.Empty<BeatmapSetFile>();
20+
}
21+
22+
public struct BeatmapSetFile
23+
{
24+
[JsonProperty("filename")]
25+
public string Filename { get; set; }
26+
27+
[JsonProperty("sha2_hash")]
28+
public string SHA2Hash { get; set; }
29+
}
30+
}

0 commit comments

Comments
 (0)
Please sign in to comment.