// Checks consistency of returned data
 private static bool checkData(JSONInfoData _data)
 {
     return(!String.IsNullOrEmpty(_data.Wm_version) &&
            !String.IsNullOrEmpty(_data.Wurfl_api_version) &&
            !String.IsNullOrEmpty(_data.Wurfl_info) &&
            (_data.Static_caps != null || _data.Virtual_caps != null));
 }
        /// <summary>
        /// Returns information about the running WM server and API.
        /// Throws WmClientException in case any client related problem occurs.
        /// </summary>
        /// <returns></returns>
        public JSONInfoData GetInfo()
        {
            JSONInfoData info = null;

            HttpResponseMessage response = null;

            try
            {
                response = c.GetAsync(createURL("/v2/getinfo/json")).Result;
                if (response != null && response.IsSuccessStatusCode && response.Content != null)
                {
                    info = response.Content.ReadAsAsync <JSONInfoData>().Result;
                    // check if caches must be cleaned
                    if (!checkData(info))
                    {
                        throw new WmException("Server returned empty data or a wrong json format");
                    }

                    ClearCachesIfNeeded(info.Ltime);
                }
            }
            catch (Exception e)
            {
                throw new WmException("Error getting informations for WM server: " + e.Message, e);
            }
            finally
            {
                if (response != null)
                {
                    response.Dispose();
                }
            }

            return(info);
        }
        /// <summary>
        /// Creates a WURFL microservice client and tests if server denoted by the given host and port is available for connection.
        /// Throws WmClientException in case server is down or not available for connection
        /// </summary>
        /// <param name="host">WM server host</param>
        /// <param name="port">WM server port</param>
        /// <returns>A instance of the wurfl microservice client</returns>
        public static WmClient Create(string scheme, string host, string port, string baseURI)
        {
            WmClient client = new WmClient();

            client.Host = host;
            client.Port = port;
            if (scheme != null && scheme.Length > 0)
            {
                client.scheme = scheme;
            }
            else
            {
                client.scheme = "http";
            }
            client.baseURI = baseURI;

            // retieves internal http client and performs an HEAD request

            JSONInfoData data = null;

            try
            {
                data = client.GetInfo();

                client.ImportantHeaders = data.Important_headers;
                client.StaticCaps       = data.Static_caps;
                client.VirtualCaps      = data.Virtual_caps;
                Array.Sort(client.StaticCaps);
                Array.Sort(client.VirtualCaps);
            }
            catch (Exception e)
            {
                if (client != null)
                {
                    client.DestroyConnection();
                }
                throw new WmException("Error creating WM CLIENT: " + e.Message, e);
            }
            return(client);
        }