Пример #1
0
            public void FailsGracefullyWhenNothingFound()
            {
                var args = new ReverseMilepostArgs(15, 15, new ReverseMilepostOptions());

                var client = new HttpClient();
                var url = "http://localhost:6080/arcgis/rest/services/Soe/SearchApi/MapServer/exts/Search/ReverseMilepost?{0}&f=json"
                    .With(args.ToQueryString());

                var response = client.GetAsync(url);
                var result = response.Result.Content.ReadAsStringAsync().Result;

                Assert.That(result, Is.Not.Null);
                Assert.That(result, Is.EqualTo("{\"TopResult\":null,\"EqualCandidates\":[]}"));

            }
Пример #2
0
            public void SkipsRampsAndFindsHighway()
            {
                var args = new ReverseMilepostArgs(423834.42, 4508729.44, new ReverseMilepostOptions());

                var client = new HttpClient();
                var url = "http://localhost:6080/arcgis/rest/services/Soe/SearchApi/MapServer/exts/Search/ReverseMilepost?{0}&f=json"
                    .With(args.ToQueryString());

                var response = client.GetAsync(url);
                var result = response.Result.Content.ReadAsStringAsync().Result;

                Assert.That(result, Is.Not.Null);
                Assert.That(result, Is.StringStarting(
                    "{\"TopResult\":{\"Milepost\":17.426200270235142,\"Route\":\"0201P\"").IgnoreCase);
            }
Пример #3
0
            public void FindsMilepostOnFederalInterstate()
            {
                var args = new ReverseMilepostArgs(311770, 4510850, new ReverseMilepostOptions());

                var client = new HttpClient();
                var url = "http://localhost:6080/arcgis/rest/services/Soe/SearchApi/MapServer/exts/Search/ReverseMilepost?{0}&f=json"
                    .With(args.ToQueryString());

                var response = client.GetAsync(url);
                var result = response.Result.Content.ReadAsStringAsync().Result;

                Assert.That(result, Is.Not.Null);
                Assert.That(result,
                            Is.StringStarting("{\"TopResult\":{\"Milepost\":42.848619712799547,\"Route\":\"0080N\"").
                               IgnoreCase);
            }
Пример #4
0
            public void ReprojectsPoints()
            {
                var args = new ReverseMilepostArgs(-111.90190785190332, 40.72597381846899,
                                                   new ReverseMilepostOptions {WkId = 4326});

                var client = new HttpClient();
                var url = "http://localhost:6080/arcgis/rest/services/Soe/SearchApi/MapServer/exts/Search/ReverseMilepost?{0}&f=json"
                    .With(args.ToQueryString());

                var response = client.GetAsync(url);
                var result = response.Result.Content.ReadAsStringAsync().Result;

                Assert.That(result, Is.Not.Null);
                Assert.That(result, Is.StringStarting(
                    "{\"TopResult\":{\"Milepost\":17.42619992647089,\"Route\":\"0201P\"").IgnoreCase);
            }
        public async Task<HttpResponseMessage> ReverseMilePost(double? x, double? y,
                                                               [FromUri] ReverseMilepostOptions options)
        {
            #region validation

            var errors = "";
            if (!x.HasValue)
            {
                errors = "X is empty.";
            }

            if (!y.HasValue)
            {
                errors += "Y is emtpy";
            }

            if (errors.Length > 0)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest,
                                              new ResultContainer<ReverseGeocodeResult>
                                                  {
                                                      Status = (int) HttpStatusCode.BadRequest,
                                                      Message = errors
                                                  });
            }

            #endregion

            var queryArgs = new ReverseMilepostArgs(x.Value, y.Value, options);

            var client = new HttpClient();

            var requestUri = ConfigurationManager.AppSettings["reverse_milepost_url"]
                .With(queryArgs.ToQueryString());

            HttpResponseMessage request;

            try
            {
                request = await client.GetAsync(requestUri);
            }
            catch (AggregateException)
            {
                return Request.CreateResponse(HttpStatusCode.InternalServerError,
                                              new ResultContainer
                                                  {
                                                      Status = (int) HttpStatusCode.InternalServerError,
                                                      Message = "I'm sorry, it seems as though the request had issues."
                                                  })
                               .AddTypeHeader(typeof(ResultContainer<ReverseMilepostResult>));
            }

            try
            {
                request.EnsureSuccessStatusCode();
            }
            catch (Exception)
            {
                return Request.CreateResponse(HttpStatusCode.InternalServerError,
                                              new ResultContainer
                                                  {
                                                      Status = (int) HttpStatusCode.InternalServerError,
                                                      Message = "I'm sorry, we were unable to communicate with the SOE."
                                                  })
                             .AddTypeHeader(typeof(ResultContainer<ReverseMilepostResult>));
            }

            var response = await request.Content.ReadAsAsync<TopAndEqualMilepostCandidates>(new[]
                {
                    new TextPlainResponseFormatter()
                });


            if (response.TopResult == null)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound, new ResultContainer
                    {
                        Status = (int) HttpStatusCode.NotFound,
                        Message = "No milepost was found within your buffer radius."
                    })
                              .AddTypeHeader(typeof(ResultContainer<ReverseMilepostResult>))
                              .AddCache();
            }

            var transformed = new ReverseMilepostResult
                {
                    Route = response.TopResult.Route,
                    OffsetMeters = response.TopResult.Distance,
                    Milepost = response.TopResult.Milepost,
                    Increasing = response.TopResult.Increasing
                };

            return Request.CreateResponse(HttpStatusCode.OK, new ResultContainer<ReverseMilepostResult>
                {
                    Status = (int) HttpStatusCode.OK,
                    Result = transformed
                })
                          .AddTypeHeader(typeof(ResultContainer<ReverseMilepostResult>))
                          .AddCache();
        }