# space-lib

**space-lib** is a crate that abstracts the low level details of calling our custom host functions and includes some nice helper functions. The crate is located here: [docs.rs/space-lib](https://docs.rs/space-lib).

It currently includes a HTTP client that is used for sending requests and receiving responses to websites.

## HTTP Client

```rust
use space_lib::Request;
 
let body = Request::get("https://www.spaceoperator.com")
    .call()?
    .into_string()?;
```

There are multiple ways to create a Request, and `get` is one of them. There's also `post`, `delete`, `head`, `patch` and `put`. To set a header field, use the set method like so:

```rust
use space_lib::Request;
 
let body = Request::get("https://www.spaceoperator.com")
    .set("Authorization", "Bearer 12345")?;
```

For setting a query field, use the `query` method. To send any data with the request, use any of these methods: `send_bytes`, `send_string`, `send_form` and `send_json`. Note that the type need to implement `serde::Serialize` to be able to serialize them into json.

After creating the [Request](https://docs.rs/space-lib/latest/space_lib/struct.Request.html), use the `call` method. This will call the request on the host system where the Web Assembly is running. After this, you will get a [Response](https://docs.rs/space-lib/latest/space_lib/struct.Response.html).

There are three methods that can be called on [Response](https://docs.rs/space-lib/latest/space_lib/struct.Response.html): `into_vec` which returns `Vec<u8>`, `into_string` which returns `Result<String>` and `into_json` which returns `Result<T>` where T: serde::DeserializeOwned.
