Skip to content

Commit

Permalink
refactor: Make parameters better and adds an example
Browse files Browse the repository at this point in the history
  • Loading branch information
Day-OS committed Mar 27, 2024
1 parent 9b9fc45 commit 3725001
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 17 deletions.
40 changes: 34 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,48 @@ http:
| method | POST | String | POST, PUT |
| endpoint | - | String | HTTP URL endpoint |
| headers | - | Array\<String\> | Request header(s) "Key:Value" pairs |
| params | - | Array\<Mapping\>| Dynamic URL parameters gathered from a json response|
| url_parameters | - | Array\<Mapping\>| Dynamic URL parameters gathered from a json response|
| user-agent | `fluvio/http-sink 0.2.2` | String | Request user-agent |
| http_request_timeout | 1s | String | HTTP Request Timeout |
| http_connect_timeout | 15s | String | HTTP Connect Timeout |

#### Parameter Configuration
#### URL Parameter Configuration
| Option | default | type | description |
| :--------------------| :--------------------------| :-------------- | :-------------------------------------------------|
| key | - | String | The JSON key to get the value for the parameter |
| replace | - | String | Parameter's new name |
| prefix | - | String | String to be added before the value |
| record_key | - | String | A key from a JSON record that will be used to insert a value to the parameter |
| url_key | - | String | Parameter's key name |
| prefix | - | String | String to be added before the value |
| suffix | - | String | String to be added after the value |

Set any key from a json message that you want to include into the URL as a parameter. You can also replace the parameter name by adding ": <param name> in the end of the string, like "json_key : new_key_name"
###### Example: Inserting an ID from a JSON record into a URL Parameter

Let's assume a scenario that the following endpoint requires a SQL condition to update a piece of information: `https://someurl.com/`

It accepts `updatecondition=<SQL CONDITION HERE>` as a parameter to set the condition for the exact row that we want to update.

We have the following JSON record that we want to get updated:
```json
{
"id": 2901,
"name": "Luiz Barros Rocha",
"age": 26
}
```
We could write a url_parameter containing the following information:
```yaml
{
url_parameters:
- url_key: updatecondition
record_key: id
prefix: "user_id = "
}
```

This would be the ending result:
```md
https://someurl.com?updatecondition=user_id%20%3D%202901
```


> By default HTTP headers will use `Content-Type: text/html` unless anothed value
> is provided to the Headers configuration.
Expand Down
7 changes: 4 additions & 3 deletions crates/http-sink/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,14 @@ pub(crate) struct HttpConfig {

//HTTP Parameters that can be gattered from a Message if the message is a json file
#[serde(default = "default_http_params")]
pub params: Vec<Parameter>,
pub url_parameters: Vec<Parameter>,
}

#[derive(Deserialize, Debug, Clone)]
pub struct Parameter{
pub key: String,
pub replace: Option<String>,
/// The key that will be get from
pub record_key: String,
pub url_key: Option<String>,
pub prefix: Option<String>,
pub suffix: Option<String>
}
Expand Down
16 changes: 8 additions & 8 deletions crates/http-sink/src/sink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl HttpSink {
request = request.header(key, value.trim());
}

Ok(Self { body: Body{request, params: config.params.clone()} })
Ok(Self { body: Body{request, params: config.url_parameters.clone()} })
}
}

Expand All @@ -55,23 +55,22 @@ impl Sink<String> for HttpSink {
if params.len() > 0 {
if let Ok(json_message) = serde_json::from_str::<HashMap<String, serde_json::Value>>(&record){
for param in params.into_iter() {
let key = param.replace.unwrap_or(param.key.clone());
if json_message.contains_key(&param.key){
let mut value = json_message.get(&param.key).unwrap().to_string();
let url_key = param.url_key.unwrap_or(param.record_key.clone());
if json_message.contains_key(&param.record_key){
let mut value = json_message.get(&param.record_key).unwrap().to_string();
if let Some(prefix) = param.prefix{
value = prefix + &value;
}
if let Some(suffix) = param.suffix{
value = value + &suffix;
}
body.request = body.request.query(&[(encode(&key), encode(&value))]);
body.request = body.request.query(&[(encode(&url_key),&value)]);

}
}
}
}

tracing::trace!("{:?}", body.request);
tracing::info!("{:?}", body.request);

body.request = body.request.body(record);
let response = body.request
Expand Down Expand Up @@ -109,7 +108,7 @@ mod test {
headers: vec!["Content-Type: text/html".into()],
http_connect_timeout: Duration::from_secs(1),
http_request_timeout: Duration::from_secs(15),
params: vec![]
url_parameters: vec![]
};
let sink = HttpSink::new(&config).unwrap();
let req = sink.body.request.build().unwrap();
Expand All @@ -122,4 +121,5 @@ mod test {
assert_eq!(req.method().to_string(), "POST");
assert_eq!(req.url().to_string(), "http://localhost:8080/");
}

}
7 changes: 7 additions & 0 deletions tests/integration-sends-data-via-post.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,10 @@ meta:
http:
endpoint: http://localhost:8080
interval: 3s
url_parameters:
- record_key: id
url_key: condition
prefix: "user_id = "
- record_key: age
url_key: age
suffix: " years"

0 comments on commit 3725001

Please sign in to comment.