~ruther/NosTale-Gfless

ref: fc51a68e949a505ac95fe9b5e7363245d2b539d8 NosTale-Gfless/NostaleAuth/Models/GameforgeRequest.cs -rw-r--r-- 3.4 KiB
fc51a68e — František Boháček Initial commit 4 years ago
                                                                                
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
104
105
106
107
108
109
110
111
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace NostaleAuth.Models
{
    public class GameforgeRequest
    {
        protected const string Url = "https://spark.gameforge.com/api/v1";
        protected static string UserAgent = "GameforgeClient/2.1.12";
        protected const string MediaType = "application/json";

        public static void SetVersion(string version)
        {
            if (!string.IsNullOrEmpty(version))
            {
                UserAgent = "GameforgeClient/" + version;
            }
        }   
    }
    
    public class GameforgeRequest<T> : GameforgeRequest
    {
        private static readonly HttpClient _httpClient = new HttpClient();

        public GameforgeRequest(HttpMethod method, string path, Guid? installationId, string bearerToken)
        {
            Method = method;
            Path = path;
            InstallationId = installationId;
            BearerToken = bearerToken;
        }

        public GameforgeRequest(HttpMethod method, string path, Guid? installationId)
            : this(method, path, installationId, null)
        {
        }
        
        public GameforgeRequest(HttpMethod method, string path)
            : this(method, path, null, null)
        {
        }

        public string Path { get; set; }

        public Guid? InstallationId { get; set; }
        public HttpMethod Method { get; set; }

        public string BearerToken { get; set; }

        public async Task<Dictionary<string, T>> Send()
        {
            using (HttpRequestMessage request = PrepareRequest())
            {
                return await GetResponse(request);
            }
        }

        protected async Task<Dictionary<string, T>> GetResponse(HttpRequestMessage request)
        {
            HttpResponseMessage response = await _httpClient.SendAsync(request);
            
            if (!response.IsSuccessStatusCode)
            {
                return null;
            }
            
            string content = await response.Content.ReadAsStringAsync();
            return JsonConvert.DeserializeObject<Dictionary<string, T>>(content);
        }

        protected HttpRequestMessage PrepareRequest()
        {
            var request = new HttpRequestMessage(Method, $"{Url}" + Path);
            if (InstallationId != null)
            {
                request.Headers.Add("TNT-Installation-Id", InstallationId.ToString());
            }

            request.Headers.Add("User-Agent", UserAgent);

            if (BearerToken != null)
            {
                request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", BearerToken);
            }

            return request;
        }
    }

    public class GameforgeRequest<T, TU> : GameforgeRequest<TU>
    {
        public GameforgeRequest(HttpMethod method, string path, Guid installationId, string bearerToken = null)
            : base(method, path, installationId, bearerToken)
        {
        }

        public async Task<Dictionary<string, TU>> Send(T body)
        {
            using (HttpRequestMessage request = PrepareRequest())
            {
                request.Content = new StringContent(JsonConvert.SerializeObject(body), Encoding.UTF8, MediaType);
                return await GetResponse(request);
            }
        }
    }
}
Do not follow this link