-
Notifications
You must be signed in to change notification settings - Fork 51
/
ServiceMetadata.cs
98 lines (87 loc) · 4.65 KB
/
ServiceMetadata.cs
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
92
93
94
95
96
97
98
/*
* Copyright 2022 Google LLC
* Use of this source code is governed by a BSD-style
* license that can be found in the LICENSE file or at
* https://developers.google.com/open-source/licenses/bsd
*/
using Google.Protobuf.Reflection;
using System.Collections.Generic;
using System.Linq;
namespace Google.Api.Gax.Grpc
{
/// <summary>
/// Provides metadata about a single service within an API.
/// Often most of these aspects will be the same across multiple services,
/// but they can be specified with different values in the original proto, so
/// they are specified individually here. This class is expected to be constructed
/// with a single instance per service; equality is by simple identity.
/// </summary>
public sealed class ServiceMetadata
{
internal const string DefaultUniverseDomain = "googleapis.com";
/// <summary>
/// The protobuf service descriptor for this service. This is never null.
/// </summary>
public ServiceDescriptor ServiceDescriptor { get; }
/// <summary>
/// The name of the service within the API, e.g. "Subscriber". This is never null or empty.
/// </summary>
public string Name { get; }
/// <summary>
/// The default endpoint for the service. This may be null, if a service has no default endpoint.
/// </summary>
/// <remarks>
/// The default endpoint is an endpoint in the default universe domain.
/// </remarks>
public string DefaultEndpoint { get; }
/// <summary>
/// The template to build and endpoint for the service taking into account a custom universe domain,
/// for instance "storage.{0}".
/// May be null, in which case no universe domain dependent endpoint may be built for the service.
/// </summary>
public string EndpointTemplate { get; }
/// <summary>
/// The default scopes for the service. This will never be null, but may be empty.
/// This will never contain any null references.
/// This will never change after construction.
/// </summary>
public IReadOnlyList<string> DefaultScopes { get; }
/// <summary>
/// Whether this service supports scoped JWT access (in which case
/// this is preferred by default over OAuth tokens).
/// </summary>
public bool SupportsScopedJwts { get; }
/// <summary>
/// The metadata for the API this is part of. This is never null.
/// </summary>
public ApiMetadata ApiMetadata { get; }
/// <summary>
/// The transports supported by this service.
/// </summary>
public ApiTransports Transports { get; }
/// <summary>
/// Constructs a new instance for a given service.
/// </summary>
/// <param name="serviceDescriptor">The protobuf descriptor for the service.</param>
/// <param name="defaultEndpoint">The default endpoint to connect to.</param>
/// <param name="defaultScopes">The default scopes for the service. Must not be null, and must not contain any null elements. May be empty.</param>
/// <param name="supportsScopedJwts">Whether the service supports scoped JWTs as credentials.</param>
/// <param name="transports">The transports supported by this service.</param>
/// <param name="apiMetadata">The metadata for this API, including all of the services expected to be available at the same endpoint, and all associated protos.</param>
public ServiceMetadata(ServiceDescriptor serviceDescriptor, string defaultEndpoint, IEnumerable<string> defaultScopes, bool supportsScopedJwts, ApiTransports transports, ApiMetadata apiMetadata)
{
ServiceDescriptor = GaxPreconditions.CheckNotNull(serviceDescriptor, nameof(serviceDescriptor));
Name = serviceDescriptor.Name;
GaxPreconditions.CheckArgument(Name.Length > 0, nameof(serviceDescriptor), "Service has an empty name");
DefaultEndpoint = defaultEndpoint;
EndpointTemplate = defaultEndpoint?.Contains(DefaultUniverseDomain) == true ?
defaultEndpoint.Replace(DefaultUniverseDomain, "{0}") :
null;
DefaultScopes = GaxPreconditions.CheckNotNull(defaultScopes, nameof(defaultScopes)).ToList().AsReadOnly();
GaxPreconditions.CheckArgument(!DefaultScopes.Any(x => x == null), nameof(defaultScopes), "Scopes must not contain any null references");
SupportsScopedJwts = supportsScopedJwts;
Transports = transports;
ApiMetadata = GaxPreconditions.CheckNotNull(apiMetadata, nameof(apiMetadata));
}
}
}