kopi 0.0.9

Kopi is a JDK version management tool
Documentation
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
// Copyright 2025 dentsusoken
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::api::query::PackageQuery;
use crate::error::{KopiError, Result};
use crate::models::api::*;
use crate::platform::get_foojay_libc_type;
use crate::user_agent;
use attohttpc::{RequestBuilder, Session};
use log::{debug, trace};
use retry::{OperationResult, delay::Exponential, retry_with_index};
use std::thread;
use std::time::Duration;

pub const FOOJAY_API_BASE: &str = "https://api.foojay.io/disco";
pub const API_VERSION: &str = "v3.0";
const DEFAULT_TIMEOUT: u64 = 30;
const MAX_RETRIES: usize = 3;
const INITIAL_BACKOFF_MS: u64 = 1000;

#[derive(Debug, Clone)]
pub struct ApiClient {
    pub(crate) session: Session,
    pub(crate) base_url: String,
}

impl ApiClient {
    pub fn new() -> Self {
        let mut session = Session::new();
        session.header("User-Agent", user_agent::api_client());
        session.timeout(Duration::from_secs(DEFAULT_TIMEOUT));
        session.proxy_settings(attohttpc::ProxySettings::from_env());

        Self {
            session,
            base_url: FOOJAY_API_BASE.to_string(),
        }
    }

    pub fn with_base_url(mut self, base_url: String) -> Self {
        self.base_url = base_url;
        self
    }

    pub fn with_timeout(mut self, timeout: Duration) -> Self {
        self.session.timeout(timeout);
        self
    }

    pub fn fetch_all_metadata(&self) -> Result<ApiMetadata> {
        // Fetch distributions
        let distributions = self.get_distributions()?;

        // Get platform-specific parameters
        let architecture = crate::platform::get_current_architecture();
        let operating_system = crate::platform::get_current_os();
        let lib_c_type = get_foojay_libc_type();

        // For each distribution, fetch available packages
        let mut metadata = ApiMetadata {
            distributions: Vec::new(),
        };

        // Archive types to query for (as expected by foojay.io API)
        let archive_types = vec![
            "tar.gz".to_string(),
            "zip".to_string(),
            "tgz".to_string(),
            "tar".to_string(),
        ];

        for dist in distributions {
            let query = PackageQuery {
                distribution: Some(dist.api_parameter.clone()),
                architecture: Some(architecture.clone()),
                package_type: None,
                operating_system: Some(operating_system.clone()),
                lib_c_type: Some(lib_c_type.to_string()),
                archive_types: Some(archive_types.clone()),
                latest: Some("available".to_string()),
                directly_downloadable: Some(true),
                version: None,
                javafx_bundled: Some(false),
            };

            let packages = match self.get_packages(Some(query)) {
                Ok(packages) => packages,
                Err(e) => {
                    debug!("Failed to fetch packages for {}: {e}", dist.api_parameter);
                    Vec::new()
                }
            };

            metadata.distributions.push(DistributionMetadata {
                distribution: dist,
                packages,
            });
        }

        Ok(metadata)
    }

    pub fn fetch_all_metadata_with_options(&self, javafx_bundled: bool) -> Result<ApiMetadata> {
        // Fetch distributions
        let distributions = self.get_distributions()?;

        // Get platform-specific parameters
        let architecture = crate::platform::get_current_architecture();
        let operating_system = crate::platform::get_current_os();
        let lib_c_type = get_foojay_libc_type();

        // For each distribution, fetch available packages
        let mut metadata = ApiMetadata {
            distributions: Vec::new(),
        };

        // Archive types to query for (as expected by foojay.io API)
        let archive_types = vec![
            "tar.gz".to_string(),
            "zip".to_string(),
            "tgz".to_string(),
            "tar".to_string(),
        ];

        for dist in distributions {
            let query = PackageQuery {
                distribution: Some(dist.api_parameter.clone()),
                architecture: Some(architecture.clone()),
                package_type: None,
                operating_system: Some(operating_system.clone()),
                lib_c_type: Some(lib_c_type.to_string()),
                archive_types: Some(archive_types.clone()),
                latest: Some("available".to_string()),
                directly_downloadable: Some(true),
                version: None,
                javafx_bundled: if javafx_bundled { None } else { Some(false) },
            };

            let packages = match self.get_packages(Some(query)) {
                Ok(packages) => packages,
                Err(e) => {
                    debug!("Failed to fetch packages for {}: {e}", dist.api_parameter);
                    Vec::new()
                }
            };

            metadata.distributions.push(DistributionMetadata {
                distribution: dist,
                packages,
            });
        }

        Ok(metadata)
    }

