Skip to content

Commit 18309d9

Browse files
committed
Refactor
1 parent 86db743 commit 18309d9

File tree

25 files changed

+210
-164
lines changed

25 files changed

+210
-164
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Licensed to Elasticsearch B.V under one or more agreements.
2+
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3+
// See the LICENSE file in the project root for more information
4+
5+
using Elastic.Documentation.Links;
6+
7+
namespace Elastic.Documentation.LinkIndex;
8+
9+
public interface ILinkIndexReader
10+
{
11+
Task<LinkRegistry> GetRegistry(Cancel cancellationToken = default);
12+
Task<RepositoryLinks> GetRepositoryLinks(string key, Cancel cancellationToken = default);
13+
string RegistryUrl { get; }
14+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Licensed to Elasticsearch B.V under one or more agreements.
2+
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3+
// See the LICENSE file in the project root for more information
4+
5+
namespace Elastic.Documentation.LinkIndex;
6+
7+
public interface ILinkIndexReaderWriter : ILinkIndexReader, ILinkIndexWriter;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Licensed to Elasticsearch B.V under one or more agreements.
2+
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3+
// See the LICENSE file in the project root for more information
4+
5+
using Elastic.Documentation.Links;
6+
7+
namespace Elastic.Documentation.LinkIndex;
8+
9+
public interface ILinkIndexWriter
10+
{
11+
Task SaveRegistry(LinkRegistry registry, Cancel cancellationToken = default);
12+
}

src/Elastic.Documentation.LinkIndex/LinkIndexProvider.cs

Lines changed: 0 additions & 84 deletions
This file was deleted.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Licensed to Elasticsearch B.V under one or more agreements.
2+
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3+
// See the LICENSE file in the project root for more information
4+
5+
using System.Net;
6+
using Amazon.Runtime;
7+
using Amazon.S3;
8+
using Amazon.S3.Model;
9+
using Elastic.Documentation.Links;
10+
11+
namespace Elastic.Documentation.LinkIndex;
12+
13+
public class Aws3LinkIndexReader(IAmazonS3 s3Client, string bucketName = "elastic-docs-link-index", string registryKey = "link-index.json") : ILinkIndexReader
14+
{
15+
16+
// <summary>
17+
// Using <see cref="AnonymousAWSCredentials"/> to access the link index
18+
// allows to read from the link index without the need to provide AWS credentials.
19+
// </summary>
20+
public static Aws3LinkIndexReader CreateAnonymous()
21+
{
22+
var credentials = new AnonymousAWSCredentials();
23+
var config = new AmazonS3Config
24+
{
25+
RegionEndpoint = Amazon.RegionEndpoint.USEast2
26+
};
27+
var s3Client = new AmazonS3Client(credentials, config);
28+
return new AwsS3LinkIndexReaderWriter(s3Client);
29+
}
30+
31+
public async Task<LinkRegistry> GetRegistry(Cancel cancellationToken = default)
32+
{
33+
var getObjectRequest = new GetObjectRequest
34+
{
35+
BucketName = bucketName,
36+
Key = registryKey
37+
};
38+
var getObjectResponse = await s3Client.GetObjectAsync(getObjectRequest, cancellationToken);
39+
await using var stream = getObjectResponse.ResponseStream;
40+
var linkIndex = LinkRegistry.Deserialize(stream);
41+
return linkIndex with { ETag = getObjectResponse.ETag };
42+
}
43+
public async Task<RepositoryLinks> GetRepositoryLinks(string key, Cancel cancellationToken)
44+
{
45+
var getObjectRequest = new GetObjectRequest
46+
{
47+
BucketName = bucketName,
48+
Key = key
49+
};
50+
var getObjectResponse = await s3Client.GetObjectAsync(getObjectRequest, cancellationToken);
51+
await using var stream = getObjectResponse.ResponseStream;
52+
return RepositoryLinks.Deserialize(stream);
53+
}
54+
55+
public string RegistryUrl { get; } = $"https://{bucketName}.s3.{s3Client.Config.RegionEndpoint.SystemName}.amazonaws.com/{registryKey}";
56+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Licensed to Elasticsearch B.V under one or more agreements.
2+
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3+
// See the LICENSE file in the project root for more information
4+
5+
using System.Net;
6+
using Amazon.Runtime;
7+
using Amazon.S3;
8+
using Amazon.S3.Model;
9+
using Elastic.Documentation.Links;
10+
11+
namespace Elastic.Documentation.LinkIndex;
12+
13+
public class AwsS3LinkIndexReaderWriter(
14+
IAmazonS3 s3Client,
15+
string bucketName = "elastic-docs-link-index",
16+
string registryKey = "link-index.json"
17+
) : Aws3LinkIndexReader(s3Client, bucketName, registryKey), ILinkIndexReaderWriter
18+
{
19+
private readonly IAmazonS3 _s3Client = s3Client;
20+
private readonly string _bucketName = bucketName;
21+
private readonly string _registryKey = registryKey;
22+
23+
public async Task SaveRegistry(LinkRegistry registry, Cancel cancellationToken = default)
24+
{
25+
if (registry.ETag == null)
26+
// The ETag should not be null if the LinkReferenceRegistry was retrieved from GetLinkIndex()
27+
throw new InvalidOperationException($"{nameof(LinkRegistry)}.{nameof(registry.ETag)} cannot be null");
28+
var json = LinkRegistry.Serialize(registry);
29+
var putObjectRequest = new PutObjectRequest
30+
{
31+
BucketName = _bucketName,
32+
Key = _registryKey,
33+
ContentBody = json,
34+
ContentType = "application/json",
35+
IfMatch = registry.ETag // Only update if the ETag matches. Meaning the object has not been changed in the meantime.
36+
};
37+
var putResponse = await _s3Client.PutObjectAsync(putObjectRequest, cancellationToken);
38+
if (putResponse.HttpStatusCode != HttpStatusCode.OK)
39+
throw new Exception($"Unable to save {nameof(LinkRegistry)} to s3://{_bucketName}/{_registryKey}");
40+
}
41+
}

src/Elastic.Documentation/Links/LinkReferenceRegistry.cs renamed to src/Elastic.Documentation/Links/LinkRegistry.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
namespace Elastic.Documentation.Links;
1010

11-
public record LinkReferenceRegistry
11+
public record LinkRegistry
1212
{
1313
/// Map of branch to <see cref="LinkRegistryEntry"/>
1414
[JsonPropertyName("repositories")]
@@ -17,7 +17,7 @@ public record LinkReferenceRegistry
1717
[JsonIgnore]
1818
public string? ETag { get; init; }
1919

20-
public LinkReferenceRegistry WithLinkRegistryEntry(LinkRegistryEntry entry)
20+
public LinkRegistry WithLinkRegistryEntry(LinkRegistryEntry entry)
2121
{
2222
var copiedRepositories = new Dictionary<string, Dictionary<string, LinkRegistryEntry>>(Repositories);
2323
var repository = entry.Repository;
@@ -48,14 +48,14 @@ public LinkReferenceRegistry WithLinkRegistryEntry(LinkRegistryEntry entry)
4848
return this with { Repositories = copiedRepositories };
4949
}
5050

51-
public static LinkReferenceRegistry Deserialize(Stream json) =>
52-
JsonSerializer.Deserialize(json, SourceGenerationContext.Default.LinkReferenceRegistry)!;
51+
public static LinkRegistry Deserialize(Stream json) =>
52+
JsonSerializer.Deserialize(json, SourceGenerationContext.Default.LinkRegistry)!;
5353

54-
public static LinkReferenceRegistry Deserialize(string json) =>
55-
JsonSerializer.Deserialize(json, SourceGenerationContext.Default.LinkReferenceRegistry)!;
54+
public static LinkRegistry Deserialize(string json) =>
55+
JsonSerializer.Deserialize(json, SourceGenerationContext.Default.LinkRegistry)!;
5656

57-
public static string Serialize(LinkReferenceRegistry referenceRegistry) =>
58-
JsonSerializer.Serialize(referenceRegistry, SourceGenerationContext.Default.LinkReferenceRegistry);
57+
public static string Serialize(LinkRegistry registry) =>
58+
JsonSerializer.Serialize(registry, SourceGenerationContext.Default.LinkRegistry);
5959
}
6060

6161
public record LinkRegistryEntry

src/Elastic.Documentation/Links/LinkReference.cs renamed to src/Elastic.Documentation/Links/RepositoryLinks.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public record LinkRedirect : LinkSingleRedirect
3939
public LinkSingleRedirect[]? Many { get; init; }
4040
}
4141

42-
public record LinkReference
42+
public record RepositoryLinks
4343
{
4444
[JsonPropertyName("origin")]
4545
public required GitCheckoutInformation Origin { get; init; }
@@ -61,12 +61,12 @@ public record LinkReference
6161
public static string SerializeRedirects(Dictionary<string, LinkRedirect>? redirects) =>
6262
JsonSerializer.Serialize(redirects, SourceGenerationContext.Default.DictionaryStringLinkRedirect);
6363

64-
public static LinkReference Deserialize(Stream json) =>
65-
JsonSerializer.Deserialize(json, SourceGenerationContext.Default.LinkReference)!;
64+
public static RepositoryLinks Deserialize(Stream json) =>
65+
JsonSerializer.Deserialize(json, SourceGenerationContext.Default.RepositoryLinks)!;
6666

67-
public static LinkReference Deserialize(string json) =>
68-
JsonSerializer.Deserialize(json, SourceGenerationContext.Default.LinkReference)!;
67+
public static RepositoryLinks Deserialize(string json) =>
68+
JsonSerializer.Deserialize(json, SourceGenerationContext.Default.RepositoryLinks)!;
6969

70-
public static string Serialize(LinkReference reference) =>
71-
JsonSerializer.Serialize(reference, SourceGenerationContext.Default.LinkReference);
70+
public static string Serialize(RepositoryLinks reference) =>
71+
JsonSerializer.Serialize(reference, SourceGenerationContext.Default.RepositoryLinks);
7272
}

src/Elastic.Documentation/Serialization/SourceGenerationContext.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ namespace Elastic.Documentation.Serialization;
1212

1313
[JsonSourceGenerationOptions(WriteIndented = true, UseStringEnumConverter = true)]
1414
[JsonSerializable(typeof(GenerationState))]
15-
[JsonSerializable(typeof(LinkReference))]
15+
[JsonSerializable(typeof(RepositoryLinks))]
1616
[JsonSerializable(typeof(GitCheckoutInformation))]
17-
[JsonSerializable(typeof(LinkReferenceRegistry))]
17+
[JsonSerializable(typeof(LinkRegistry))]
1818
[JsonSerializable(typeof(LinkRegistryEntry))]
1919
public sealed partial class SourceGenerationContext : JsonSerializerContext;

src/Elastic.Markdown/DocumentationGenerator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,11 +276,11 @@ private bool CompilationNotNeeded(GenerationState? generationState, out HashSet<
276276
return false;
277277
}
278278

279-
private async Task<LinkReference> GenerateLinkReference(Cancel ctx)
279+
private async Task<RepositoryLinks> GenerateLinkReference(Cancel ctx)
280280
{
281281
var file = DocumentationSet.LinkReferenceFile;
282282
var state = DocumentationSet.CreateLinkReference();
283-
var bytes = JsonSerializer.SerializeToUtf8Bytes(state, SourceGenerationContext.Default.LinkReference);
283+
var bytes = JsonSerializer.SerializeToUtf8Bytes(state, SourceGenerationContext.Default.RepositoryLinks);
284284
await DocumentationSet.OutputDirectory.FileSystem.File.WriteAllBytesAsync(file.FullName, bytes, ctx);
285285
return state;
286286
}

0 commit comments

Comments
 (0)