@@ -20,7 +20,7 @@ public sealed partial class AvatarClient : global::Pika.IAvatarClient, global::S
2020 public global ::System . Net . Http . HttpClient HttpClient { get ; }
2121
2222 /// <inheritdoc/>
23- public System . Uri ? BaseUri => HttpClient . BaseAddress ;
23+ public System . Uri ? BaseUri => ResolveDisplayedBaseUri ( ) ;
2424
2525 /// <inheritdoc/>
2626 public global ::System . Collections . Generic . List < global ::Pika . EndPointAuthorization > Authorizations { get ; }
@@ -33,12 +33,43 @@ public sealed partial class AvatarClient : global::Pika.IAvatarClient, global::S
3333
3434 /// <inheritdoc/>
3535 public global ::Pika . AutoSDKClientOptions Options { get ; }
36+
37+
38+ internal global ::Pika . AutoSDKServerConfiguration AutoSDKServerConfiguration { get ; set ; } = new global ::Pika . AutoSDKServerConfiguration ( ) ;
3639 /// <summary>
3740 ///
3841 /// </summary>
3942 public global ::System . Text . Json . Serialization . JsonSerializerContext JsonSerializerContext { get ; set ; } = global ::Pika . SourceGenerationContext . Default ;
4043
4144
45+
46+ private static readonly global ::Pika . AutoSDKServer [ ] s_availableServers = new global ::Pika . AutoSDKServer [ ]
47+ { new global ::Pika . AutoSDKServer (
48+ id : "https-api-pika-art" ,
49+ name : "Primary API endpoint" ,
50+ url : "https://api.pika.art/" ,
51+ description : "Primary API endpoint" ) ,
52+ new global ::Pika . AutoSDKServer (
53+ id : "https-srkibaanghvsriahb-pika-art-proxy-realtime" ,
54+ name : "Proxy/realtime endpoint (used by Python SDK)" ,
55+ url : "https://srkibaanghvsriahb.pika.art/proxy/realtime" ,
56+ description : "Proxy/realtime endpoint (used by Python SDK)" ) ,
57+ } ;
58+
59+ /// <summary>
60+ /// The server options available for this client.
61+ /// </summary>
62+ public global ::System . Collections . Generic . IReadOnlyList < global ::Pika . AutoSDKServer > AvailableServers => s_availableServers ;
63+
64+ /// <summary>
65+ /// The currently selected server for this client, if any.
66+ /// </summary>
67+ public global ::Pika . AutoSDKServer ? SelectedServer
68+ {
69+ get => ResolveSelectedServer ( ) ;
70+ set => SelectServer ( value ) ;
71+ }
72+
4273 /// <summary>
4374 /// Creates a new instance of the AvatarClient.
4475 /// If no httpClient is provided, a new one will be created.
@@ -85,6 +116,8 @@ public AvatarClient(
85116 Options = options ?? new global ::Pika . AutoSDKClientOptions ( ) ;
86117 _disposeHttpClient = disposeHttpClient ;
87118
119+ AutoSDKServerConfiguration . ExplicitBaseUri = baseUri ?? httpClient ? . BaseAddress ;
120+
88121 Initialized ( HttpClient ) ;
89122 }
90123
@@ -111,5 +144,117 @@ partial void ProcessResponseContent(
111144 global ::System . Net . Http . HttpClient client ,
112145 global ::System . Net . Http . HttpResponseMessage response ,
113146 ref string content ) ;
147+
148+
149+ /// <summary>
150+ /// Selects one of the generated server options by id.
151+ /// </summary>
152+ public bool TrySelectServer ( string serverId )
153+ {
154+ if ( string . IsNullOrWhiteSpace ( serverId ) )
155+ {
156+ return false ;
157+ }
158+
159+ foreach ( var server in s_availableServers )
160+ {
161+ if ( string . Equals ( server . Id , serverId , global ::System . StringComparison . OrdinalIgnoreCase ) )
162+ {
163+ AutoSDKServerConfiguration . SelectedServer = server ;
164+ AutoSDKServerConfiguration . ExplicitBaseUri = null ;
165+ return true ;
166+ }
167+ }
168+
169+ return false ;
170+ }
171+
172+ /// <summary>
173+ /// Clears the currently selected server.
174+ /// </summary>
175+ public void ClearSelectedServer ( )
176+ {
177+ AutoSDKServerConfiguration . SelectedServer = null ;
178+ }
179+
180+ private global ::Pika . AutoSDKServer ? ResolveSelectedServer ( )
181+ {
182+ var selectedServer = AutoSDKServerConfiguration . SelectedServer ;
183+ if ( selectedServer is null )
184+ {
185+ return null ;
186+ }
187+
188+ foreach ( var server in s_availableServers )
189+ {
190+ if ( string . Equals ( server . Id , selectedServer . Id , global ::System . StringComparison . Ordinal ) )
191+ {
192+ return server ;
193+ }
194+ }
195+
196+ return null ;
197+ }
198+
199+ private void SelectServer ( global ::Pika . AutoSDKServer ? server )
200+ {
201+ if ( server is null )
202+ {
203+ AutoSDKServerConfiguration . SelectedServer = null ;
204+ return ;
205+ }
206+
207+ foreach ( var candidate in s_availableServers )
208+ {
209+ if ( string . Equals ( candidate . Id , server . Id , global ::System . StringComparison . Ordinal ) )
210+ {
211+ AutoSDKServerConfiguration . SelectedServer = candidate ;
212+ AutoSDKServerConfiguration . ExplicitBaseUri = null ;
213+ return ;
214+ }
215+ }
216+
217+ throw new global ::System . ArgumentException ( "The provided server is not available for this client." , nameof ( server ) ) ;
218+ }
219+
220+ private global ::System . Uri ? ResolveDisplayedBaseUri ( )
221+ {
222+ if ( AutoSDKServerConfiguration . ExplicitBaseUri is global ::System . Uri explicitBaseUri )
223+ {
224+ return explicitBaseUri ;
225+ }
226+
227+ return ResolveSelectedServer ( ) ? . Uri ?? HttpClient . BaseAddress ;
228+ }
229+
230+ private global ::System . Uri ? ResolveBaseUri (
231+ global ::Pika . AutoSDKServer [ ] servers ,
232+ string defaultBaseUrl )
233+ {
234+ if ( AutoSDKServerConfiguration . ExplicitBaseUri is global ::System . Uri explicitBaseUri )
235+ {
236+ return explicitBaseUri ;
237+ }
238+
239+ if ( AutoSDKServerConfiguration . SelectedServer is global ::Pika . AutoSDKServer selectedServer )
240+ {
241+ foreach ( var server in servers )
242+ {
243+ if ( string . Equals ( server . Id , selectedServer . Id , global ::System . StringComparison . Ordinal ) )
244+ {
245+ return server . Uri ;
246+ }
247+ }
248+ }
249+
250+ if ( servers . Length > 0 )
251+ {
252+ return servers [ 0 ] . Uri ;
253+ }
254+
255+ return string . IsNullOrWhiteSpace ( defaultBaseUrl )
256+ ? HttpClient . BaseAddress
257+ : new global ::System . Uri ( defaultBaseUrl , global ::System . UriKind . RelativeOrAbsolute ) ;
258+ }
114259 }
115260}
0 commit comments