Exemples¶
Dans cette section, vous trouverez des exemples de code dans les langages Python et JavaScript pour appeler l’API, analyser et manipuler le résultat.
import requests
import lxml.etree
# Requêtes GET
## Resources
### Liste des ressources sans keywords
response = requests.get(url="http://127.0.0.1:8000/resources/")
response_status_code = response.status_code
response_content = response.json()
resources = response_content["resources"]
resources_id = [resource["id"] for resource in resources]
print(response_status_code)
print(response_content)
print(resources)
print(resources_id)
### Liste des ressources avec keywords
params = {"keywords": "mnt"}
response = requests.get(url="http://127.0.0.1:8000/resources/", params=params)
### Informations sur une ressource
id_resource = "rgealti"
response = requests.get(url=f"http://127.0.0.1:8000/resources/{id_resource}")
resource = response.json()
resource_pyramids = resource["pyramids"]
print(resource_pyramids)
## Elevations
format = "json"
lon = "1.48|1.49"
lat = "43.54|43.55"
resource = "rgealti"
delimiter = "|"
indent = "false"
measures = "false"
zonly = "false"
### Récupération des altitudes et diverses informations (zonly = false et measures = false) au format json
response = requests.get(url=f"http://127.0.0.1:8000/calcul/alti/rest/elevation.{format}", params={
"lon": lon,
"lat": lat,
"resource": resource,
"delimiter": delimiter,
"indent": indent,
"measures": measures,
"zonly": zonly
})
elevations = response.json()["elevations"]
for elevation in elevations:
print(elevation["lon"])
print(elevation["lat"])
print(elevation["z"])
print(elevation["acc"])
### Récupération uniquement des altitudes (zonly = true)
zonly = "true"
response = requests.get(url=f"http://127.0.0.1:8000/calcul/alti/rest/elevation.{format}", params={
"lon": lon,
"lat": lat,
"resource": resource,
"zonly": zonly
})
elevations = response.json()["elevations"]
for elevation in elevations:
print(elevation)
### Récupération d'altitudes sans measures au format xml
format = "xml"
response = requests.get(url=f"http://127.0.0.1:8000/calcul/alti/rest/elevation.{format}", params={
"lon": lon,
"lat": lat,
"resource": resource,
})
elevations_string = response.text
elevations_xml = lxml.etree.fromstring(response.text)
print(elevations_string)
print(elevations_xml)
### Récupération d'altitudes avec measures
format = "json"
measures = "true"
response = requests.get(url=f"http://127.0.0.1:8000/calcul/alti/rest/elevation.{format}", params={
"lon": lon,
"lat": lat,
"measures": measures,
"resource": resource,
})
elevations = response.json()["elevations"]
for elevation in elevations:
for measure in elevation["measures"]:
print(measure["z"])
print(measure["source_name"])
print(measure["source_measure"])
print(measure["acc"])
print(measure["title"])
### Récupération profil altimétrique sans mesures
measures = "false"
profile_mode = "simple"
sampling = 4
response = requests.get(url=f"http://127.0.0.1:8000/calcul/alti/rest/elevationLine.{format}", params={
"lon": lon,
"lat": lat,
"resource": resource,
"delimiter": delimiter,
"indent": indent,
"measures": measures,
"profile_mode": profile_mode,
"sampling": sampling
})
elevations = response.json()["elevations"]
count_elevations = len(elevations)
height_differences = response.json()["height_differences"]
height_difference_positive = height_differences["positive"]
height_difference_negative = height_differences["negative"]
print(elevations)
print(count_elevations)
print(height_differences)
print(height_difference_positive)
print(height_difference_negative)
# Requêtes POST
## Elevations
### Récupération d'altitudes
body = {
"lon": lon,
"lat": lat,
"resource": resource,
"delimiter": delimiter,
"indent": indent,
"measures": measures,
"zonly": zonly
}
response = requests.post(url=f"http://127.0.0.1:8000/calcul/alti/rest/elevation.{format}", json=body)
### Récupération profil altimétrique
body = {
"lon": lon,
"lat": lat,
"resource": resource,
"delimiter": delimiter,
"indent": indent,
"measures": measures,
"profile_mode": profile_mode,
"sampling": sampling
}
response = requests.post(url=f"http://127.0.0.1:8000/calcul/alti/rest/elevationLine.{format}", json=body)
# Requêtes HEAD
response = requests.head('http://127.0.0.1:8000/resources/')
response_status_code = response.status_code
response_content_length = response.headers["content-length"]
print(response_status_code)
print(response_content_length)
# Requêtes OPTIONS
response = requests.options('http://127.0.0.1:8000/resources/')
// Requêtes GET
/// Resources
//// Liste des ressources sans keywords
let responseStatus;
fetch("http://127.0.0.1:8000/resources/")
.then((response) => {
responseStatus = response.status;
return response.json();
})
.then((json) => {
let responseContent = json;
let resources = responseContent["resources"];
console.log(responseStatus);
console.log(responseContent);
console.log(resources);
let resourcesId = [];
resources.forEach((element) => {
resourcesId.push(element.id);
});
console.log(resourcesId);
});
//// Liste des ressources avec keywords
let params = { keywords: "mnt" };
fetch("http://127.0.0.1:8000/resources/?" + new URLSearchParams(params))
.then((response) => {
return response.json();
})
.then((json) => {
let responseContent = json;
});
//// Informations sur une ressource
let idResource = "rgealti";
fetch(`http://127.0.0.1:8000/resources/${idResource}`)
.then((response) => {
return response.json();
})
.then((json) => {
let responseContent = json;
let responsePyramids = responseContent["pyramids"];
console.log(responsePyramids);
}
);
/// Elevations
let format = "json";
let lon = "1.48|1.49";
let lat = "43.54|43.55";
let resource = "rgealti";
let delimiter = "|";
let indent = "false";
let measures = "false";
let zonly = "false";
//// Récupération des altitudes et diverses informations (zonly = false et measures = false) au format json
fetch(
`http://127.0.0.1:8000/calcul/alti/rest/elevation.${format}?` +
new URLSearchParams({
lon: lon,
lat: lat,
resource: resource,
delimiter: delimiter,
indent: indent,
measures: measures,
zonly: zonly,
})
)
.then((response) => {
return response.json();
})
.then((json) => {
let responseContent = json;
let elevations = responseContent["elevations"];
elevations.forEach((elevation) => {
console.log(elevation["lon"]);
console.log(elevation["lat"]);
console.log(elevation["z"]);
console.log(elevation["acc"]);
});
});
//// Récupération uniquement des altitudes (zonly = true)
let zonly = "true";
fetch(`http://127.0.0.1:8000/calcul/alti/rest/elevation.${format}?` + new URLSearchParams({
"lon": lon,
"lat": lat,
"resource": resource,
"zonly": zonly
}))
.then((response) => {
return response.json();
})
.then((json) => {
let responseContent = json;
let elevations = responseContent["elevations"];
elevations.forEach((elevation) => {
console.log(elevation);
});
});
//// Récupération d'altitudes avec measures
let format = "json";
let measures = "true";
fetch(
`http://127.0.0.1:8000/calcul/alti/rest/elevation.${format}?` +
new URLSearchParams({
lon: lon,
lat: lat,
measures: measures,
resource: resource,
})
)
.then((response) => {
return response.json();
})
.then((json) => {
let responseContent = json;
let elevations = responseContent["elevations"];
elevations.forEach((elevation) => {
elevation["measures"].forEach((measure) => {
console.log(measure["z"]);
console.log(measure["source_name"]);
console.log(measure["source_measure"]);
console.log(measure["acc"]);
console.log(measure["title"]);
});
});
});
//// Récupération profil altimétrique sans mesures
let measures = "false";
let profile_mode = "simple";
let sampling = 4;
fetch(
`http://127.0.0.1:8000/calcul/alti/rest/elevationLine.${format}?` +
new URLSearchParams({
"lon": lon,
"lat": lat,
"resource": resource,
"delimiter": delimiter,
"indent": indent,
"measures": measures,
"profile_mode": profile_mode,
"sampling": sampling
})
)
.then((response) => {
return response.json();
})
.then((json) => {
let responseContent = json;
let elevations = responseContent["elevations"];
let countElevations = elevations.length;
let heightDifferences = responseContent["height_differences"];
let heightDifferencePositive = heightDifferences["positive"];
let heightDifferenceNegative = heightDifferences["negative"];
console.log(elevations);
console.log(countElevations);
console.log(heightDifferences);
console.log(heightDifferencePositive);
console.log(heightDifferenceNegative);
});
// Requêtes POST
/// Elevations
//// Récupération d'altitudes
let options = {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
body: JSON.stringify({
lon: lon,
lat: lat,
resource: resource,
delimiter: delimiter,
indent: indent,
measures: measures,
zonly: zonly,
}),
};
fetch(`http://127.0.0.1:8000/calcul/alti/rest/elevation.${format}`, options)
.then((response) => {
return response.json();
})
.then((json) => {
let responseContent = json;
let elevations = responseContent["elevations"];
});
//// Récupération profil altimétrique
let options = {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
body: JSON.stringify({
lon: lon,
lat: lat,
resource: resource,
delimiter: delimiter,
indent: indent,
measures: measures,
profile_mode: profile_mode,
sampling: sampling,
}),
};
fetch(`http://127.0.0.1:8000/calcul/alti/rest/elevationLine.${format}`, options)
.then((response) => {
return response.json();
})
.then((json) => {
let responseContent = json;
let elevations = responseContent["elevations"];
});
// Requêtes HEAD
let responseStatus;
let responseContentLength;
let options = {
method: "HEAD",
};
fetch("http://127.0.0.1:8000/resources/", options).then((response) => {
console.log(response);
responseStatus = response.status;
responseContentLength = response.headers.get("content-length");
console.log(responseStatus);
console.log(responseContentLength);
});
// Requêtes OPTIONS
let options = {
method: "OPTIONS",
};
fetch("http://127.0.0.1:8000/resources/", options).then((response) => {
console.log(response);
});