-
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.
add : 필요 deserialize 모델 정의 refactoring : user data 가져오는 로직 분리
- Loading branch information
1 parent
db7dbb8
commit 303e598
Showing
10 changed files
with
221 additions
and
81 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
92 changes: 92 additions & 0 deletions
92
src/dotnetdev-badge/dotnetdev-badge.web/Core/ForumDataProvider.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 |
---|---|---|
@@ -0,0 +1,92 @@ | ||
using DotNetDevBadgeWeb.Interfaces; | ||
using DotNetDevBadgeWeb.Model; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
|
||
namespace DotNetDevBadgeWeb.Core | ||
{ | ||
internal class ForumDataProvider : IProvider | ||
{ | ||
private const string UNKOWN_IMG_PATH = "Assets/unknown.png"; | ||
|
||
private const string BASE_URL = "https://forum.dotnetdev.kr"; | ||
private const string BADGE_URL = "https://forum.dotnetdev.kr/user-badges/{0}.json?grouped=true"; | ||
private const string SUMMARY_URL = "https://forum.dotnetdev.kr/u/{0}/summary.json"; | ||
|
||
private readonly IHttpClientFactory _httpClientFactory; | ||
|
||
public ForumDataProvider(IHttpClientFactory httpClientFactory) | ||
{ | ||
_httpClientFactory = httpClientFactory; | ||
} | ||
|
||
private async Task<string> GetResponseStringAsync(Uri uri, CancellationToken token) | ||
{ | ||
using HttpClient client = _httpClientFactory.CreateClient(); | ||
|
||
using HttpResponseMessage response = await client.GetAsync(uri, token); | ||
|
||
return await response.Content.ReadAsStringAsync(token); | ||
} | ||
|
||
private async Task<byte[]> GetResponseBytesAsync(Uri uri, CancellationToken token) | ||
{ | ||
using HttpClient client = _httpClientFactory.CreateClient(); | ||
|
||
using HttpResponseMessage response = await client.GetAsync(uri, token); | ||
|
||
return await response.Content.ReadAsByteArrayAsync(token); | ||
} | ||
|
||
public async Task<(UserSummary summary, User user)> GetUserInfoAsync(string id, CancellationToken token) | ||
{ | ||
Uri summaryUri = new(string.Format(SUMMARY_URL, id)); | ||
string summaryData = await GetResponseStringAsync(summaryUri, token); | ||
JObject summaryJson = JObject.Parse(summaryData); | ||
|
||
UserSummary userSummary = JsonConvert.DeserializeObject<UserSummary>(summaryJson["user_summary"]?.ToString() ?? string.Empty) ?? new(); | ||
List<User>? users = JsonConvert.DeserializeObject<List<User>>(summaryJson["users"]?.ToString() ?? string.Empty); | ||
|
||
User user = users?.Where(user => user.Id != -1).FirstOrDefault() ?? new(); | ||
|
||
return (userSummary, user); | ||
} | ||
|
||
public async Task<(byte[] avatar, UserSummary summary, User user)> GetUserInfoWithAvatarAsync(string id, CancellationToken token) | ||
{ | ||
(UserSummary summary, User user) = await GetUserInfoAsync(id, token); | ||
|
||
if (string.IsNullOrEmpty(user.AvatarEndPoint)) | ||
return (await File.ReadAllBytesAsync(UNKOWN_IMG_PATH, token), summary, user); | ||
|
||
Uri avatarUri = new(string.Concat(BASE_URL, user.AvatarEndPoint)); | ||
return (await GetResponseBytesAsync(avatarUri, token), summary, user); | ||
} | ||
|
||
public async Task<(int gold, int silver, int bronze)> GetBadgeCountAsync(string id, CancellationToken token) | ||
{ | ||
Uri badgeUri = new(string.Format(BADGE_URL, id)); | ||
string badgeData = await GetResponseStringAsync(badgeUri, token); | ||
JObject badgeJson = JObject.Parse(badgeData); | ||
|
||
var badges = badgeJson["badges"]?.GroupBy(badge => badge["badge_type_id"]?.ToString() ?? string.Empty).Select(g => new | ||
{ | ||
Type = g.Key, | ||
Count = g.Count(), | ||
}).ToDictionary(kv => kv.Type, kv => kv.Count); | ||
|
||
int gold = default; | ||
int silver = default; | ||
int bronze = default; | ||
|
||
if (badges is not null) | ||
{ | ||
gold = badges.ContainsKey("1") ? badges["1"] : 0; | ||
silver = badges.ContainsKey("2") ? badges["2"] : 0; | ||
bronze = badges.ContainsKey("3") ? badges["3"] : 0; | ||
} | ||
|
||
return (gold, silver, bronze); | ||
} | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
src/dotnetdev-badge/dotnetdev-badge.web/Endpoints/Badge/BadgeEndpoints.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
6 changes: 3 additions & 3 deletions
6
...-badge/dotnetdev-badge.web/Core/IBadge.cs → .../dotnetdev-badge.web/Interfaces/IBadge.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
11 changes: 11 additions & 0 deletions
11
src/dotnetdev-badge/dotnetdev-badge.web/Interfaces/IProvider.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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using DotNetDevBadgeWeb.Model; | ||
|
||
namespace DotNetDevBadgeWeb.Interfaces | ||
{ | ||
public interface IProvider | ||
{ | ||
Task<(UserSummary summary, User user)> GetUserInfoAsync(string id, CancellationToken token); | ||
Task<(byte[] avatar, UserSummary summary, User user)> GetUserInfoWithAvatarAsync(string id, CancellationToken token); | ||
Task<(int gold, int silver, int bronze)> GetBadgeCountAsync(string id, CancellationToken token); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using DotNetDevBadgeWeb.Common; | ||
using Newtonsoft.Json; | ||
|
||
namespace DotNetDevBadgeWeb.Model | ||
{ | ||
public class User | ||
{ | ||
private const int AVATAR_SIZE = 128; | ||
|
||
|
||
[JsonProperty("id")] | ||
public int Id { get; set; } | ||
|
||
[JsonProperty("username")] | ||
public string Username { get; set; } | ||
|
||
[JsonProperty("name")] | ||
public string Name { get; set; } | ||
|
||
[JsonProperty("avatar_template")] | ||
public string AvatarTemplate { get; set; } | ||
|
||
[JsonProperty("flair_name")] | ||
public object FlairName { get; set; } | ||
|
||
[JsonProperty("trust_level")] | ||
public int TrustLevel { get; set; } | ||
|
||
[JsonProperty("admin")] | ||
public bool? Admin { get; set; } | ||
|
||
[JsonProperty("moderator")] | ||
public bool? Moderator { get; set; } | ||
|
||
public ELevel Level => TrustLevel switch | ||
{ | ||
3 => ELevel.Silver, | ||
4 => ELevel.Gold, | ||
_ => ELevel.Bronze, | ||
}; | ||
|
||
public string AvatarEndPoint => AvatarTemplate?.Replace("{size}", AVATAR_SIZE.ToString()) ?? string.Empty; | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
src/dotnetdev-badge/dotnetdev-badge.web/Model/UserSummary.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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using Newtonsoft.Json; | ||
|
||
namespace DotNetDevBadgeWeb.Model | ||
{ | ||
public class UserSummary | ||
{ | ||
[JsonProperty("likes_given")] | ||
public int LikesGiven { get; set; } | ||
|
||
[JsonProperty("likes_received")] | ||
public int LikesReceived { get; set; } | ||
|
||
[JsonProperty("topics_entered")] | ||
public int TopicsEntered { get; set; } | ||
|
||
[JsonProperty("posts_read_count")] | ||
public int PostsReadCount { get; set; } | ||
|
||
[JsonProperty("days_visited")] | ||
public int DaysVisited { get; set; } | ||
|
||
[JsonProperty("topic_count")] | ||
public int TopicCount { get; set; } | ||
|
||
[JsonProperty("post_count")] | ||
public int PostCount { get; set; } | ||
|
||
[JsonProperty("time_read")] | ||
public int TimeRead { get; set; } | ||
|
||
[JsonProperty("recent_time_read")] | ||
public int RecentTimeRead { get; set; } | ||
|
||
[JsonProperty("bookmark_count")] | ||
public int BookmarkCount { get; set; } | ||
|
||
[JsonProperty("can_see_summary_stats")] | ||
public bool CanSeeSummaryStats { get; set; } | ||
|
||
[JsonProperty("solved_count")] | ||
public int SolvedCount { get; set; } | ||
} | ||
} |
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