1
+ using System . Net ;
2
+ using System . Threading . Tasks ;
3
+ using ArangoDBNetStandard . Serialization ;
4
+ using ArangoDBNetStandard . Transport ;
5
+ using ArangoDBNetStandard . AdminApi . Models ;
6
+
7
+ namespace ArangoDBNetStandard . AdminApi
8
+ {
9
+ /// <summary>
10
+ /// A client for interacting with ArangoDB Admin API,
11
+ /// implementing <see cref="IAdminApiClient"/>.
12
+ /// </summary>
13
+ public class AdminApiClient : ApiClientBase , IAdminApiClient
14
+ {
15
+ /// <summary>
16
+ /// The transport client used to communicate with the ArangoDB host.
17
+ /// </summary>
18
+ protected IApiClientTransport _client ;
19
+
20
+ /// <summary>
21
+ /// The root path of the API.
22
+ /// </summary>
23
+ protected readonly string _adminApiPath = "_admin" ;
24
+
25
+ /// <summary>
26
+ /// Creates an instance of <see cref="AdminApiClient"/>
27
+ /// using the provided transport layer and the default JSON serialization.
28
+ /// </summary>
29
+ /// <param name="client">Transport client that the API client will use to communicate with ArangoDB</param>
30
+ public AdminApiClient ( IApiClientTransport client )
31
+ : base ( new JsonNetApiClientSerialization ( ) )
32
+ {
33
+ _client = client ;
34
+ }
35
+
36
+ /// <summary>
37
+ /// Creates an instance of <see cref="AdminApiClient"/>
38
+ /// using the provided transport and serialization layers.
39
+ /// </summary>
40
+ /// <param name="client">Transport client that the API client will use to communicate with ArangoDB.</param>
41
+ /// <param name="serializer">Serializer to be used.</param>
42
+ public AdminApiClient ( IApiClientTransport client , IApiClientSerialization serializer )
43
+ : base ( serializer )
44
+ {
45
+ _client = client ;
46
+ }
47
+
48
+ /// <summary>
49
+ /// Retrieves log messages from the server.
50
+ /// GET /_admin/log/entries
51
+ /// Works on ArangoDB 3.8 or later.
52
+ /// </summary>
53
+ /// <param name="query">Query string parameters</param>
54
+ /// <returns></returns>
55
+ /// <remarks>
56
+ /// For further information see
57
+ /// https://www.arangodb.com/docs/stable/http/administration-and-monitoring.html#read-global-logs-from-the-server
58
+ /// </remarks>
59
+ public virtual async Task < GetLogsResponse > GetLogsAsync ( GetLogsQuery query = null )
60
+ {
61
+ string uri = $ "{ _adminApiPath } /log/entries";
62
+ if ( query != null )
63
+ {
64
+ uri += '?' + query . ToQueryString ( ) ;
65
+ }
66
+ using ( var response = await _client . GetAsync ( uri ) . ConfigureAwait ( false ) )
67
+ {
68
+ if ( response . IsSuccessStatusCode )
69
+ {
70
+ var stream = await response . Content . ReadAsStreamAsync ( ) . ConfigureAwait ( false ) ;
71
+ return DeserializeJsonFromStream < GetLogsResponse > ( stream ) ;
72
+ }
73
+ throw await GetApiErrorException ( response ) . ConfigureAwait ( false ) ;
74
+ }
75
+ }
76
+
77
+ /// <summary>
78
+ /// Reloads the routing table.
79
+ /// POST /_admin/routing/reload
80
+ /// </summary>
81
+ /// <returns></returns>
82
+ /// <remarks>
83
+ /// For further information see
84
+ /// https://www.arangodb.com/docs/stable/http/administration-and-monitoring.html#reloads-the-routing-information
85
+ /// </remarks>
86
+ public virtual async Task < bool > PostReloadRoutingInfoAsync ( )
87
+ {
88
+ string uri = $ "{ _adminApiPath } /routing/reload";
89
+ var body = new byte [ ] { } ;
90
+ using ( var response = await _client . PostAsync ( uri , body ) . ConfigureAwait ( false ) )
91
+ {
92
+ if ( response . IsSuccessStatusCode )
93
+ {
94
+ return true ;
95
+ }
96
+ throw await GetApiErrorException ( response ) . ConfigureAwait ( false ) ;
97
+ }
98
+ }
99
+
100
+ /// <summary>
101
+ /// Retrieves the internal id of the server.
102
+ /// The method will fail if the server is not running in cluster mode.
103
+ /// GET /_admin/server/id
104
+ /// </summary>
105
+ /// <returns></returns>
106
+ /// <remarks>
107
+ /// For further information see
108
+ /// https://www.arangodb.com/docs/stable/http/administration-and-monitoring.html#return-id-of-a-server-in-a-cluster
109
+ /// </remarks>
110
+ public virtual async Task < GetServerIdResponse > GetServerIdAsync ( )
111
+ {
112
+ string uri = $ "{ _adminApiPath } /server/id";
113
+ using ( var response = await _client . GetAsync ( uri ) . ConfigureAwait ( false ) )
114
+ {
115
+ if ( response . IsSuccessStatusCode )
116
+ {
117
+ var stream = await response . Content . ReadAsStreamAsync ( ) . ConfigureAwait ( false ) ;
118
+ return DeserializeJsonFromStream < GetServerIdResponse > ( stream ) ;
119
+ }
120
+ throw await GetApiErrorException ( response ) . ConfigureAwait ( false ) ;
121
+ }
122
+ }
123
+
124
+ /// <summary>
125
+ /// Retrieves the role of the server in a cluster.
126
+ /// GET /_admin/server/role
127
+ /// </summary>
128
+ /// <returns></returns>
129
+ /// <remarks>
130
+ /// For further information see
131
+ /// https://www.arangodb.com/docs/stable/http/administration-and-monitoring.html#return-the-role-of-a-server-in-a-cluster
132
+ /// </remarks>
133
+ public virtual async Task < GetServerRoleResponse > GetServerRoleAsync ( )
134
+ {
135
+ string uri = $ "{ _adminApiPath } /server/role";
136
+ using ( var response = await _client . GetAsync ( uri ) . ConfigureAwait ( false ) )
137
+ {
138
+ if ( response . IsSuccessStatusCode )
139
+ {
140
+ var stream = await response . Content . ReadAsStreamAsync ( ) . ConfigureAwait ( false ) ;
141
+ return DeserializeJsonFromStream < GetServerRoleResponse > ( stream ) ;
142
+ }
143
+ throw await GetApiErrorException ( response ) . ConfigureAwait ( false ) ;
144
+ }
145
+ }
146
+
147
+ /// <summary>
148
+ /// Retrieves the server database engine type.
149
+ /// GET /_api/engine
150
+ /// </summary>
151
+ /// <returns></returns>
152
+ /// <remarks>
153
+ /// For further information see
154
+ /// https://www.arangodb.com/docs/stable/http/miscellaneous-functions.html#return-server-database-engine-type
155
+ /// </remarks>
156
+ public virtual async Task < GetServerEngineTypeResponse > GetServerEngineTypeAsync ( )
157
+ {
158
+ string uri = "_api/engine" ;
159
+ using ( var response = await _client . GetAsync ( uri ) . ConfigureAwait ( false ) )
160
+ {
161
+ if ( response . IsSuccessStatusCode )
162
+ {
163
+ var stream = await response . Content . ReadAsStreamAsync ( ) . ConfigureAwait ( false ) ;
164
+ return DeserializeJsonFromStream < GetServerEngineTypeResponse > ( stream ) ;
165
+ }
166
+ throw await GetApiErrorException ( response ) . ConfigureAwait ( false ) ;
167
+ }
168
+ }
169
+
170
+ /// <summary>
171
+ /// Retrieves the server version.
172
+ /// GET /_api/version
173
+ /// </summary>
174
+ /// <param name="query">Query string parameters</param>
175
+ /// <returns></returns>
176
+ /// <remarks>
177
+ /// For further information see
178
+ /// https://www.arangodb.com/docs/stable/http/miscellaneous-functions.html#return-server-version
179
+ /// </remarks>
180
+ public virtual async Task < GetServerVersionResponse > GetServerVersionAsync ( GetServerVersionQuery query = null )
181
+ {
182
+ string uri = "_api/version" ;
183
+ if ( query != null )
184
+ {
185
+ uri += '?' + query . ToQueryString ( ) ;
186
+ }
187
+ using ( var response = await _client . GetAsync ( uri ) . ConfigureAwait ( false ) )
188
+ {
189
+ if ( response . IsSuccessStatusCode )
190
+ {
191
+ var stream = await response . Content . ReadAsStreamAsync ( ) . ConfigureAwait ( false ) ;
192
+ return DeserializeJsonFromStream < GetServerVersionResponse > ( stream ) ;
193
+ }
194
+ throw await GetApiErrorException ( response ) . ConfigureAwait ( false ) ;
195
+ }
196
+ }
197
+ }
198
+ }
0 commit comments