apisix_admin_client/
lib.rs

1//! Apisix Client Library
2//!
3//! Provides a REST models for service consumers of Apisix.
4//!
5//! Maintained by [Trust1Team](https://trust1team.com) for [Apisix](https://apisix.apache.org/)
6
7use serde_json::Value;
8use crate::config::ApisixConfig;
9pub mod client;
10pub mod error;
11pub mod models;
12pub mod config;
13
14pub mod client_admin_impl;
15pub mod client_ctrl_impl;
16
17type Result<T> = std::result::Result<T, crate::error::ApisixClientError>;
18
19/// Common models are exposed
20use crate::client_admin_impl::{api_admin_check_version, api_admin_get_services, api_admin_get_upstreams};
21use crate::client_ctrl_impl::api_ctrl_schema;
22use crate::models::{ApisixConsumer, ApisixConsumerGroup, ApisixRoute, ApisixService, ApisixUpstream, ConsumerGroupRequest, ConsumerRequest, RouteRequest, ServiceRequest};
23use crate::models::common::{ListResponse, TypedItem};
24use crate::models::ctrl_responses::CtrlHealthCheckResponse;
25use crate::models::UpstreamRequest;
26
27/// Get configuration based on the environment variables (default config override)
28/// Function will panic when the environment variables are not set
29/// The following variables must be set:
30///| ENV_VAR                                   | DESCRIPTION                                                                    | REQUIRED       |
31///|-------------------------------------------|--------------------------------------------------------------------------------|----------------|
32///| APISIX_URL                                | The Apisix gateway url for operational traffic                                 | N - default () |
33///| APISIX_ADMIN_URL                          | The Apisix admin api url                                                       | N - default () |
34///| APISIX_CONTROL_URL                        | The Apisix control api url                                                     | N - default () |
35///| APISIX_ADMIN_API_KEY                      | The Apisix admin api key                                                       | N - default () |
36pub async fn get_config_from_env() -> ApisixConfig {
37    ApisixConfig::from_env()
38}
39
40/// Get configuration based on the Apisix development environment variables
41/// Function is used during development, and in combination with a Smart ID demo client application
42pub async fn get_config_default() -> ApisixConfig {
43    ApisixConfig::default()
44}
45
46
47
48/// Check if the Admin API is up and running
49pub async fn admin_check(cfg: &ApisixConfig) -> Result<()> {
50    api_admin_check_version(cfg).await
51}
52
53/// Fetch a list of all configured Upstreams
54pub async fn admin_get_upstreams(cfg: &ApisixConfig) -> Result<ListResponse<TypedItem<ApisixUpstream>>> {
55    api_admin_get_upstreams(cfg).await
56}
57
58/// Fetches specified Upstream by id
59pub async fn admin_get_upstream(cfg: &ApisixConfig, id: &str) -> Result<TypedItem<ApisixUpstream>> {
60    client_admin_impl::api_admin_get_upstream(cfg, id).await
61}
62
63/// Creates or updates a Route with the specified id
64pub async fn admin_create_upstream_with_id(cfg: &ApisixConfig, id: &str, req: &UpstreamRequest) -> Result<TypedItem<ApisixUpstream>> {
65    client_admin_impl::api_admin_create_upstream_with_id(cfg, id, req).await
66}
67
68/*/// Creates an Upstream and assigns a random id
69/// The default behaviour is that Apisix generated an ID for you, but that causes problems as Apisix
70/// allows the use of integers or strings for id's.
71/// We force a random generated string, fallback is the default Apisix behaviour
72pub async fn admin_create_upstream(cfg: &ApisixConfig, req: &UpstreamRequest) -> Result<TypedItem<ApisixUpstream>> {
73    client_admin_impl::api_admin_create_upstream(cfg, req).await
74}*/
75
76/// Removes the Upstream with the specified id
77pub async fn admin_delete_upstream(cfg: &ApisixConfig, id: &str) -> Result<()> {
78    client_admin_impl::api_admin_delete_upstream(cfg, id).await
79}
80
81/// Fetches a list of available Services
82pub async fn admin_get_services(cfg: &ApisixConfig) -> Result<ListResponse<TypedItem<ApisixService>>> {
83    api_admin_get_services(cfg).await
84}
85
86/// Fetches specified Service by id
87pub async fn admin_get_service(cfg: &ApisixConfig, id: &str) -> Result<TypedItem<ApisixService>> {
88    client_admin_impl::api_admin_get_service(cfg, id).await
89}
90
91/// Creates or updates a Service with the specified id
92pub async fn admin_create_service_with_id(cfg: &ApisixConfig, id: &str, req: &ServiceRequest) -> Result<TypedItem<ApisixService>> {
93    client_admin_impl::api_admin_create_service_with_id(cfg, id, req).await
94}
95
96/// Removes the Service with the specified id
97pub async fn admin_delete_service(cfg: &ApisixConfig, id: &str) -> Result<()> {
98    client_admin_impl::api_admin_delete_service(cfg, id).await
99}
100
101/// Fetches a list of all configured Routes
102pub async fn admin_get_routes(cfg: &ApisixConfig) -> Result<ListResponse<TypedItem<ApisixRoute>>>
103{
104    client_admin_impl::api_admin_get_routes(cfg).await
105}
106
107/// Fetches specified Route by id
108pub async fn admin_get_route(cfg: &ApisixConfig, id: &str) -> Result<TypedItem<ApisixRoute>> {
109    client_admin_impl::api_admin_get_route(cfg, id).await
110}
111
112/// Creates or updates a Route with the specified id
113pub async fn admin_create_route_with_id(cfg: &ApisixConfig, id: &str, req: &RouteRequest) -> Result<TypedItem<ApisixRoute>> {
114    client_admin_impl::api_admin_create_route_with_id(cfg, id, req).await
115}
116
117/// Removes the Route with the specified id
118pub async fn admin_delete_route(cfg: &ApisixConfig, id: &str) -> Result<()> {
119    client_admin_impl::api_admin_delete_route(cfg, id).await
120}
121
122/// Fetches a list of all configured Consumer Groups
123pub async fn admin_get_consumer_groups(cfg: &ApisixConfig) -> Result<ListResponse<TypedItem<ApisixConsumerGroup>>> {
124    client_admin_impl::api_admin_get_consumer_groups(cfg).await
125}
126
127/// Fetches specified Consumer group by id
128pub async fn admin_get_consumer_group(cfg: &ApisixConfig, id: &str) -> Result<TypedItem<ApisixConsumerGroup>> {
129    client_admin_impl::api_admin_get_consumer_group(cfg, id).await
130}
131
132/// Creates or updates a new Consumer group with the specified id
133pub async fn admin_create_consumer_group_with_id(cfg: &ApisixConfig, id: &str, req: &ConsumerGroupRequest) -> Result<TypedItem<ApisixConsumerGroup>> {
134    client_admin_impl::api_admin_create_consumer_group_with_id(cfg, id, req).await
135}
136
137/// Removes the Consumer group with the specified id
138pub async fn admin_delete_consumer_group(cfg: &ApisixConfig, id: &str) -> Result<()> {
139    client_admin_impl::api_admin_delete_consumer_group(cfg, id).await
140}
141
142/// Fetches a list of all Consumers
143pub async fn admin_get_consumers(cfg: &ApisixConfig) -> Result<ListResponse<TypedItem<ApisixConsumer>>> {
144    client_admin_impl::api_admin_get_consumers(cfg).await
145}
146
147/// Fetches specified Consumer by username
148pub async fn admin_get_consumer(cfg: &ApisixConfig, username: &str) -> Result<TypedItem<ApisixConsumer>> {
149    client_admin_impl::api_admin_get_consumer(cfg, username).await
150}
151
152/// Create new Consumer
153pub async fn admin_create_consumer_with_name(cfg: &ApisixConfig, id: &str, req: &ConsumerRequest) -> Result<TypedItem<ApisixConsumer>> {
154    client_admin_impl::api_admin_create_consumer(cfg, id, req).await
155}
156
157/// Removes the Consumer with the specified username
158pub async fn admin_delete_consumer(cfg: &ApisixConfig, username: &str) -> Result<()> {
159    client_admin_impl::api_admin_delete_consumer(cfg, username).await
160}
161// region: controller
162/// Returns the JSON schema used by the APISIX instance (untyped JSON)
163pub async  fn ctrl_schema(cfg: &ApisixConfig) -> Result<Value> {
164    api_ctrl_schema(cfg).await
165}
166
167/// Returns a health check of the APISIX instance
168pub async fn ctrl_health_check(cfg: &ApisixConfig) -> Result<CtrlHealthCheckResponse> {
169    client_ctrl_impl::api_ctrl_health_check(cfg).await
170}
171
172/// Triggers a full garbage collection in the HTTP subsystem.
173/// Note: When stream proxy is enabled, APISIX runs another Lua VM for the stream subsystem.
174/// Full garbage collection is not triggered in this VM.
175pub async fn ctrl_garbage_collect(cfg: &ApisixConfig) -> Result<()> {
176    client_ctrl_impl::api_ctrl_garbage_collect(cfg).await
177}
178// endregion: controller
179