Wolfram Language

Interfaces Cloud et Web

Créez une application de repérage d'animaux

Une API RESTful utilise la même URL mais répond différemment en fonction de la méthode HTTP. GenerateHTTPResponse fonctionne de manière récursive, donc un calcul Delayed peut renvoyer différentes instances d'APIFunction pour gérer différentes méthodes HTTP afin de mettre en œuvre une application web de signalement de lieux.

Créez une application simple qui collecte les lieux où différents types d'animaux ont été repérés.

In[1]:=
Click for copyable input
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 d'observations d'animaux.

In[2]:=
Click for copyable input
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]:=
Click for copyable input
ImportString[GenerateHTTPResponse[animallocator]["Body"], "PNG"]
Out[3]=

Exemples connexes

de en es ja ko pt-br ru zh