Skip to content

Commit 88c7b79

Browse files
committed
major cleanup, verbose API update, request processors
1 parent 0b95b31 commit 88c7b79

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+133
-284
lines changed

MusixmatchClientLib.Sample/Program.cs

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using MusixmatchClientLib.Auth;
1+
using MusixmatchClientLib.API.Processors;
2+
using MusixmatchClientLib.Auth;
23
using MusixmatchClientLib.Exploits;
34
using MusixmatchClientLib.Types;
45
using System;
@@ -19,30 +20,7 @@ static void Main(string[] args)
1920

2021
// Example usage of request processor functions
2122
// The one below is used as a default function to process requests and requires cloudflare cookie handling
22-
CookieContainer container = new CookieContainer();
23-
client.SetRequestProcessor((string url, string method, string data) =>
24-
{
25-
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
26-
request.Method = method;
27-
request.CookieContainer = container;
28-
if (method == "POST")
29-
{
30-
byte[] byteArray = Encoding.UTF8.GetBytes(data);
31-
request.ContentType = "application/x-www-form-urlencoded";
32-
request.ContentLength = byteArray.Length;
33-
using (Stream dataStream = request.GetRequestStream())
34-
dataStream.Write(byteArray, 0, byteArray.Length);
35-
}
36-
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
37-
container.Add(response.Cookies);
38-
using (Stream stream = response.GetResponseStream())
39-
{
40-
using (StreamReader reader = new StreamReader(stream))
41-
{
42-
return reader.ReadToEnd();
43-
}
44-
}
45-
});
23+
client.SetRequestProcessor(new DefaultRequestProcessor());
4624

