|
1 | 1 | use crate::{ |
2 | | - errors::HttpClientError, |
3 | | - response::{ReadiedResponse, RESPONSE_CLASS_NAME}, |
| 2 | + errors::HttpClientError, replace_and_get, replace_and_set, request::REQUEST_BUILDER_CLASS_NAME, |
4 | 3 | }; |
5 | | - |
6 | | -use phper::{classes::DynamicClass, functions::Argument, objects::Object}; |
7 | | -use reqwest::blocking::{Client, ClientBuilder}; |
| 4 | +use phper::{ |
| 5 | + classes::{ClassEntry, DynamicClass, Visibility}, |
| 6 | + functions::Argument, |
| 7 | + objects::Object, |
| 8 | +}; |
| 9 | +use reqwest::blocking::{Client, ClientBuilder, RequestBuilder}; |
8 | 10 | use std::time::Duration; |
9 | 11 |
|
| 12 | +const HTTP_CLIENT_BUILDER_CLASS_NAME: &'static str = "HttpClient\\HttpClientBuilder"; |
10 | 13 | const HTTP_CLIENT_CLASS_NAME: &'static str = "HttpClient\\HttpClient"; |
11 | 14 |
|
12 | | -pub fn make_client_class() -> DynamicClass<Client> { |
13 | | - let mut class = DynamicClass::new_with_constructor(HTTP_CLIENT_CLASS_NAME, || { |
14 | | - let client = ClientBuilder::new() |
15 | | - .timeout(Duration::from_secs(15)) |
16 | | - .build()?; |
17 | | - Ok::<_, HttpClientError>(client) |
18 | | - }); |
| 15 | +pub fn make_client_builder_class() -> DynamicClass<ClientBuilder> { |
| 16 | + let mut class = DynamicClass::new_with_default(HTTP_CLIENT_BUILDER_CLASS_NAME); |
19 | 17 |
|
20 | 18 | class.add_method( |
21 | | - "get", |
| 19 | + "timeout", |
| 20 | + Visibility::Public, |
22 | 21 | |this, arguments| { |
23 | | - let url = arguments[0].as_string()?; |
24 | | - let client = this.as_state(); |
25 | | - let response = client.get(url).send()?; |
| 22 | + let ms = arguments[0].as_long()?; |
| 23 | + let state = this.as_mut_state(); |
| 24 | + replace_and_set(state, ClientBuilder::new(), |builder| { |
| 25 | + builder.timeout(Duration::from_millis(ms as u64)) |
| 26 | + }); |
| 27 | + Ok::<_, HttpClientError>(()) |
| 28 | + }, |
| 29 | + vec![Argument::by_val("ms")], |
| 30 | + ); |
| 31 | + |
| 32 | + class.add_method( |
| 33 | + "cookie_store", |
| 34 | + Visibility::Public, |
| 35 | + |this, arguments| { |
| 36 | + let enable = arguments[0].as_bool()?; |
| 37 | + let state = this.as_mut_state(); |
| 38 | + replace_and_set(state, ClientBuilder::new(), |builder| { |
| 39 | + builder.cookie_store(enable) |
| 40 | + }); |
| 41 | + Ok::<_, HttpClientError>(()) |
| 42 | + }, |
| 43 | + vec![Argument::by_val("enable")], |
| 44 | + ); |
26 | 45 |
|
27 | | - let readied_response = ReadiedResponse { |
28 | | - status: response.status(), |
29 | | - remote_addr: response.remote_addr(), |
30 | | - headers: response.headers().clone(), |
31 | | - body: response.bytes()?, |
32 | | - }; |
| 46 | + class.add_method( |
| 47 | + "build", |
| 48 | + Visibility::Public, |
| 49 | + |this, _arguments| { |
| 50 | + let state = this.as_mut_state(); |
| 51 | + let client = replace_and_get(state, ClientBuilder::new(), ClientBuilder::build)?; |
| 52 | + let mut object = |
| 53 | + ClassEntry::<Option<Client>>::from_globals(HTTP_CLIENT_CLASS_NAME)?.new_object(); |
| 54 | + *object.as_mut_state() = Some(client); |
| 55 | + Ok::<_, HttpClientError>(object) |
| 56 | + }, |
| 57 | + vec![], |
| 58 | + ); |
33 | 59 |
|
34 | | - let mut response_object = |
35 | | - Object::<Option<ReadiedResponse>>::new_by_class_name(RESPONSE_CLASS_NAME)?; |
36 | | - *response_object.as_mut_state() = Some(readied_response); |
| 60 | + class |
| 61 | +} |
37 | 62 |
|
38 | | - Ok::<_, HttpClientError>(response_object) |
| 63 | +pub fn make_client_class() -> DynamicClass<Option<Client>> { |
| 64 | + let mut class = DynamicClass::new_with_none(HTTP_CLIENT_CLASS_NAME); |
| 65 | + |
| 66 | + class.add_method( |
| 67 | + "__construct", |
| 68 | + Visibility::Private, |
| 69 | + |_: &mut Object<Option<Client>>, _| {}, |
| 70 | + vec![], |
| 71 | + ); |
| 72 | + |
| 73 | + class.add_method( |
| 74 | + "get", |
| 75 | + Visibility::Public, |
| 76 | + |this, arguments| { |
| 77 | + let url = arguments[0].as_string()?; |
| 78 | + let client = this.as_state().as_ref().unwrap(); |
| 79 | + let request_builder = client.get(url); |
| 80 | + let mut object = |
| 81 | + ClassEntry::<Option<RequestBuilder>>::from_globals(REQUEST_BUILDER_CLASS_NAME)? |
| 82 | + .new_object(); |
| 83 | + *object.as_mut_state() = Some(request_builder); |
| 84 | + Ok::<_, HttpClientError>(object) |
| 85 | + }, |
| 86 | + vec![Argument::by_val("url")], |
| 87 | + ); |
| 88 | + |
| 89 | + class.add_method( |
| 90 | + "post", |
| 91 | + Visibility::Public, |
| 92 | + |this, arguments| { |
| 93 | + let url = arguments[0].as_string()?; |
| 94 | + let client = this.as_state().as_ref().unwrap(); |
| 95 | + let request_builder = client.post(url); |
| 96 | + let mut object = |
| 97 | + ClassEntry::<Option<RequestBuilder>>::from_globals(REQUEST_BUILDER_CLASS_NAME)? |
| 98 | + .new_object(); |
| 99 | + *object.as_mut_state() = Some(request_builder); |
| 100 | + Ok::<_, HttpClientError>(object) |
39 | 101 | }, |
40 | 102 | vec![Argument::by_val("url")], |
41 | 103 | ); |
|
0 commit comments