I am using Wiremock from docker compose
wiremock:
image: "wiremock/wiremock:latest"
ports:
- "8095:8080"
container_name: wiremock
volumes:
- ./wiremock/extensions:/var/wiremock/extensions
- ./wiremock/__files:/home/wiremock/__files
- ./wiremock/mappings:/home/wiremock/mappings
entrypoint: ["/docker-entrypoint.sh", "--global-response-templating", "--disable-gzip", "--verbose"]
I have a request that contains a Date and I want to return the next 3 hours in the response
I can allready return a list of 3 nodes in the json response but I cant find a way to calculate the next 3 hours. Is this feasible?
Request
/nexthours?deliveryDateUtc=2024-05-20T10:00:00Z
Expected Response
{
"next3hours":[
{"position":"1","timestamp":"2024-05-20T10:00:00Z"},
{"position":"2","timestamp":"2024-05-20T11:00:00Z",
{"position":"3","timestamp":"2024-05-20T12:00:00Z"
]
}
file in mapping folder (nexthours.json)
{
"request": {
"method": "GET",
"urlPath": "/nexthours"
},
"response": {
"status": 200,
"headers": {
"Content-Type": "application/json"
},
"bodyFileName": "nexthourstemplate.json",
"transformers": ["response-template"]
}
}
file in __files (nexthourstemplate.json)
{
"next3hours": [
{{#each (range 1 3)}}
{
"position": {{this}},
"timestamp": "{{request.query.deliveryDateUtc}}"
}{{#unless @last}},{{/unless}}
{{/each}}
]
}
This gives me
{
"next3hours":[
{"position":"1","timestamp":"2024-05-20T10:00:00Z"},
{"position":"2","timestamp":"2024-05-20T10:00:00Z",
{"position":"3","timestamp":"2024-05-20T10:00:00Z"
]
}
but I dont see a way to calculate the value for timestamp. Is this feasible?
The trick here is to parse the date and provide an offset
. Offsets can be things like 3 days
or 1 years
but in this case we can use the hours
offset.
For your example, we will need to base the offset off of the range we are looping through. Here is the updated version of your nexthourstemplate.json
:
{
"next3hours": [
{{#each (range 1 3)}}
{{#assign 'currentOffset'}}{{this}} hours{{/assign}}
{
"position": {{this}},
"timestamp": "{{date (parseDate request.query.deliveryDateUtc) offset=(lookup currentOffset) }}"
}{{#unless @last}},{{/unless}}
{{/each}}
]
}
As you can see, the first thing we do is construct the offset and assign it to the variable currentOffset
. We then use the date
helper and the parseDate
helper to parse the date passed as the query parameter using the offest we generated:
{{date (parseDate request.query.deliveryDateUtc) offset=(lookup currentOffset) }}
Using your original request of /nexthours?deliveryDateUtc=2024-05-20T10:00:00Z
this should return the following json:
{
"next3hours": [
{
"position": 1,
"timestamp": "2024-05-20T11:00:00Z"
},
{
"position": 2,
"timestamp": "2024-05-20T12:00:00Z"
},
{
"position": 3,
"timestamp": "2024-05-20T13:00:00Z"
}
]
}