ashpd/desktop/proxy_resolver.rs
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 = url::Url::parse("www.google.com").unwrap();
10//!
11//! println!("{:#?}", proxy.lookup(&url).await?);
12//!
13//! Ok(())
14//! }
15//! ```
16
17use crate::{proxy::Proxy, Error};
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")]
28pub struct ProxyResolver<'a>(Proxy<'a>);
29
30impl<'a> ProxyResolver<'a> {
31 /// Create a new instance of [`ProxyResolver`].
32 pub async fn new() -> Result<ProxyResolver<'a>, Error> {
33 let proxy = Proxy::new_desktop("org.freedesktop.portal.ProxyResolver").await?;
34 Ok(Self(proxy))
35 }
36
37 /// Looks up which proxy to use to connect to `uri`.
38 ///
39 /// # Returns
40 ///
41 /// A list of proxy uris of the form `protocol://[user[:password]host:port`
42 /// The protocol can be `http`, `rtsp`, `socks` or another proxying
43 /// protocol. `direct://` is used when no proxy is needed.
44 ///
45 /// # Specifications
46 ///
47 /// See also [`Lookup`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.ProxyResolver.html#org-freedesktop-portal-proxyresolver-lookup).
48 #[doc(alias = "Lookup")]
49 pub async fn lookup(&self, uri: &url::Url) -> Result<Vec<url::Url>, Error> {
50 self.0.call("Lookup", &(uri)).await
51 }
52}
53
54impl<'a> std::ops::Deref for ProxyResolver<'a> {
55 type Target = zbus::Proxy<'a>;
56
57 fn deref(&self) -> &Self::Target {
58 &self.0
59 }
60}