Description
This PR adds a very basic HTTP client which allows us to make requests to an actual local Ambi server.
The URL is hardcoded at the moment as http://localhost:4000/api/readings/add
which is the default endpoint for adding readings when running Ambi locally.
I also updated the usage notes in the README
it works
ambi_mock_client
caleb@calebs-MacBook-Pro ambi_mock_client % ./target/debug/ambi_mock_client
Sending POST request to http://localhost:4000/api/readings/add as JSON: {"tempurature":"28.8","humidity":"85.2","pressure":"964","dust_concentration":"930","air_purity":"DANGEROUS"}
Response: Ok(
Response {
url: Url {
scheme: "http",
cannot_be_a_base: false,
username: "",
password: None,
host: Some(
Domain(
"localhost",
),
),
port: Some(
4000,
),
path: "/api/readings/add",
query: None,
fragment: None,
},
status: 200,
headers: {
"cache-control": "max-age=0, private, must-revalidate",
"content-length": "60",
"content-type": "application/json; charset=utf-8",
"date": "Sat, 22 Jan 2022 19:28:08 GMT",
"server": "Cowboy",
"x-request-id": "FsyuX1U1NOj9m7IAAABB",
},
},
)
Ambi
[info] POST /api/readings/add
Verb: "POST"
Host: "localhost"
Headers: [{"accept", "*/*"}, {"content-length", "109"}, {"host", "localhost:4000"}]
[debug] Processing with AmbiWeb.ApiController.add_reading/2
Parameters: %{}
Pipelines: [:api]
[debug] Added new reading to the DB
[info] Sent 200 in 1ms
Testing
I decided to not write tests for this step in the MVP
Two options I considered were:
- Finding library to provide some sort of mock that I could send requests to in tests.
- This seemed like overkill a bit for a CLI at this stage
- Use dependency injection and define a test Client
- It would be really cool if reqwest provided a trait for Client that we could use to create a test client. For the moment this felt like overkill as well.
Also, since we can assume the functions being used that are provided by reqwest are themselves tested within that crate maybe it does not make sense to re-test them here. We would essentially be writing a test for the
main
function.