/// <summary> /// Convert JObject(JSON object) to BasicUserLocationInfo. /// </summary> /// <param name="jsonObject">The jsonObject<see cref="JObject"/></param> /// <returns>The <see cref="BasicUserLocationInfo"/>Basic information about the user's location.</returns> public static BasicUserLocationInfo JObjectToBasicUserLocationInfo(JObject jsonObject) { BasicUserLocationInfo result = null; if (jsonObject != null) { result = new BasicUserLocationInfo { Ip = (string)jsonObject["ip"], City = (string)jsonObject["city"], Region = (string)jsonObject["region"], RegionCode = (string)jsonObject["region_code"], Country = (string)jsonObject["country"], CountryName = (string)jsonObject["country_name"], ContinentCode = (string)jsonObject["continent_code"], InEU = (bool)jsonObject["in_eu"], Location = new GeoPosition((double)jsonObject["latitude"], (double)jsonObject["longitude"]), Timezone = (string)jsonObject["timezone"], UtcOffset = (string)jsonObject["utc_offset"], CountryCallingCode = (string)jsonObject["country_calling_code"], Currency = (string)jsonObject["currency"], Languages = StringLanguagesToList(jsonObject), Asn = (string)jsonObject["asn"], Org = (string)jsonObject["org"] }; } return(result); }
/// <summary> /// Get information about the user's location including URL of the country flag image. /// </summary> /// <returns>The <see cref="Task{UserLocationInfo}"/></returns> public static async Task <UserLocationInfo> GetLocationInfoAsync() { try { BasicUserLocationInfo ipApiResult = await GetBasicLocatioInfoAsync(); var client = new RestClient(); var request = new RestRequest(APIResources.RESTCOUNTRIES_API + ipApiResult.CountryName, Method.GET, DataFormat.Json); IRestResponse response = await client.ExecuteGetTaskAsync(request); JArray jsonArray = JArray.Parse(response.Content.ToString()); var flagUrl = (string)jsonArray[0]["flag"]; return(new UserLocationInfo(ipApiResult, flagUrl)); } catch (Exception) { return(null); } }