-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch to HttpClient and run the web request on the thread pool
- Loading branch information
1 parent
cc6b417
commit 1bb858e
Showing
6 changed files
with
76 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 8 additions & 22 deletions
30
Feli.RocketMod.JoinLeaveMessages/Helpers/IpGeolocationHelper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,33 @@ | ||
using Newtonsoft.Json.Linq; | ||
using Rocket.Core.Logging; | ||
using System.IO; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
|
||
namespace Feli.RocketMod.JoinLeaveMessages.Helpers | ||
{ | ||
public class IpGeolocationHelper | ||
{ | ||
public static string GetCountryFromIp(string address) | ||
public static async Task<string> GetCountryFromIp(string address) | ||
{ | ||
var request = CreateRequest(address); | ||
using var client = new HttpClient(); | ||
var response = await client.GetAsync($"http://ip-api.com/json/{address}?fields=status,message,country"); | ||
|
||
var response = (HttpWebResponse)request.GetResponse(); | ||
|
||
if (response.StatusCode != HttpStatusCode.OK) | ||
if (!response.IsSuccessStatusCode) | ||
{ | ||
Logger.LogError($"HTTP Error: {(int)response.StatusCode}, {response.StatusCode}"); | ||
return string.Empty; | ||
} | ||
|
||
var content = ReadContent(response.GetResponseStream()); | ||
var content = await response.Content.ReadAsStringAsync(); | ||
|
||
var @object = JObject.Parse(content); | ||
|
||
if(@object["status"].ToString() == "fail") | ||
if (@object["status"].ToString() == "fail") | ||
{ | ||
Logger.LogError($"Failed to get the ip location. Error: {@object["message"]}"); | ||
return string.Empty; | ||
} | ||
|
||
return @object["country"].ToString(); | ||
} | ||
|
||
internal static WebRequest CreateRequest(string address) | ||
{ | ||
return WebRequest.Create($"http://ip-api.com/json/{address}?fields=status,message,country"); | ||
} | ||
|
||
internal static string ReadContent(Stream stream) | ||
{ | ||
using var reader = new StreamReader(stream); | ||
|
||
return reader.ReadToEnd(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters