Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· openaiHugging Face Suzhen/CodeChat

напиши библиотеку для API osu v2 на C#

  1. User

    напиши библиотеку для API osu v2 на C#

  2. Assistant

    Определение типов и классов запросов:

    using System;
    using System.Net.Http;
    using System.Threading.Tasks;
    
    namespace OsuAPI
    {
        public class OsuApi
        {
            private const string baseUrl = "https://osu.ppy.sh/api/v2/";
            private readonly HttpClient httpClient;
    
            public OsuApi()
            {
                httpClient = new HttpClient();
            }
    
            public async Task<Beatmap> GetBeatmap(int beatmapId)
            {
                string url = $"{baseUrl}beatmaps/{beatmapId}";
                return await SendRequest<Beatmap>(url);
            }
    
            public async Task<User> GetUser(int userId)
            {
                string url = $"{baseUrl}users/{userId}";
                return await SendRequest<User>(url);
            }
    
            private async Task<T> SendRequest<T>(string url)
            {
                try
                {
                    var response = await httpClient.GetAsync(url);
                    response.EnsureSuccessStatusCode();
                    return await response.Content.ReadAsAsync<T>();
                }
                catch (Exception ex)
                {
                    throw new Exception("Error sending request to osu! API", ex);
                }
            }
        }
    
        public class Beatmap
        {
            public int Id { get; set; }
            public string Title { get; set; }
            public string Artist { get; set; }
            public int DifficultyRating { get; set; }
        }
    
        public class User
        {
            public int Id { get; set; }
            public string Username { get; set; }
            public int PlayCount { get; set; }
        }
    }
    

    Пример использования:

    using System;
    using System.Threading.Tasks;
    
    namespace OsuAPI
    {
        class Program
        {
            static async Task Main(string[] args)
            {
                OsuApi osuApi = new OsuApi();
                int userId = 123456;
                int beatmapId = 789012;
    
                try
                {
                    User user = await osuApi.GetUser(userId);
                    Beatmap beatmap = await osuApi.GetBeatmap(beatmapId);
    
                    Console.WriteLine($"User: {user.Username}");
                    Console.WriteLine($"Play Count: {user.PlayCount}");
    
                    Console.WriteLine($"Beatmap: {beatmap.Title}");
                    Console.WriteLine($"Artist: {beatmap.Artist}");
                    Console.WriteLine($"Difficulty Rating: {beatmap.DifficultyRating}");
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"Error: {ex.Message}");
                }
    
                Console.ReadLine();
            }
        }
    }
    

    Не забудьте добавить ссылку на System.Net.Http в проект с помощью NuGet.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.