You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 line
2.1KB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Net;
  5. using Newtonsoft.Json;
  6. namespace NScript.Gitea
  7. {
  8. public class ReleaseUtility
  9. {
  10. public static List<Release> GetReleases(string owner, string repo,
  11. string apiUrl = "https://git.exmods.org/api/v1")
  12. {
  13. string fullUrl = $"{apiUrl}/repos/{owner}/{repo}/releases";
  14. WebRequest req = WebRequest.Create(fullUrl);
  15. req.Method = "GET";
  16. HttpWebResponse resp = (HttpWebResponse) req.GetResponse();
  17. StreamReader body = new StreamReader(resp.GetResponseStream());
  18. string respBody = body.ReadToEnd();
  19. resp.Close();
  20. return JsonConvert.DeserializeObject<List<Release>>(respBody);
  21. }
  22. public static byte[] DownloadAsset(Asset asset)
  23. {
  24. return DownloadAsset(asset.BrowserDownloadUrl);
  25. }
  26. public static byte[] DownloadAsset(string url)
  27. {
  28. WebRequest req = WebRequest.Create(url);
  29. req.Method = "GET";
  30. HttpWebResponse resp = (HttpWebResponse) req.GetResponse();
  31. BinaryReader body = new BinaryReader(resp.GetResponseStream());
  32. byte[] respBody = body.ReadBytes(int.MaxValue);
  33. resp.Close();
  34. return respBody;
  35. }
  36. public static Release? FindRelease(List<Release> releases, bool prerelease = false, bool draft = false)
  37. {
  38. foreach (Release release in releases)
  39. {
  40. if (release.PreRelease == prerelease && release.Draft == draft) return release;
  41. }
  42. return null;
  43. }
  44. public static Asset? FindAsset(Release release, string name)
  45. {
  46. return FindAsset(release.Assets, name);
  47. }
  48. public static Asset? FindAsset(List<Asset> assets, string name)
  49. {
  50. foreach (var asset in assets)
  51. {
  52. if (string.Equals(asset.Name, name, StringComparison.InvariantCulture)) return asset;
  53. }
  54. return null;
  55. }
  56. }
  57. }