11using MusixmatchClientLib . API . Contexts ;
22using MusixmatchClientLib . API . Model ;
3+ using MusixmatchClientLib . API . Processors ;
34using Newtonsoft . Json . Linq ;
45using System ;
56using System . Collections . Generic ;
67using System . IO ;
7- using System . Linq ;
88using System . Net ;
99using System . Text ;
10- using System . Threading . Tasks ;
1110
1211namespace 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 } ;
0 commit comments