Wolfram Language

Cloud & Web Interfaces

Control Web Response for Space Station Tracker

You can control the exact features of the HTTP response from a cloud object by returning an HTTPResponse. Error conditions can be handled with HTTPErrorResponse.

Define a function that finds the city closest to the current location of the International Space Station. The space station moves quickly, so the results change rapidly.

show complete Wolfram Language input
In[1]:=
Click for copyable input
issLocationDisplay[] := Module[{pos, posline, country, city, distance}, {pos, posline} = SatelliteData[ Entity["Satellite", "25544"], {"Position", "PositionLine"}]; country = First[GeoNearest["Country", pos]]; distance = Round[GeoDistance[pos, country], 0.1]; Panel@Column[{Row[{"ISS Position: ", Style[If[distance <= Quantity[0., "Miles"], "over " <> First[GeoNearest["City", pos]]["Name"] <> ", " <> country["Name"], ToString[ Row[{"over the open sea ", distance, " from ", country["Name"]}]]], Darker[Blue]]}, BaseStyle -> Directive["FontFamily" -> "SansSerif", RGBColor["#497CA4"], Larger, Bold]], GeoGraphics[{Gray, Thickness[.005], Arrowheads[{{0.05, 0.4}, {0.05, 0.13}}], Arrow @@ posline, Red, PointSize[.01], Point[pos], Opacity[.1], Black, GeoVisibleRegion[pos]}, GeoCenter -> pos, GeoRange -> "World", ImageSize -> 600]}]]
In[2]:=
Click for copyable input
issLocationDisplay[]
Out[2]=

Deploy a cloud object that computes the result each time, using HTTPResponse to indicate the content type and to specify a header that tells the browser not to cache the result.

In[3]:=
Click for copyable input
obj = CloudDeploy[ Delayed[HTTPResponse[ ExportString[issLocationDisplay[], "PNG"], <| "ContentType" -> "image/png", "Headers" -> {"Cache-Control" -> "no-cache"}|>]], CloudObject["iss-location"], Permissions -> "Public"]
Out[3]=

Ban certain users from using the cloud object, returning an HTTPErrorResponse.

In[4]:=
Click for copyable input
obj = CloudDeploy[Delayed[ If[MemberQ[{"badapple@example.com", "blackhat@example.com", "spammer@example.com"}, $WolframID], HTTPErrorResponse[403], HTTPResponse[ issLocationDisplay[], <| "Headers" -> {"Cache-Control" -> "no-cache"}|>] ]], CloudObject["iss-location"], Permissions -> "Public"]
Out[4]=

Related Examples

de es fr ja ko pt-br ru zh