@arrirpc/codegen-rust 0.55.0 → 0.57.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. package/README.md +79 -9
  2. package/package.json +3 -3
package/README.md CHANGED
@@ -37,31 +37,101 @@ cargo add arri_client
37
37
 
38
38
  ## Using the generated code
39
39
 
40
- ### Initialize the client
40
+ All of the generated procedures in this client will be async functions, so you will need an async runtime like [tokio](https://tokio.rs/)
41
+
42
+ ### Initializing the client
41
43
 
42
44
  ```rust
43
45
  let config = ArriClientConfig {
44
46
  http_client: reqwest::Client::new(),
45
47
  base_url: "https://example.com".to_string(),
46
- // this function will run before every request
47
- headers: || {
48
- let mut header_map = Hashmap::<&'static str, &'static str>::new();
49
- header_map.insert("some-header", "some-header-value");
50
- header_map
51
- }
48
+ headers: Hashmap::new(),
52
49
  }
53
- let client = MyClient::create(&config);
50
+ let client = MyClient::create(config);
54
51
 
52
+ // start calling procedures
55
53
  client.my_procedure().await;
56
54
  ```
57
55
 
58
56
  The root client will be a struct containing all of the services and procedures. If you only need a particular service you can initialize just that service.
59
57
 
60
58
  ```rust
61
- let users_service = MyClientUsersService(&config);
59
+ let users_service = MyClientUsersService(config);
62
60
  users_service.some_procedure().await;
63
61
  ```
64
62
 
63
+ ### Updating Headers
64
+
65
+ For instances that you need to update the http headers (like in the case of an expired auth token), you can call the `update_headers()` function. When called, changes will propagate to all nested subservices.
66
+
67
+ ```rust
68
+ client.update_headers(new_headers);
69
+ ```
70
+
71
+ Be aware that if `update_headers()` is called from a subservice it will not propagate up to the parent service(s).
72
+
73
+ ```rust
74
+ client.subservice.update_headers(new_headers);
75
+
76
+ // this will still use the original headers
77
+ client.do_something();
78
+ ```
79
+
80
+ `update_headers()` be also be called across threads.
81
+
82
+ ```rust
83
+ let mut headers: HashMap<&'static str, String> = HashMap::new();
84
+ let config = ArriClientConfig {
85
+ http_client: reqwest::Client::new(),
86
+ base_url: "https://example.com".to_string(),
87
+ headers: headers.clone(),
88
+ }
89
+ let client = Arc::new(MyClient::create(config));
90
+ tokio::spawn(async move {
91
+ loop {
92
+ client.do_something().await;
93
+ }
94
+ });
95
+ tokio::spawn(async move {
96
+ loop {
97
+ client.do_another_thing().await;
98
+ }
99
+ });
100
+
101
+ // wait two seconds then change the headers
102
+ tokio::time::sleep(Duration::from_millis(2000)).await;
103
+ headers.insert("hello", "world".to_string());
104
+ client.update_headers(headers.clone());
105
+ // now both threads will start using the updated headers on their next loop
106
+ ```
107
+
108
+ ### Calling SSE Procedures
109
+
110
+ ```rust
111
+ let mut msg_count = 0;
112
+ let mut open_count = 0;
113
+ client
114
+ .users
115
+ .watch_user(
116
+ &mut |event, controller| match event {
117
+ SseEvent::Message(msg) => {
118
+ msg_count += 1;
119
+ printl("NEW_MESSAGE: {:?}", msg);
120
+ }
121
+ SSeEvent::Error(err) => {
122
+ // call abort to close the event stream
123
+ controller.abort()
124
+ }
125
+ SseEvent::Open => {
126
+ open_count += 1;
127
+ }
128
+ SseEvent::Close => {}
129
+ },
130
+ None, // max_retry_count (u64)
131
+ None, // max_retry_interval (u64)
132
+ ).await;
133
+ ```
134
+
65
135
  ### Using the generated types
66
136
 
67
137
  All the generated types will have the following methods implemented
package/package.json CHANGED
@@ -22,10 +22,10 @@
22
22
  ],
23
23
  "dependencies": {
24
24
  "pathe": "^1.1.2",
25
- "@arrirpc/codegen-utils": "0.55.0"
25
+ "@arrirpc/codegen-utils": "0.57.0"
26
26
  },
27
27
  "devDependencies": {
28
- "@arrirpc/schema": "0.55.0"
28
+ "@arrirpc/schema": "0.57.0"
29
29
  },
30
- "version": "0.55.0"
30
+ "version": "0.57.0"
31
31
  }