    pub fn get_packages(&self, query: Option<PackageQuery>) -> Result<Vec<Package>> {
        let url = format!("{}/{API_VERSION}/packages", self.base_url);
        let query = query.clone();

        self.execute_with_retry(move || {
            let mut request = self.session.get(&url);

            if let Some(ref q) = query {
                if let Some(ref version) = q.version {
                    request = request.param("version", version);
                }
                if let Some(ref distribution) = q.distribution {
                    request = request.param("distribution", distribution);
                }
                if let Some(ref architecture) = q.architecture {
                    request = request.param("architecture", architecture);
                }
                if let Some(ref package_type) = q.package_type {
                    request = request.param("package_type", package_type);
                }
                if let Some(ref operating_system) = q.operating_system {
                    request = request.param("operating_system", operating_system);
                }
                if let Some(ref archive_types) = q.archive_types {
                    for archive_type in archive_types {
                        request = request.param("archive_type", archive_type);
                    }
                }
                if let Some(ref latest) = q.latest {
                    request = request.param("latest", latest);
                }
                if let Some(directly_downloadable) = q.directly_downloadable {
                    request =
                        request.param("directly_downloadable", directly_downloadable.to_string());
                }
                if let Some(ref lib_c_type) = q.lib_c_type {
                    request = request.param("lib_c_type", lib_c_type);
                }
                if let Some(javafx_bundled) = q.javafx_bundled {
                    request = request.param("javafx_bundled", javafx_bundled.to_string());
                }
            }

            request
        })
    }

    pub fn get_distributions(&self) -> Result<Vec<Distribution>> {
        let url = format!("{}/{API_VERSION}/distributions", self.base_url);
        self.execute_with_retry(move || self.session.get(&url))
    }

    pub fn get_major_versions(&self) -> Result<Vec<MajorVersion>> {
        let url = format!("{}/{API_VERSION}/major_versions", self.base_url);
        self.execute_with_retry(move || self.session.get(&url))
    }

    pub fn get_package_by_id(&self, package_id: &str) -> Result<PackageInfo> {
        // Special handling for package by ID endpoint which returns an array
        let url = format!("{}/{API_VERSION}/ids/{package_id}", self.base_url);
        debug!("Fetching package info for ID: {package_id}");
        let package_id_copy = package_id.to_string();

        // Use the common retry logic but handle the array response
        self.execute_with_retry_raw(
            move || self.session.get(&url),
            move |body| match serde_json::from_str::<serde_json::Value>(&body) {
                Ok(json_value) => {
                    // API v3.0 always wraps responses with "result" field
                    if let Some(result) = json_value.get("result") {
                        // The result is an array of PackageInfo
                        match serde_json::from_value::<Vec<PackageInfo>>(result.clone()) {
                            Ok(packages) => {
                                if let Some(package) = packages.into_iter().next() {
                                    Ok(package)
                                } else {
                                    Err(KopiError::MetadataFetch(format!(
                                        "No package info found for ID: {package_id_copy} (API \
                                         v{API_VERSION})"
                                    )))
                                }
                            }
                            Err(e) => {
                                debug!("Failed to parse 'result' field as array: {e}");
                                trace!("Result field: {result:?}");
                                Err(KopiError::MetadataFetch(format!(
                                    "Failed to parse API v{API_VERSION} response: {e}"
                                )))
                            }
                        }
                    } else {
                        Err(KopiError::MetadataFetch(format!(
                            "Invalid API v{API_VERSION} response: missing 'result' field"
                        )))
                    }
                }
                Err(e) => {
                    debug!("Failed to parse as JSON: {e}");
                    Err(KopiError::MetadataFetch(format!(
                        "Invalid JSON response from API v{API_VERSION}: {e}"
                    )))
                }
            },
        )
    }

