1+ using System ;
2+ using System . Net . Http ;
3+ using Platform . Common . Platform ;
4+
5+ namespace InfluxData . Platform . Client . Option
6+ {
7+ /**
8+ * PlatformOptions are used to configure the InfluxData Platform connections.
9+ */
10+ public class PlatformOptions
11+ {
12+ public string Url { get ; private set ; }
13+
14+ public EAuthScheme AuthScheme { get ; private set ; }
15+ public char [ ] Token { get ; private set ; }
16+ public string Username { get ; private set ; }
17+ public char [ ] Password { get ; private set ; }
18+
19+ public TimeSpan Timeout { get ; private set ; }
20+
21+ private PlatformOptions ( Builder builder )
22+ {
23+ Arguments . CheckNotNull ( builder , "PlatformOptions.Builder" ) ;
24+
25+ Url = builder . UrlString ;
26+ AuthScheme = builder . AuthScheme ;
27+ Token = builder . Token ;
28+ Username = builder . Username ;
29+ Password = builder . Password ;
30+ Timeout = builder . TimeOut ;
31+ }
32+
33+ /**
34+ * The scheme uses to Authentication.
35+ */
36+ public enum EAuthScheme
37+ {
38+ /**
39+ * Basic auth.
40+ */
41+ Session ,
42+
43+ /**
44+ * Authentication token.
45+ */
46+ Token
47+ }
48+
49+ /**
50+ * A builder for {@code PlatformOptions}.
51+ */
52+ public sealed class Builder
53+ {
54+ public string UrlString { get ; private set ; }
55+
56+ public EAuthScheme AuthScheme { get ; private set ; }
57+ public char [ ] Token { get ; private set ; }
58+ public string Username { get ; private set ; }
59+ public char [ ] Password { get ; private set ; }
60+
61+ public TimeSpan TimeOut { get ; private set ; }
62+
63+ public static Builder CreateNew ( )
64+ {
65+ return new Builder ( ) ;
66+ }
67+
68+ /**
69+ * Set the url to connect to Platform.
70+ *
71+ * @param url the url to connect to Platform. It must be defined.
72+ * @return {@code this}
73+ */
74+ public Builder Url ( string url )
75+ {
76+ Arguments . CheckNonEmptyString ( url , "url" ) ;
77+
78+ UrlString = url ;
79+
80+ return this ;
81+ }
82+
83+ /**
84+ * Set the Timeout to connect to Platform.
85+ *
86+ * @param timeout is the timeout to connect to Platform. It must be defined.
87+ * @return {@code this}
88+ */
89+ public Builder Timeout ( TimeSpan timeout )
90+ {
91+ Arguments . CheckNotNull ( timeout , "timeout" ) ;
92+
93+ TimeOut = timeout ;
94+
95+ return this ;
96+ }
97+
98+ /**
99+ * Setup authorization by {@link AuthScheme#SESSION}.
100+ *
101+ * @param username the username to use in the basic auth
102+ * @param password the password to use in the basic auth
103+ * @return {@link PlatformOptions}
104+ */
105+ public PlatformOptions . Builder Authenticate ( string username ,
106+ char [ ] password )
107+ {
108+ Arguments . CheckNonEmptyString ( username , "username" ) ;
109+ Arguments . CheckNotNull ( password , "password" ) ;
110+
111+ AuthScheme = EAuthScheme . Session ;
112+ Username = username ;
113+ Password = password ;
114+
115+ return this ;
116+ }
117+
118+ /**
119+ * Setup authorization by {@link AuthScheme#TOKEN}.
120+ *
121+ * @param token the token to use for the authorization
122+ * @return {@link PlatformOptions}
123+ */
124+ public PlatformOptions . Builder AuthenticateToken ( char [ ] token )
125+ {
126+ Arguments . CheckNotNull ( token , "token" ) ;
127+
128+ AuthScheme = EAuthScheme . Token ;
129+ Token = token ;
130+
131+ return this ;
132+ }
133+
134+ /**
135+ * Build an instance of PlatformOptions.
136+ *
137+ * @return {@link PlatformOptions}
138+ */
139+ public PlatformOptions Build ( )
140+ {
141+ if ( string . IsNullOrEmpty ( UrlString ) )
142+ {
143+ throw new InvalidOperationException ( "The url to connect to Platform has to be defined." ) ;
144+ }
145+
146+ if ( TimeOut == TimeSpan . Zero || TimeOut == TimeSpan . FromMilliseconds ( 0 ) )
147+ {
148+ TimeOut = TimeSpan . FromSeconds ( 60 ) ;
149+ }
150+
151+ return new PlatformOptions ( this ) ;
152+ }
153+ }
154+ }
155+ }
0 commit comments