Network

http/https

If you need to use network requests (http/https) in GenUI, you need to use the plugin gen_makepad_http.

You can refer to this example: todo example

Install plugins usingRact

1ract add gen_makepad_http

After the installation is complete, a .plugins directory will appear in your GenUI project, which contains a gen_makepad_http/token.toml file.

Add required dependencies

Please add the following dependencies to the project's Cargo.toml:

Cargo.toml
1[package]
2# ...
3
4[dependencies]
5gen_macro = "0.1.0"
6serde = "1.0.217"
7lazy_static = "1.5.0"
8serde_json = "1.0.135"

Register inmain.rs

This HttpPublisher is registered globally, the name is set to http1, and the init function is used for initialization

main.rs
1use gen_macro::plugin;
2
3fn init() -> HttpPublisher {
4    let mut http = HttpPublisher::new("/url/path");
5    http.basic.protocol = Protocol::Https; // default is http
6    http
7}
8
9plugin! {
10    http1: HttpPublisher => init()
11}

Use in Component

1#[before_create]
2fn before_create(){
3    let _ = http_get!(http_response1);
4}
5
6#[http_response]
7fn http_response1(response: &HttpResponse){
8    dbg!("response");
9    if response.status_code == 200 {
10        // ...
11    }
12}

Request

The following requests are currently supported:

  • GET: http_get!
  • POST: http_post!
  • PUT: http_put!
  • DELETE: http_delete!
  • PATCH: http_patch!

These macros are used in the same way:

  1. http_get!($Id: tt)
  2. http_get!($Id: tt, $Url: expr)
  3. http_get!($Id: tt, $Url: expr, $Patch: expr)
  • $Id: the function name corresponding to the response
  • $Url: the address attached to the basic url, for example, basic_url is localhost:8888/hello, if this $Url is set to "/user/get/1", then the complete url is: localhost:8888/hello/user/get/1
  • $Patch: Into<PatchRequest>, in short, you can directly use a serializable type or PatchRquest

HttpPublisher

The request publisher, where you can configure the basic request configuration (BasicConfig)

BasicConfig

NOTE

default is:

  • protocol: Protocol::Http
  • headers: Content-Type: application/json
1#[derive(Debug, Clone)]
2pub struct BasicConfig {
3    pub protocol: Protocol,
4    pub url: String,
5    pub headers: HashMap<HttpRequestHeader, String>,
6}

If you need to add header,please use:pub fn push_header(&mut self, header: HttpRequestHeader, value: String) ;

Protocol

1#[derive(Debug, Clone, Copy)]
2pub enum Protocol {
3    Http,
4    Https,
5}

HttpRequestHeader

1#[derive(Hash, Eq, PartialEq, Debug, Clone)]
2pub enum HttpRequestHeader {
3    ContentType,
4    Authorization,
5    Other(String),
6}

PatchRequest

1#[derive(Debug, Clone, Default)]
2pub struct PatchRequest {
3    pub params: Option<HashMap<String, String>>,
4    pub headers: Option<HashMap<HttpRequestHeader, String>>,
5    pub body: Option<HttpRequestBody>,
6}

HttpRequestBody

Vec<u8> implements data serialization, which means that you can use any data serialized by serde as HttpRequestBody