forked from TypedRest/OpenServiceBroker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOpenServiceBrokerClient.cs
98 lines (88 loc) · 4.8 KB
/
OpenServiceBrokerClient.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
using System;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using OpenServiceBroker.Catalogs;
using OpenServiceBroker.Instances;
using TypedRest.Endpoints;
using TypedRest.Endpoints.Generic;
namespace OpenServiceBroker
{
/// <summary>
/// A type-safe client for the Open Service Broker API.
/// </summary>
public class OpenServiceBrokerClient : EntryEndpoint, IOpenServiceBrokerClient
{
/// <summary>
/// Creates a new Open Service Broker Client using a custom <see cref="HttpClient"/>. This is usually used for custom authentication schemes, e.g. client certificates.
/// </summary>
/// <param name="uri">The base URI of the Open Service Broker API instance (without the version number).</param>
/// <param name="httpClient">The <see cref="HttpClient"/> to use for communication with My Service.</param>
public OpenServiceBrokerClient(HttpClient httpClient, Uri uri)
: base(httpClient, uri, errorHandler: new OpenServiceBrokerErrorHandler())
{
SetApiVersion(DefaultApiVersion);
}
/// <summary>
/// Creates a new Open Service Broker Client.
/// </summary>
/// <param name="uri">The base URI of the Open Service Broker API instance (without the version number).</param>
/// <param name="credentials">Optional HTTP Basic Auth credentials used to authenticate against the REST interface.</param>
public OpenServiceBrokerClient(Uri uri, ICredentials? credentials = null)
: base(uri, credentials, errorHandler: new OpenServiceBrokerErrorHandler())
{
SetApiVersion(DefaultApiVersion);
}
/// <summary>
/// Creates a new Open Service Broker Client.
/// </summary>
/// <param name="uri">The base URI of the Open Service Broker API instance (without the version number).</param>
/// <param name="token">The OAuth token to present as a "Bearer" to the REST interface.</param>
public OpenServiceBrokerClient(Uri uri, string token)
: base(uri, errorHandler: new OpenServiceBrokerErrorHandler())
{
SetApiVersion(DefaultApiVersion);
HttpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
}
/// <summary>
/// The default Open Service Broker API version to set for requests.
/// </summary>
public static ApiVersion DefaultApiVersion => new ApiVersion(2, 13);
/// <summary>
/// Sets the Open Service Broker API version to a custom value. Only use this if you know what you are doing!
/// </summary>
public void SetApiVersion(ApiVersion version)
{
HttpClient.DefaultRequestHeaders.Remove(ApiVersion.HttpHeaderName);
HttpClient.DefaultRequestHeaders.Add(ApiVersion.HttpHeaderName, version.ToString());
}
/// <summary>
/// Sets the identity of the user that initiated the request from the Platform.
/// </summary>
public void SetOriginatingIdentity(OriginatingIdentity identity)
{
HttpClient.DefaultRequestHeaders.Remove(OriginatingIdentity.HttpHeaderName);
HttpClient.DefaultRequestHeaders.Add(OriginatingIdentity.HttpHeaderName, identity.ToString());
}
/// <summary>
/// Exposes a list of all services available on the Service Broker.
/// </summary>
public IElementEndpoint<Catalog> Catalog
=> new ElementEndpoint<Catalog>(this, relativeUri: "./v2/catalog");
/// <summary>
/// Exposes Service Instances with blocking operations. If the Service Broker can only handle a request deferred (asynchronously) <see cref="Errors.AsyncRequiredException"/> is thrown.
/// </summary>
public IIndexerEndpoint<IServiceInstanceEndpoint> ServiceInstancesBlocking
=> new IndexerEndpoint<ServiceInstanceBlockingEndpoint>(this, relativeUri: "./v2/service_instances");
/// <summary>
/// Exposes Service Instances with potentially deferred (asynchronous) operations.
/// </summary>
public IIndexerEndpoint<IServiceInstanceDeferredEndpoint> ServiceInstancesDeferred
=> new IndexerEndpoint<ServiceInstanceDeferredEndpoint>(this, relativeUri: "./v2/service_instances");
/// <summary>
/// Exposes Service Instances. Uses potentially deferred (asynchronous) operations and automatically handles polling to make them appear blocking.
/// </summary>
public IIndexerEndpoint<IServiceInstanceEndpoint> ServiceInstancesPolling
=> new IndexerEndpoint<ServiceInstancePollingEndpoint>(this, relativeUri: "./v2/service_instances");
}
}