1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
// Copyright 2022 Jamie Taylor
using System;
using System.Diagnostics.CodeAnalysis;
using System.Net;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using Pathoschild.Http.Client;
using StardewModdingAPI.Toolkit;
using StardewModdingAPI.Toolkit.Framework.UpdateData;
using StardewModdingAPI.Web.Framework.Clients.UpdateManifest.ResponseModels;
namespace StardewModdingAPI.Web.Framework.Clients.UpdateManifest
{
/// <summary>An API client for fetching update metadata from an arbitrary JSON URL.</summary>
internal class UpdateManifestClient : IUpdateManifestClient
{
/*********
** Fields
*********/
/// <summary>The underlying HTTP client.</summary>
private readonly IClient Client;
/*********
** Accessors
*********/
/// <summary>The unique key for the mod site.</summary>
public ModSiteKey SiteKey => ModSiteKey.UpdateManifest;
/*********
** Public methods
*********/
/// <summary>Construct an instance.</summary>
/// <param name="userAgent">The user agent for the API client.</param>
public UpdateManifestClient(string userAgent)
{
this.Client = new FluentClient()
.SetUserAgent(userAgent);
this.Client.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/plain"));
}
/// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
public void Dispose()
{
this.Client.Dispose();
}
/// <inheritdoc/>
[SuppressMessage("ReSharper", "ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract", Justification = "This is the method which ensures the annotations are correct.")]
public async Task<IModPage?> GetModData(string id)
{
// get raw update manifest
UpdateManifestModel? manifest;
try
{
manifest = await this.Client.GetAsync(id).As<UpdateManifestModel?>();
if (manifest is null)
return new GenericModPage(this.SiteKey, id).SetError(RemoteModStatus.InvalidData, $"The update manifest at {id} is empty");
}
catch (ApiException ex) when (ex.Status == HttpStatusCode.NotFound)
{
return new GenericModPage(this.SiteKey, id).SetError(RemoteModStatus.DoesNotExist, $"No update manifest found at {id}");
}
catch (Exception ex)
{
return new GenericModPage(this.SiteKey, id).SetError(RemoteModStatus.InvalidData, $"The update manifest at {id} has an invalid format: {ex.Message}");
}
// validate
if (!SemanticVersion.TryParse(manifest.Format, out _))
return new GenericModPage(this.SiteKey, id).SetError(RemoteModStatus.InvalidData, $"The update manifest at {id} has invalid format version '{manifest.Format}'");
foreach ((string modKey, UpdateManifestModModel mod) in manifest.Mods)
{
if (string.IsNullOrWhiteSpace(mod.ModPageUrl))
{
foreach (UpdateManifestVersionModel download in mod.Versions)
{
if (string.IsNullOrWhiteSpace(download.ModPageUrl))
return new GenericModPage(this.SiteKey, id).SetError(RemoteModStatus.InvalidData, $"The update manifest at {id} is invalid (all mod downloads must have a mod page URL)");
}
}
}
// build model
return new UpdateManifestModPage(id, manifest);
}
}
}
|