Wolfram 语言

云端和网络界面

创建动物目击地的应用程序

RESTful API 使用相同的 URL,但响应不同,具体取决于HTTP方法. GenerateHTTPResponse 以递归方式工作,所以一个 Delayed 计算会返回不同的 APIFunction 实例以处理不同的 HTTP 方法,从而实现一个位置报告的 Web 应用程序.

创建一个简单的应用程序,收集发现不同类型动物的位置.

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] ] ]];

模拟一些动物的目击报告.

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]=

模拟提取结果.

In[3]:=
Click for copyable input
ImportString[GenerateHTTPResponse[animallocator]["Body"], "PNG"]
Out[3]=

相关范例

de en es fr ja ko pt-br ru