PageRenderTime 22ms CodeModel.GetById 16ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/WifiChannelSpread/WiFiChannelSpread/GoogleApi/GoogleApi.cs

#
C# | 90 lines | 76 code | 14 blank | 0 comment | 4 complexity | c1e52cd2f1bdc23508369c43f5251afe MD5 | raw file
 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using System.Text;
 5using System.Net;
 6using System.IO;
 7using System.Collections.ObjectModel;
 8
 9namespace WiFiChannelSpread.GoogleApi
10{
11    public static class GeolocationApi
12    {
13        private const int HttpRequestTimeoutMilliseconds = 5000;
14        private const string GeolocationApiUrl = "http://www.google.com/loc/json";
15        private const string ApplicationHost = "WiFiChannelSpread.CodePlex.com";
16        private const string GeolocationApiVersion = "1.1.0";
17
18        public static Location GetGeolocation(Collection<WiFiTower> listOfWiFiTowers, bool includeAddress)
19        {
20            GeolocationRequest request = CreateGeolocationRequest(includeAddress);
21            request.wifi_towers = listOfWiFiTowers;
22
23            string jsonResponse = PostHttpJson(GeolocationApiUrl, Newtonsoft.Json.JsonConvert.SerializeObject(request));
24            GeolocationResponse response = Newtonsoft.Json.JsonConvert.DeserializeObject<GeolocationResponse>(jsonResponse);
25            if (response == null)
26            {
27                throw new InvalidOperationException("No response from Google.");
28            }
29
30            return response.location;
31        }
32
33        public static Location GetGeolocationOfWiFiTower(string macAddress, string ssid, bool includeAddress)
34        {
35            GeolocationRequest request = CreateGeolocationRequest(includeAddress);
36            request.wifi_towers.Add(new WiFiTower { mac_address = macAddress, ssid = ssid });
37
38            string jsonResponse = PostHttpJson(GeolocationApiUrl, Newtonsoft.Json.JsonConvert.SerializeObject(request));
39            GeolocationResponse response = Newtonsoft.Json.JsonConvert.DeserializeObject<GeolocationResponse>(jsonResponse);
40            if (response == null)
41            {
42                throw new InvalidOperationException("No response from Google.");
43            }
44
45            return response.location;
46        }
47
48        private static GeolocationRequest CreateGeolocationRequest(bool includeAddress)
49        {
50            return new GeolocationRequest
51                {
52                    host = ApplicationHost,
53                    version = GeolocationApiVersion,
54                    request_address = includeAddress,
55                    wifi_towers = new Collection<WiFiTower>()
56                };
57        }
58
59        private static string PostHttpJson(string url, string data)
60        {
61            byte[] postData = Encoding.ASCII.GetBytes(data);
62
63            HttpWebRequest request = CreateWebRequest(url);
64            request.Method = "POST";
65            request.ContentType = "application/json; charset=utf-8";
66            request.Accept = "application/json, text/javascript, */*";
67            request.ContentLength = postData.Length;
68
69            using (Stream requestStream = request.GetRequestStream())
70            {
71                requestStream.Write(postData, 0, postData.Length);
72            }
73
74            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
75            using (Stream responseStream = response.GetResponseStream())
76            using (StreamReader reader = new StreamReader(responseStream))
77            {
78                return reader.ReadToEnd();
79            }
80        }
81
82        private static HttpWebRequest CreateWebRequest(string url)
83        {
84            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
85            request.Timeout = HttpRequestTimeoutMilliseconds;
86            return request;
87        }
88
89    }
90}