|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Net;
- using Newtonsoft.Json;
-
- namespace NScript.Gitea
- {
- public class ReleaseUtility
- {
- public static List<Release> GetReleases(string owner, string repo,
- string apiUrl = "https://git.exmods.org/api/v1")
- {
- string fullUrl = $"{apiUrl}/repos/{owner}/{repo}/releases";
- WebRequest req = WebRequest.Create(fullUrl);
- req.Method = "GET";
- HttpWebResponse resp = (HttpWebResponse) req.GetResponse();
- StreamReader body = new StreamReader(resp.GetResponseStream());
- string respBody = body.ReadToEnd();
- resp.Close();
- return JsonConvert.DeserializeObject<List<Release>>(respBody);
- }
-
- public static byte[] DownloadAsset(Asset asset)
- {
- return DownloadAsset(asset.BrowserDownloadUrl);
- }
-
- public static byte[] DownloadAsset(string url)
- {
- WebRequest req = WebRequest.Create(url);
- req.Method = "GET";
- HttpWebResponse resp = (HttpWebResponse) req.GetResponse();
- BinaryReader body = new BinaryReader(resp.GetResponseStream());
- byte[] respBody = body.ReadBytes(int.MaxValue);
- resp.Close();
- return respBody;
- }
-
- public static Release? FindRelease(List<Release> releases, bool prerelease = false, bool draft = false)
- {
- foreach (Release release in releases)
- {
- if (release.PreRelease == prerelease && release.Draft == draft) return release;
- }
-
- return null;
- }
-
- public static Asset? FindAsset(Release release, string name)
- {
- return FindAsset(release.Assets, name);
- }
-
- public static Asset? FindAsset(List<Asset> assets, string name)
- {
- foreach (var asset in assets)
- {
- if (string.Equals(asset.Name, name, StringComparison.InvariantCulture)) return asset;
- }
-
- return null;
- }
- }
- }
|