Créez une application qui reconnaît des animaux
Une RESTful API utilise la même URL mais réagit différemment en fonction de la méthode HTTP. GenerateHTTPResponse fonctionne de manière récursive, donc un calcul Delayed peut retourner les différentes instances de APIFunction pour gérer différentes méthodes HTTP pour exécuter une application web qui rapporte une localisation.
Créez une application simple qui recueille des endroits où différents types d'animaux ont été repérés.
In[1]:=

animallocator = Delayed[
With[{query = HTTPRequestData["Query"]},
Switch[HTTPRequestData["Method"],
"POST",
APIFunction[{"what" -> "Animal",
"where" -> "Location" :> $GeoLocation},
(PutAppend[{#where, #what}, "locations.m"];
"Thank you for your submission.") &],
"GET",
APIFunction[{"zoom" -> Restricted["Integer", {1, 18}] -> 14},
GeoGraphics[
Map[GeoMarker[First[#], Tooltip[Last[#]["Image"], Last[#]],
"Scale" -> Offset[80]] &,
Quiet[Check[ReadList["locations.m"], {}]]],
GeoZoomLevel -> #zoom, ImageSize -> 700] &, "PNG"],
_, HTTPErrorResponse[400]
]
]];
Simulez le rapport de quelques vues d'animaux.
In[2]:=

GenerateHTTPResponse[
animallocator, <|"Method" -> "POST",
"Query" -> {"what" -> "coyote", "where" -> "40.083449,-88.200732"}|>];
GenerateHTTPResponse[
animallocator, <|"Method" -> "POST",
"Query" -> {"what" -> "buffalo",
"where" -> "40.140722, -88.195257"}|>];
GenerateHTTPResponse[
animallocator, <|"Method" -> "POST",
"Query" -> {"what" -> "beaver", "where" -> "40.063029,-87.9870"}|>];
GenerateHTTPResponse[
animallocator, <|"Method" -> "POST",
"Query" -> {"what" -> "reindeer", "where" -> "40.29778, -88.2023"}|>];
GenerateHTTPResponse[animallocator, <|"Method" -> "POST",
"Query" -> {"what" -> "gopher", "where" -> "40.116420,-88.243383"}|>]
Out[2]=

Simulez l'obtention des résultats.
In[3]:=

ImportString[GenerateHTTPResponse[animallocator]["Body"], "PNG"]
Out[3]=
