Skip to content

Commit

Permalink
В методе получения профилей добавил безопасное получение code
Browse files Browse the repository at this point in the history
  • Loading branch information
Primus-max committed Oct 4, 2023
1 parent b79e0ec commit 2c436f1
Showing 1 changed file with 68 additions and 40 deletions.
108 changes: 68 additions & 40 deletions Manager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,58 +160,86 @@ public class ProfileManager
/// Получает список профилей.
/// </summary>
/// <returns>Список профилей.</returns>
public async Task<List<Profile>> GetProfiles()
public static async Task<List<Profile>> GetProfiles()
{
string apiUrl = "http://local.adspower.com:50325/api/v1/user/list?page_size=100";
var httpClient = new HttpClient();
try
{
string apiUrl = "http://local.adspower.com:50325/api/v1/user/list?page_size=100";
using var httpClient = new HttpClient();

var response = await httpClient.GetAsync(apiUrl);
var response = await httpClient.GetAsync(apiUrl);

if (!response.IsSuccessStatusCode)
{
Console.WriteLine($"Failed to get profiles. Status code: {response.StatusCode}");
return null;
}
if (!response.IsSuccessStatusCode)
{
Console.WriteLine($"Failed to get profiles. Status code: {response.StatusCode}");
return new List<Profile>();
}

var responseString = await response.Content.ReadAsStringAsync();
var responseString = await response.Content.ReadAsStringAsync();

JObject responseDataJson = JObject.Parse(responseString);
JObject responseDataJson = JObject.Parse(responseString);

int code = (int)responseDataJson["code"];
if (code != 0)
{
string errorMsg = (string)responseDataJson["msg"];
Console.WriteLine($"Failed to get profiles: {errorMsg}");
return null;
}
int code;
if (!int.TryParse(responseDataJson["code"]?.ToString(), out code))
{
Console.WriteLine("Invalid or missing 'code' in the response.");
return new List<Profile>();
}

var profilesJsonArray = (JArray)responseDataJson["data"]["list"];
var profiles = new List<Profile>();
if (code != 0)
{
string errorMsg = responseDataJson["msg"]?.ToString() ?? "Unknown error";
Console.WriteLine($"Failed to get profiles: {errorMsg}");
return new List<Profile>();
}

foreach (JToken profileJson in profilesJsonArray)
{
Profile profile = new Profile
var dataToken = responseDataJson["data"];
if (dataToken == null)
{
SerialNumber = (string?)profileJson["serial_number"],
UserId = (string?)profileJson["user_id"],
Name = (string?)profileJson["name"],
GroupId = (string?)profileJson["group_id"],
GroupName = (string?)profileJson["group_name"],
DomainName = (string?)profileJson["domain_name"],
Username = (string?)profileJson["username"],
Remark = (string?)profileJson["remark"],
CreatedTime = DateTimeOffset.FromUnixTimeSeconds((long)profileJson["created_time"]).DateTime,
IP = (string?)profileJson["ip"],
IPCountry = (string?)profileJson["ip_country"],
Password = (string?)profileJson["password"],
LastOpenTime = DateTimeOffset.FromUnixTimeSeconds((long)profileJson["last_open_time"]).DateTime
};
Console.WriteLine("Invalid or missing 'data' in the response.");
return new List<Profile>();
}

profiles.Add(profile);
}
var profilesJsonArray = dataToken["list"];
if (profilesJsonArray == null || !profilesJsonArray.Any())
{
Console.WriteLine("Invalid or missing 'list' in the 'data' section of the response.");
return new List<Profile>();
}

var profiles = new List<Profile>();

return profiles;
foreach (JToken profileJson in profilesJsonArray)
{
Profile profile = new Profile
{
SerialNumber = (string?)profileJson["serial_number"],
UserId = (string?)profileJson["user_id"],
Name = (string?)profileJson["name"],
GroupId = (string?)profileJson["group_id"],
GroupName = (string?)profileJson["group_name"],
DomainName = (string?)profileJson["domain_name"],
Username = (string?)profileJson["username"],
Remark = (string?)profileJson["remark"],
CreatedTime = DateTimeOffset.FromUnixTimeSeconds((long?)profileJson["created_time"] ?? 0).DateTime,
IP = (string?)profileJson["ip"],
IPCountry = (string?)profileJson["ip_country"],
Password = (string?)profileJson["password"],
LastOpenTime = DateTimeOffset.FromUnixTimeSeconds((long?)profileJson["last_open_time"] ?? 0).DateTime
};

profiles.Add(profile);
}

return profiles;
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
return new List<Profile>();
}
}

}

/// <summary>
Expand Down

0 comments on commit 2c436f1

Please sign in to comment.