Repository navigation
Communication Contract
Alex Holly edited this page Nov 21, 2023
·
3 revisions
Coordinate Calculator Microservice
This is a flask service that can be deployed onto your local computer and will respond to requests sent to it with longitude and latitude coordinates.
REQUESTING DATA -- A request is made to this serice in the way of a http post request. This can be sent through another API, or directly from javascript. The service is expecting a JSON dictionary that includes 'lat1', 'lat2', 'lon1', and 'lon2' keys. Each with its corresponding value.
Javascript call with flask
const formData = {
lat1: document.getElementById('lat1').value,
lon1: document.getElementById('lon1').value,
lat2: document.getElementById('lat2').value,
lon2: document.getElementById('lon2').value,
};
fetch('/make_request', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(formData), //this line adds the data into the body of the request
}
Flask API call
data = {
lat1: lat1value,
lon1: lon1value,
lat2: lat2value,
lon2: lon2value
};
response = requests.post('http://localhost:12121/modify_data', json=data)
## This URL should match the port number that this microservice is deployed on your computer.
RECEIVING DATA -- The outputed data will be sent back in a JSON dictionary. It will only have one key which is 'result'. The value of this key will be the distance between the two points in miles.
Javascript call with flask
fetch('/make_request', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(formData), //this line adds the data into the body of the request
})
.then(response => response.json()) //handeling the response
.then(data => {
// Update the response container with the data received from the server
// I have a container but you can just print the result for testing
responseContainer.innerHTML = data['result'];
})
.catch(error => { //error checking
responseContainer.innerHTML += error;
console.error('Error:', error);
});
Flask API call
response = requests.post('http://localhost:12121/modify_data', json=data)
UML SEQUENCE