■ IAsyncEnumerable<T> 인터페이스를 사용해 넥서스 저장소(Nexus Repository)에서 자산(Asset) 정보를 구하는 방법을 보여준다.
▶ Checksum.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
using Newtonsoft.Json; namespace TestProject { /// <summary> /// 체크썸 /// </summary> public class Checksum { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region MD5 - MD5 /// <summary> /// MD5 /// </summary> [JsonProperty("md5")] public string MD5 { get; set; } #endregion #region SHA1 - SHA1 /// <summary> /// SHA1 /// </summary> [JsonProperty("sha1")] public string SHA1 { get; set; } #endregion #region SHA256 - SHA256 /// <summary> /// SHA256 /// </summary> [JsonProperty("sha256")] public string SHA256 { get; set; } #endregion #region SHA512 - SHA512 /// <summary> /// SHA512 /// </summary> [JsonProperty("sha512")] public string SHA512 { get; set; } #endregion } } |
▶ Item.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
using Newtonsoft.Json; namespace TestProject { /// <summary> /// 항목 /// </summary> public class Item { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region ID - ID /// <summary> /// ID /// </summary> [JsonProperty("id")] public string ID { get; set; } #endregion #region 저장소 - Repository /// <summary> /// 저장소 /// </summary> [JsonProperty("repository")] public string Repository { get; set; } #endregion #region 경로 - Path /// <summary> /// 경로 /// </summary> [JsonProperty("path")] public string Path { get; set; } #endregion #region 포맷 - Format /// <summary> /// 포맷 /// </summary> [JsonProperty("format")] public string Format { get; set; } #endregion #region 다운로드 URL - DownloadURL /// <summary> /// 다운로드 URL /// </summary> [JsonProperty("downloadUrl")] public string DownloadURL { get; set; } #endregion #region 체크썸 - Checksum /// <summary> /// 체크썸 /// </summary> [JsonProperty("checksum")] public Checksum Checksum { get; set; } #endregion } } |
▶ AssetData.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
using System.Collections.Generic; using Newtonsoft.Json; namespace TestProject { /// <summary> /// 자산 데이터 /// </summary> public class AssetData { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 항목 리스트 - ItemList /// <summary> /// 항목 리스트 /// </summary> [JsonProperty("items")] public IList<Item> ItemList { get; set; } #endregion #region 연속 토큰 - ContinuationToken /// <summary> /// 연속 토큰 /// </summary> [JsonProperty("continuationToken")] public string ContinuationToken { get; set; } #endregion } } |
▶ NexusHelper.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
using System.Collections.Generic; using System.Net; using System.Net.Http; using Newtonsoft.Json; namespace TestProject { /// <summary> /// 넥서스 헬퍼 /// </summary> public static class NexusHelper { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 항목 리스트 구하기 URL 포맷 1 /// </summary> private const string GET_ITEM_LIST_URL_FORMAT1 = "http://nexus.arca.net/service/rest/v1/assets?repository={0}"; /// <summary> /// 항목 리스트 구하기 URL 포맷 2 /// </summary> private const string GET_ITEM_LIST_URL_FORMAT2 = "http://nexus.arca.net/service/rest/v1/assets?repository={0}&continuationToken={1}"; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 사용자 ID - UserID /// <summary> /// 사용자 ID /// </summary> public static string UserID { get; set; } #endregion #region 패스워드 - Password /// <summary> /// 패스워드 /// </summary> public static string Password { get; set; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 항목 리스트 구하기 (비동기) - GetItemListAsync(repositoryName) /// <summary> /// 항목 리스트 구하기 (비동기) /// </summary> /// <param name="repositoryName">저장소명</param> /// <returns>항목 비동기 열거 가능형</returns> public async static IAsyncEnumerable<Item> GetItemListAsync(string repositoryName) { NetworkCredential credential = new NetworkCredential(UserID, Password); using(HttpClientHandler handler = new HttpClientHandler { Credentials = credential }) { using(HttpClient client = new HttpClient(handler)) { List<Item> list = new List<Item>(); string url = string.Format(GET_ITEM_LIST_URL_FORMAT1, repositoryName); while(true) { string json = await client.GetStringAsync(url); AssetData data = JsonConvert.DeserializeObject<AssetData>(json); foreach(Item item in data.ItemList) { yield return item; } if(string.IsNullOrWhiteSpace(data.ContinuationToken)) { break; } else { url = string.Format(GET_ITEM_LIST_URL_FORMAT2, repositoryName, data.ContinuationToken); } } } } } #endregion } } |
▶ Program.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
using System; using System.Threading.Tasks; namespace TestProject { /// <summary> /// 프로그램 /// </summary> class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> /// <returns>태스크</returns> private async static Task Main() { NexusHelper.UserID = "admin"; NexusHelper.Password = "1234"; await foreach(Item item in NexusHelper.GetItemListAsync("arca-update")) { Console.WriteLine(item.DownloadURL); } } #endregion } } |