동물 찾기 응용 프로그램 작성
RESTful API는 동일한 URL을 사용하지만, HTTP 메소드에 따라 응답을 달리합니다. GenerateHTTPResponse는 재귀적으로 작동하기 때문에 Delayed의 계산은 위치 보고서의 웹 응용 프로그램을 구현하는 다른 HTTP 메소드를 처리하기 위해 다른 APIFunction을 반환 할 수 있습니다.
다양한 종류의 동물이 발견된 위치를 수집하는 간단한 응용 프로그램을 작성합니다.
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]
]
]];
동물 발견 보고서를 시뮬레이션합니다.
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]=

결과 가져오기를 시뮬레이션합니다.
In[3]:=

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