4725
var list = new List<API.Model.Types.TranslationPost>();
4826
list.Add(new API.Model.Types.TranslationPost

MusixmatchClientLib/API/ApiRequestFactory.cs

Lines changed: 21 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
using MusixmatchClientLib.API.Contexts;
22
using MusixmatchClientLib.API.Model;
3+
using MusixmatchClientLib.API.Processors;
34
using Newtonsoft.Json.Linq;
45
using System;
56
using System.Collections.Generic;
67
using System.IO;
7-
using System.Linq;
88
using System.Net;
99
using System.Text;
10-
using System.Threading.Tasks;
1110

1211
namespace MusixmatchClientLib.API
1312
{
@@ -19,33 +18,9 @@ class ApiRequestFactory
1918
private static CookieContainer defaultCookieContainer = new CookieContainer();
2019

2120
/// <summary>
22-
/// Function to be used to process requests (useful when debugging your application or using proxy).
21+
/// Class to be used to process requests (useful when debugging your application or using proxy).
2322
/// </summary>
24-
public Func<string, string, string, string> RequestProcessor = new Func<string, string, string, string>((string _url, string _method, string _data) =>
25-
{
26-
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_url);
27-
request.Method = _method;
28-
request.CookieContainer = defaultCookieContainer;
29-
if (_method == "POST")
30-
{
31-
byte[] byteArray = Encoding.UTF8.GetBytes(_data);
32-
request.ContentType = "application/x-www-form-urlencoded";
33-
request.ContentLength = byteArray.Length;
34-
using (Stream dataStream = request.GetRequestStream())
35-
dataStream.Write(byteArray, 0, byteArray.Length);
36-
}
37-
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
38-
defaultCookieContainer.Add(response.Cookies);
39-
using (Stream stream = response.GetResponseStream())
40-
{
41-
using (StreamReader reader = new StreamReader(stream))
42-
{
43-
return reader.ReadToEnd();
44-
}
45-
}
46-
});
47-
48-
public string RequestFilter(string _url, string _method = "GET", string _data = "") => RequestProcessor(_url, _method, _data);
23+
public RequestProcessor RequestProcessor = new DefaultRequestProcessor();
4924

5025
private string GetArgumentString(Dictionary<string, string> arguments)
5126
{
@@ -125,7 +100,7 @@ public enum ApiMethod
125100
[ApiMethod.TrackSubtitlePost] = new CustomRequestParameters
126101
{
127102
EndpointResource = "track.subtitle.post",
128-
RequestMethod = "POST"
103+
RequestMethod = RequestMethod.POST
129104
},
130105
[ApiMethod.RequestJwtToken] = new CustomRequestParameters
131106
{
@@ -146,7 +121,7 @@ public enum ApiMethod
146121
[ApiMethod.TrackLyricsTranslationPost] = new CustomRequestParameters
147122
{
148123
EndpointResource = "track.lyrics.translation.post",
149-
RequestMethod = "POST"
124+
RequestMethod = RequestMethod.POST
150125
},
151126
[ApiMethod.CrowdUserFeedbackGet] = new CustomRequestParameters
152127
{
@@ -155,7 +130,7 @@ public enum ApiMethod
155130
[ApiMethod.TrackLyricsPost] = new CustomRequestParameters
156131
{
157132
EndpointResource = "track.lyrics.post",
158-
RequestMethod = "POST"
133+
RequestMethod = RequestMethod.POST
159134
},
160135
[ApiMethod.ChartTracksGet] = new CustomRequestParameters
161136
{
@@ -168,7 +143,7 @@ public enum ApiMethod
168143
[ApiMethod.AiQuestionPost] = new CustomRequestParameters
169144
{
170145
EndpointResource = "crowd.ai.question.post",
171-
RequestMethod = "POST"
146+
RequestMethod = RequestMethod.POST
172147
},
173148
[ApiMethod.CrowdUserSuggestionLyricsGet] = new CustomRequestParameters
174149
{
@@ -189,7 +164,7 @@ public enum ApiMethod
189164
[ApiMethod.CredentialPost] = new CustomRequestParameters
190165
{
191166
EndpointResource = "credential.post",
192-
RequestMethod = "POST"
167+
RequestMethod = RequestMethod.POST
193168
},
194169
[ApiMethod.CrowdChartUsersGet] = new CustomRequestParameters
195170
{
@@ -202,7 +177,7 @@ public enum ApiMethod
202177
[ApiMethod.TrackRichsyncPost] = new CustomRequestParameters
203178
{
204179
EndpointResource = "track.richsync.post",
205-
RequestMethod = "POST"
180+
RequestMethod = RequestMethod.POST
206181
},
207182
[ApiMethod.CrowdScoreGet] = new CustomRequestParameters
208183
{
@@ -259,7 +234,6 @@ public MusixmatchApiResponse SendRequestLegacy(ApiMethod method, Dictionary<stri
259234
requestParameters = new CustomRequestParameters();
260235

261236
string endpoint = requestParameters.EndpointResource;
262-
string requestMethod = requestParameters.RequestMethod;
263237

264238
if (additionalArguments == null)
265239
additionalArguments = new Dictionary<string, string>();
@@ -272,14 +246,25 @@ public MusixmatchApiResponse SendRequestLegacy(ApiMethod method, Dictionary<stri
272246
string arguments = GetArgumentString(additionalArguments);
273247

274248
string requestUrl = $"{context.ApiUrl}{endpoint}{arguments}";
275-
string response = RequestFilter(requestUrl, requestMethod, data);
249+
250+
string response = string.Empty;
251+
switch (requestParameters.RequestMethod)
252+
{
253+
case RequestMethod.GET:
254+
response = RequestProcessor.Get(requestUrl);
255+
break;
256+
case RequestMethod.POST:
257+
response = RequestProcessor.Post(requestUrl, data);
258+
break;
259+
}
276260

277261
var responseParsed = JObject.Parse(response);
278262

279263
return new MusixmatchApiResponse
280264
{
281265
StatusCode = responseParsed.SelectToken("$..status_code", false).Value<int>(),
282266
TimeElapsed = responseParsed.SelectToken("$..execute_time", false).Value<double>(),
267+
Verbose = responseParsed.SelectToken("$..debug", false).Value<List<string>>(),
283268
Body = responseParsed.SelectToken("$..body").ToString(),
284269
Header = responseParsed.SelectToken("$..header").ToString()
285270
};

MusixmatchClientLib/API/Contexts/ApiContext.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Text;
4-
5-
namespace MusixmatchClientLib.API.Contexts
1+
namespace MusixmatchClientLib.API.Contexts
62
{
73
public enum ApiContext
84
{

MusixmatchClientLib/API/Contexts/MusixmatchApiContext.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Text;
1+
using System.Collections.Generic;
42

53
namespace MusixmatchClientLib.API.Contexts
64
{
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
6-
7-
namespace MusixmatchClientLib.API
1+
namespace MusixmatchClientLib.API
82
{
3+
public enum RequestMethod
4+
{
5+
GET,
6+
POST
7+
}
8+
99
class CustomRequestParameters
1010
{
11-
public string RequestMethod { get; set; } = "GET";
11+
public RequestMethod RequestMethod { get; set; } = RequestMethod.GET;
1212
public string EndpointResource { get; set; } = "";
1313
}
1414
}

MusixmatchClientLib/API/MissionManager.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
using MusixmatchClientLib.API.Model;
2-
using Newtonsoft.Json;
1+
using Newtonsoft.Json;
32
using System;
43
using System.Collections.Generic;
54
using System.IO;
6-
using System.Linq;
75
using System.Net;
86
using System.Text;
9-
using System.Threading.Tasks;
10-
using static MusixmatchClientLib.API.MissionManager.MissionResponse;
117

128
namespace MusixmatchClientLib.API
139
{
@@ -17,7 +13,7 @@ public class MissionManager
1713

1814
internal MissionManager(string jwtToken) => this.jwtToken = jwtToken;
1915

20-
public List<Mission> ParseMissions(string userToken, string userId)
16+
public List<MissionResponse.Mission> ParseMissions(string userToken, string userId)
2117
{
2218
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://missions-backend.musixmatch.com/graphql"); // GraphQL request, now looking for the real endpoint (if it exists)
2319
request.Method = "POST";

MusixmatchClientLib/API/Model/Exceptions/MusixmatchRequestException.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
using MusixmatchClientLib.API.Model.Types;
22
using System;
33
using System.Collections.Generic;
4-
using System.Linq;
5-
using System.Text;
6-
using System.Threading.Tasks;
74

85
namespace MusixmatchClientLib.API.Model.Exceptions
96
{

MusixmatchClientLib/API/Model/MusixmatchApiResponse.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
using Newtonsoft.Json;
2-
using System;
32
using System.Collections.Generic;
4-
using System.Linq;
5-
using System.Text;
6-
using System.Threading.Tasks;
73

84
namespace MusixmatchClientLib.API.Model
95
{
@@ -12,6 +8,7 @@ public class MusixmatchApiResponse
128
public int StatusCode { get; set; }
139
public double TimeElapsed { get; set; }
1410
public string Header { get; set; }
11+
public List<string> Verbose { get; set; } // I guess, this one is temporary
1512
public string Body { get; set; }
1613

1714
public T Cast<T>() where T : MusixmatchApiResponse => JsonConvert.DeserializeObject<T>(Body, new JsonSerializerSettings { Error = (se, ev) => { ev.ErrorContext.Handled = true; } });

MusixmatchClientLib/API/Model/AlbumGet.cs renamed to MusixmatchClientLib/API/Model/Requests/AlbumGet.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
using MusixmatchClientLib.API.Model.Types;
22
using Newtonsoft.Json;
3-
using System;
4-
using System.Collections.Generic;
5-
using System.Text;
63

7-
namespace MusixmatchClientLib.API.Model
4+
namespace MusixmatchClientLib.API.Model.Requests
85
{
96
public class AlbumGet : MusixmatchApiResponse
107
{

MusixmatchClientLib/API/Model/AlbumTracksGet.cs renamed to MusixmatchClientLib/API/Model/Requests/AlbumTracksGet.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
using MusixmatchClientLib.API.Model.Types;
22
using Newtonsoft.Json;
3-
using System;
43
using System.Collections.Generic;
5-
using System.Text;
64

7-
namespace MusixmatchClientLib.API.Model
5+
namespace MusixmatchClientLib.API.Model.Requests
86
{
97
public class AlbumTracksGet : MusixmatchApiResponse
108
{

0 commit comments

Comments
 (0)