    fn execute_with_retry<T, F>(&self, request_builder: F) -> Result<T>
    where
        T: for<'de> serde::Deserialize<'de>,
        F: Fn() -> RequestBuilder,
    {
        self.execute_with_retry_raw(request_builder, |body| {
            // Parse JSON response
            match serde_json::from_str::<serde_json::Value>(&body) {
                Ok(json_value) => {
                    // API v3.0 always wraps responses with "result" field
                    if let Some(result) = json_value.get("result") {
                        // Try to deserialize the result field
                        match serde_json::from_value::<T>(result.clone()) {
                            Ok(data) => Ok(data),
                            Err(e) => {
                                debug!("Failed to parse 'result' field: {e}");
                                trace!("Result field: {result:?}");
                                Err(KopiError::MetadataFetch(format!(
                                    "Failed to parse API v{API_VERSION} response: {e}"
                                )))
                            }
                        }
                    } else {
                        Err(KopiError::MetadataFetch(format!(
                            "Invalid API v{API_VERSION} response: missing 'result' field"
                        )))
                    }
                }
                Err(e) => {
                    debug!("Failed to parse as JSON: {e}");
                    Err(KopiError::MetadataFetch(format!(
                        "Invalid JSON response from API v{API_VERSION}: {e}"
                    )))
                }
            }
        })
    }

    fn execute_with_retry_raw<T, F, P>(&self, request_builder: F, parser: P) -> Result<T>
    where
        F: Fn() -> RequestBuilder,
        P: Fn(String) -> Result<T>,
    {
        let result = retry_with_index(
            Exponential::from_millis(INITIAL_BACKOFF_MS).take(MAX_RETRIES),
            |current_try| {
                let response = match request_builder().send() {
                    Ok(resp) => resp,
                    Err(e) => {
                        let user_error = KopiError::MetadataFetch(format!(
                            "Network error connecting to foojay.io API v{API_VERSION}: {e}. \
                             Please check your internet connection and try again."
                        ));

                        if current_try < (MAX_RETRIES - 1) as u64 {
                            return OperationResult::Retry(user_error);
                        }
                        return OperationResult::Err(user_error);
                    }
                };

                if response.status() == attohttpc::StatusCode::TOO_MANY_REQUESTS
                    && current_try < (MAX_RETRIES - 1) as u64
                {
                    if let Some(retry_after) = response.headers().get("Retry-After")
                        && let Ok(retry_str) = retry_after.to_str()
                        && let Ok(seconds) = retry_str.parse::<u64>()
                    {
                        thread::sleep(Duration::from_secs(seconds));
                    }
                    return OperationResult::Retry(KopiError::MetadataFetch(
                        "Too many requests. Waiting before retrying...".to_string(),
                    ));
                }

                if !response.is_success() {
                    let status = response.status();

                    // Try to parse error response body for more specific error message
                    let error_msg = if status.as_u16() == 400 {
                        match response.text() {
                            Ok(body) => {
                                // Try to parse as API error response
                                match serde_json::from_str::<crate::api::ApiErrorResponse>(&body) {
                                    Ok(error_response) => {
                                        // Check if the message indicates version not found
                                        if error_response.message.contains("not released yet") {
                                            format!(
                                                "Version not available: {}",
                                                error_response.message
                                            )
                                        } else {
                                            format!("Bad request: {}", error_response.message)
                                        }
                                    }
                                    Err(_) => format!(
                                        "HTTP error ({}) from foojay.io API v{API_VERSION}: {}",
                                        status.as_u16(),
                                        status.canonical_reason().unwrap_or("Unknown error")
                                    ),
                                }
                            }
                            Err(_) => format!(
                                "HTTP error ({}) from foojay.io API v{API_VERSION}: {}",
                                status.as_u16(),
                                status.canonical_reason().unwrap_or("Unknown error")
                            ),
                        }
                    } else {
                        match status.as_u16() {
                            404 => format!(
                                "The requested resource was not found on foojay.io API \
                                 v{API_VERSION}. The API endpoint may have changed."
                            ),
                            500..=599 => format!(
                                "Server error occurred on foojay.io API v{API_VERSION}. Please \
                                 try again later."
                            ),
                            401 | 403 => format!(
                                "Authentication failed for foojay.io API v{API_VERSION}. Please \
                                 check your credentials."
                            ),
                            _ => format!(
                                "HTTP error ({}) from foojay.io API v{API_VERSION}: {}",
                                status.as_u16(),
                                status.canonical_reason().unwrap_or("Unknown error")
                            ),
                        }
                    };
                    return OperationResult::Err(KopiError::MetadataFetch(error_msg));
                }

                // Try to get response text for debugging
                match response.text() {
                    Ok(body) => match parser(body) {
                        Ok(data) => OperationResult::Ok(data),
                        Err(e) => OperationResult::Err(e),
                    },
                    Err(e) => OperationResult::Err(KopiError::MetadataFetch(format!(
                        "Failed to read response body: {e}"
                    ))),
                }
            },
        );

        result.map_err(|e| e.error)
    }
}

impl Default for ApiClient {
    fn default() -> Self {
        Self::new()
    }
}