1use crate::Session;
6use crate::URI;
7use glib::object::IsA;
8use glib::translate::*;
9use std::boxed::Box as Box_;
10use std::fmt;
11use std::pin::Pin;
12use std::ptr;
13
14glib::wrapper! {
15 #[doc(alias = "SoupRequest")]
16 pub struct Request(Object<ffi::SoupRequest, ffi::SoupRequestClass>);
17
18 match fn {
19 type_ => || ffi::soup_request_get_type(),
20 }
21}
22
23pub const NONE_REQUEST: Option<&Request> = None;
24
25pub trait RequestExt: 'static {
26 #[doc(alias = "soup_request_get_content_length")]
27 #[doc(alias = "get_content_length")]
28 fn content_length(&self) -> i64;
29
30 #[doc(alias = "soup_request_get_content_type")]
31 #[doc(alias = "get_content_type")]
32 fn content_type(&self) -> Option<glib::GString>;
33
34 #[doc(alias = "soup_request_get_session")]
35 #[doc(alias = "get_session")]
36 fn session(&self) -> Option<Session>;
37
38 #[doc(alias = "soup_request_get_uri")]
39 #[doc(alias = "get_uri")]
40 fn uri(&self) -> Option<URI>;
41
42 #[doc(alias = "soup_request_send")]
43 fn send<P: IsA<gio::Cancellable>>(&self, cancellable: Option<&P>) -> Result<gio::InputStream, glib::Error>;
44
45 #[doc(alias = "soup_request_send_async")]
46 fn send_async<P: IsA<gio::Cancellable>, Q: FnOnce(Result<gio::InputStream, glib::Error>) + Send + 'static>(&self, cancellable: Option<&P>, callback: Q);
47
48
49 fn send_async_future(&self) -> Pin<Box_<dyn std::future::Future<Output = Result<gio::InputStream, glib::Error>> + 'static>>;
50}
51
52impl<O: IsA<Request>> RequestExt for O {
53 fn content_length(&self) -> i64 {
54 unsafe {
55 ffi::soup_request_get_content_length(self.as_ref().to_glib_none().0)
56 }
57 }
58
59 fn content_type(&self) -> Option<glib::GString> {
60 unsafe {
61 from_glib_none(ffi::soup_request_get_content_type(self.as_ref().to_glib_none().0))
62 }
63 }
64
65 fn session(&self) -> Option<Session> {
66 unsafe {
67 from_glib_none(ffi::soup_request_get_session(self.as_ref().to_glib_none().0))
68 }
69 }
70
71 fn uri(&self) -> Option<URI> {
72 unsafe {
73 from_glib_none(ffi::soup_request_get_uri(self.as_ref().to_glib_none().0))
74 }
75 }
76
77 fn send<P: IsA<gio::Cancellable>>(&self, cancellable: Option<&P>) -> Result<gio::InputStream, glib::Error> {
78 unsafe {
79 let mut error = ptr::null_mut();
80 let ret = ffi::soup_request_send(self.as_ref().to_glib_none().0, cancellable.map(|p| p.as_ref()).to_glib_none().0, &mut error);
81 if error.is_null() { Ok(from_glib_full(ret)) } else { Err(from_glib_full(error)) }
82 }
83 }
84
85 fn send_async<P: IsA<gio::Cancellable>, Q: FnOnce(Result<gio::InputStream, glib::Error>) + Send + 'static>(&self, cancellable: Option<&P>, callback: Q) {
86 let user_data: Box_<Q> = Box_::new(callback);
87 unsafe extern "C" fn send_async_trampoline<Q: FnOnce(Result<gio::InputStream, glib::Error>) + Send + 'static>(_source_object: *mut glib::gobject_ffi::GObject, res: *mut gio::ffi::GAsyncResult, user_data: glib::ffi::gpointer) {
88 let mut error = ptr::null_mut();
89 let ret = ffi::soup_request_send_finish(_source_object as *mut _, res, &mut error);
90 let result = if error.is_null() { Ok(from_glib_full(ret)) } else { Err(from_glib_full(error)) };
91 let callback: Box_<Q> = Box_::from_raw(user_data as *mut _);
92 callback(result);
93 }
94 let callback = send_async_trampoline::<Q>;
95 unsafe {
96 ffi::soup_request_send_async(self.as_ref().to_glib_none().0, cancellable.map(|p| p.as_ref()).to_glib_none().0, Some(callback), Box_::into_raw(user_data) as *mut _);
97 }
98 }
99
100
101 fn send_async_future(&self) -> Pin<Box_<dyn std::future::Future<Output = Result<gio::InputStream, glib::Error>> + 'static>> {
102
103 Box_::pin(gio::GioFuture::new(self, move |obj, cancellable, send| {
104 obj.send_async(
105 Some(cancellable),
106 move |res| {
107 send.resolve(res);
108 },
109 );
110 }))
111 }
112}
113
114impl fmt::Display for Request {
115 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
116 f.write_str("Request")
117 }
118}