public void ProcessRequest(HttpContext context) { try { Location location = new Location() { Point = GeoRssPoint.Parse(context.Request.Form["lat"], context.Request.Form["lng"]) }; if (FireEagle.UpdateLocation(context, location, new Uri(context.Request.Url, context.Request.ApplicationPath))) { AjaxHelper.SendOk(context); } else AjaxHelper.SendError(context, "Unable to update location"); } catch (AuthorizationRequiredException authex) { AjaxHelper.SendAuthorizationRequired(context, authex); } catch (ThreadAbortException) { throw; } catch (Exception ex) { AjaxHelper.SendError(context, "Unexpected error: " + ex.Message); } }
public static void SendLocation(HttpContext context, Location location) { if (context == null) throw new ArgumentNullException("context"); context.Response.Write(AjaxHelper.GetLocationJsonObject(location)); context.Response.End(); }
public static string GetLocationJsonObject(Location location) { if (location == null) throw new ArgumentNullException("location"); var response = new StringBuilder(); response.Append("{ ").AppendLine(); response.Append(@" ""location"":").AppendLine(); response.Append(@" {").AppendLine(); if (location.Point != null) { response.Append(@" ""point"":").AppendLine(); response.Append(@" {").AppendLine(); response.Append(@" ""latitude"": ").Append(location.Point.Latitude).Append(",").AppendLine(); response.Append(@" ""longitude"": ").Append(location.Point.Longitude).AppendLine(); response.Append(@" },").AppendLine(); } if (location.BoundingBox != null) { response.Append(@" ""boundingbox"":").AppendLine(); response.Append(@" {").AppendLine(); response.Append(@" ""southwest"": ").AppendLine(); response.Append(@" {").AppendLine(); response.Append(@" ""latitude"": ").Append(location.BoundingBox.Southwest.Latitude).Append(",").AppendLine(); response.Append(@" ""longitude"": ").Append(location.BoundingBox.Southwest.Longitude).AppendLine(); response.Append(@" },").AppendLine(); response.Append(@" ""northeast"": ").AppendLine(); response.Append(@" {").AppendLine(); response.Append(@" ""latitude"": ").Append(location.BoundingBox.Northeast.Latitude).Append(",").AppendLine(); response.Append(@" ""longitude"": ").Append(location.BoundingBox.Northeast.Longitude).AppendLine(); response.Append(@" },").AppendLine(); response.Append(@" },").AppendLine(); } response.Append(@" ""name"": """).Append(JsonEscape(location.Name)).Append(@"""").AppendLine(); response.Append(@" }").AppendLine(); response.Append(@"}").AppendLine(); return response.ToString(); }
public static bool UpdateLocation(HttpContext context, Location location, Uri callback) { if (context == null) throw new ArgumentNullException("context"); if (location == null) throw new ArgumentNullException("location"); if (callback == null) throw new ArgumentNullException("callback"); // Update the user's location var request = OAuthRequest.Create( new EndPoint("https://fireeagle.yahooapis.com/api/0.1/update", "POST"), FireEagle.OAuthService, callback, HttpContext.Current.Session.SessionID); request.VerificationHandler = AspNetOAuthRequest.HandleVerification; // Send the location latitude and longitude with the request OAuthResponse response = request.GetResource( new NameValueCollection() { { "lat", location.Point.Latitude.ToString(CultureInfo.InvariantCulture) }, { "lon", location.Point.Longitude.ToString(CultureInfo.InvariantCulture) } }); if (!response.HasProtectedResource) throw new AuthorizationRequiredException() { AuthorizationUri = FireEagle.OAuthService.BuildAuthorizationUrl(response.Token) }; // Store the access token context.Session["access_token"] = response.Token; // Load the response XML XmlDocument responseXml = new XmlDocument(); responseXml.Load(response.ProtectedResource.GetResponseStream()); // Check the response status return responseXml.SelectSingleNode("rsp/@stat").Value == "ok"; }