Debian Codegraph @

src/desktop/proxy_resolver.rs

raw
1 //! **Note** This portal doesn't work for sandboxed applications.
2 //! # Examples
3 //!
4 //! ```rust,no_run
5 //! use ashpd::desktop::proxy_resolver::ProxyResolver;
6 //!
7 //! async fn run() -> ashpd::Result<()> {
8 //! let proxy = ProxyResolver::new().await?;
9 //! let url = ashpd::Uri::parse("www.google.com").unwrap();
10 //!
11 //! println!("{:#?}", proxy.lookup(&url).await?);
12 //!
13 //! Ok(())
14 //! }
15 //! ```
16
17 use crate::{Error, Uri, proxy::Proxy};
18
19 /// The interface provides network proxy information to sandboxed applications.
20 ///
21 /// It is not a portal in the strict sense, since it does not involve user
22 /// interaction. Applications are expected to use this interface indirectly,
23 /// via a library API such as the GLib [`gio::ProxyResolver`](https://gtk-rs.org/gtk-rs-core/stable/latest/docs/gio/struct.ProxyResolver.html) interface.
24 ///
25 /// Wrapper of the DBus interface: [`org.freedesktop.portal.ProxyResolver`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.ProxyResolver.html).
26 #[derive(Debug)]
27 #[doc(alias = "org.freedesktop.portal.ProxyResolver")]
28 pub struct ProxyResolver(Proxy<'static>);
29
30 impl ProxyResolver {
31 /// Create a new instance of [`ProxyResolver`].
32 pub async fn new() -> Result<Self, Error> {
33 let proxy = Proxy::new_desktop("org.freedesktop.portal.ProxyResolver").await?;
34 Ok(Self(proxy))
35 }
36
37 /// Create a new instance of [`ProxyResolver`].
38 pub async fn with_connection(connection: zbus::Connection) -> Result<Self, Error> {
39 let proxy =
40 Proxy::new_desktop_with_connection(connection, "org.freedesktop.portal.ProxyResolver")
41 .await?;
42 Ok(Self(proxy))
43 }
44
45 /// Returns the version of the portal interface.
46 pub fn version(&self) -> u32 {
47 self.0.version()
48 }
49
50 /// Looks up which proxy to use to connect to `uri`.
51 ///
52 /// # Returns
53 ///
54 /// A list of proxy uris of the form `protocol://[user[:password]host:port`
55 /// The protocol can be `http`, `rtsp`, `socks` or another proxying
56 /// protocol. `direct://` is used when no proxy is needed.
57 ///
58 /// # Specifications
59 ///
60 /// See also [`Lookup`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.ProxyResolver.html#org-freedesktop-portal-proxyresolver-lookup).
61 #[doc(alias = "Lookup")]
62 pub async fn lookup(&self, uri: &Uri) -> Result<Vec<Uri>, Error> {
63 self.0.call("Lookup", &(uri)).await
64 }
65 }
66
67 impl std::ops::Deref for ProxyResolver {
68 type Target = zbus::Proxy<'static>;
69
70 fn deref(&self) -> &Self::Target {
71 &self.0
72 }
73 }