1use std::collections::BTreeMap;
4
5use crate::client::{
6 AppStoreClient, AuthInfo, DownloadRequest, LoginRequest, SearchResult, VersionInfo,
7};
8use crate::error::{IpatoolError, Result};
9use crate::helpers::{build_query, parse_search_json};
10use crate::store;
11
12#[derive(Debug, Default, Clone)]
14pub struct HttpClient {
15 country: String,
16}
17
18impl HttpClient {
19 pub fn new() -> Self {
20 Self {
21 country: "us".into(),
22 }
23 }
24
25 pub fn with_country(mut self, country: impl Into<String>) -> Self {
26 self.country = country.into();
27 self
28 }
29}
30
31impl AppStoreClient for HttpClient {
32 fn login(&self, req: &LoginRequest) -> Result<AuthInfo> {
33 store::login(req)
34 }
35
36 fn auth_info(&self, _keychain_passphrase: Option<&str>) -> Result<AuthInfo> {
37 store::auth_info()
38 }
39
40 fn revoke(&self) -> Result<()> {
41 store::revoke()
42 }
43
44 fn search(
45 &self,
46 term: &str,
47 limit: u32,
48 _keychain_passphrase: Option<&str>,
49 ) -> Result<SearchResult> {
50 let mut params = BTreeMap::new();
51 params.insert("term".into(), term.into());
52 params.insert("entity".into(), "software,iPadSoftware".into());
53 params.insert("limit".into(), limit.to_string());
54 params.insert("country".into(), self.country.clone());
55 let url = format!("https://itunes.apple.com/search?{}", build_query(¶ms));
56 let body = http_get(&url)?;
57 Ok(parse_search_json(&body))
58 }
59
60 fn purchase(
61 &self,
62 app_id: Option<i64>,
63 _bundle_id: Option<&str>,
64 _keychain_passphrase: Option<&str>,
65 ) -> Result<()> {
66 let id = app_id.ok_or_else(|| IpatoolError::msg("app id required"))?;
67 store::purchase(id)
68 }
69
70 fn download(&self, req: &DownloadRequest) -> Result<String> {
71 store::download(req)
72 }
73
74 fn list_versions(
75 &self,
76 app_id: Option<i64>,
77 _bundle_id: Option<&str>,
78 _keychain_passphrase: Option<&str>,
79 ) -> Result<Vec<String>> {
80 let id = app_id.ok_or_else(|| IpatoolError::msg("app id required"))?;
81 store::list_versions(id)
82 }
83
84 fn get_version_metadata(
85 &self,
86 app_id: Option<i64>,
87 _bundle_id: Option<&str>,
88 external_version_id: &str,
89 _keychain_passphrase: Option<&str>,
90 ) -> Result<VersionInfo> {
91 let id = app_id.ok_or_else(|| IpatoolError::msg("app id required"))?;
92 store::get_version_metadata(id, external_version_id)
93 }
94
95 fn list_purchases(&self, _keychain_passphrase: Option<&str>) -> Result<Vec<crate::OwnedApp>> {
96 store::list_purchases()
97 }
98}
99
100fn http_get(url: &str) -> Result<String> {
101 let resp = ureq::get(url)
102 .call()
103 .map_err(|e| IpatoolError::Http(e.to_string()))?;
104 resp.into_string()
105 .map_err(|e| IpatoolError::Http(e.to_string()))
106}