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
//! A lightweight wrapper over some networking components, like `NSURLRequest` and co.
//!
use objc::runtime::Object;
/// At the moment, this is mostly used for inspection of objects returned from system
/// calls, as `NSURL` is pervasive in some filesystem references. Over time this may grow to
/// include a proper networking stack, but the expectation for v0.1 is that most apps will want to
/// use their standard Rust networking libraries (however... odd... the async story may be).
use objc::{msg_send, sel, sel_impl};
use objc_id::ShareId;

use crate::foundation::{id, NSString};

/// A wrapper around `NSURLRequest`.
#[derive(Debug)]
pub struct URLRequest(ShareId<Object>);

impl URLRequest {
    /// Wraps and retains an `NSURLRequest`.
    pub fn with(request: id) -> Self {
        URLRequest(unsafe { ShareId::from_ptr(request) })
    }

    /// Returns the underlying request URL as an owned `String`.
    pub fn absolute_url(&self) -> String {
        NSString::from_retained(unsafe {
            let url: id = msg_send![&*self.0, URL];
            msg_send![url, absoluteString]
        })
        .to_string()
    }
}

#[cfg(test)]
mod tests {
    use objc::{class, msg_send, sel, sel_impl};

    use crate::foundation::{id, NSString};
    use crate::networking::URLRequest;

    #[test]
    fn test_urlrequest() {
        let endpoint = "https://rymc.io/";

        let url = unsafe {
            let url = NSString::new(endpoint);
            let url: id = msg_send![class!(NSURL), URLWithString:&*url];
            URLRequest::with(msg_send![class!(NSURLRequest), requestWithURL: url])
        };

        assert_eq!(&url.absolute_url(), endpoint);
    }
}