mirror of
https://github.com/Sarsoo/Spotify.NET.git
synced 2024-12-24 23:16:28 +00:00
Refactored Query and QueryAsync Methord (#209)
This commit is contained in:
parent
5559c84990
commit
aa8e940726
@ -2,12 +2,18 @@
|
|||||||
using SpotifyAPI.Local.Models;
|
using SpotifyAPI.Local.Models;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Collections.Specialized;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Text;
|
using System.Runtime.CompilerServices;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using System.Web;
|
||||||
|
|
||||||
|
[assembly: InternalsVisibleTo("SpotifyAPI.Tests")]
|
||||||
|
|
||||||
|
|
||||||
namespace SpotifyAPI.Local
|
namespace SpotifyAPI.Local
|
||||||
{
|
{
|
||||||
|
|
||||||
internal class RemoteHandler
|
internal class RemoteHandler
|
||||||
{
|
{
|
||||||
public string OauthKey { get; private set; }
|
public string OauthKey { get; private set; }
|
||||||
@ -29,23 +35,32 @@ namespace SpotifyAPI.Local
|
|||||||
|
|
||||||
internal async Task SendPauseRequest()
|
internal async Task SendPauseRequest()
|
||||||
{
|
{
|
||||||
await QueryAsync("remote/pause.json?pause=true", true, true, -1).ConfigureAwait(false);
|
var @params = new NameValueCollection() { { "pause", "true" } };
|
||||||
|
await QueryAsync("remote/pause.json", true, true, -1, @params).ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal async Task SendPlayRequest()
|
internal async Task SendPlayRequest()
|
||||||
{
|
{
|
||||||
await QueryAsync("remote/pause.json?pause=false", true, true, -1).ConfigureAwait(false);
|
var @params = new NameValueCollection() { { "pause", "false" } };
|
||||||
|
await QueryAsync("remote/pause.json", true, true, -1, @params).ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal async Task SendPlayRequest(string url, string context = "")
|
internal async Task SendPlayRequest(string url, string context = "")
|
||||||
{
|
{
|
||||||
|
|
||||||
// TODO: instead of having an empty context, one way to fix the bug with the playback time beyond the length of a song would be to provide a 1-song context, and it would be fixed.
|
// TODO: instead of having an empty context, one way to fix the bug with the playback time beyond the length of a song would be to provide a 1-song context, and it would be fixed.
|
||||||
await QueryAsync($"remote/play.json?uri={url}&context={context}", true, true, -1).ConfigureAwait(false);
|
var @params = new NameValueCollection() { {"uri", url },
|
||||||
|
{ "context", context} };
|
||||||
|
await QueryAsync($"remote/play.json", true, true, -1, @params).ConfigureAwait(false);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
internal async Task SendQueueRequest(string url)
|
internal async Task SendQueueRequest(string url)
|
||||||
{
|
{
|
||||||
await QueryAsync("remote/play.json?uri=" + url + "?action=queue", true, true, -1).ConfigureAwait(false);
|
var @params = new NameValueCollection() { {"uri", url },
|
||||||
|
{ "action", "queue"} };
|
||||||
|
await QueryAsync("remote/play.json", true, true, -1, @params).ConfigureAwait(false);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
internal StatusResponse GetNewStatus()
|
internal StatusResponse GetNewStatus()
|
||||||
@ -83,31 +98,41 @@ namespace SpotifyAPI.Local
|
|||||||
return cfidList[0].Error == null ? cfidList[0].Token : "";
|
return cfidList[0].Error == null ? cfidList[0].Token : "";
|
||||||
}
|
}
|
||||||
|
|
||||||
internal string Query(string request, bool oauth, bool cfid, int wait)
|
internal string BuildQueryString(bool oauth, bool cfid, int wait, NameValueCollection @params = null)
|
||||||
{
|
{
|
||||||
string parameters = "?&ref=&cors=&_=" + GetTimestamp();
|
if (@params == null)
|
||||||
if (request.Contains("?"))
|
|
||||||
{
|
{
|
||||||
parameters = parameters.Substring(1);
|
@params = new NameValueCollection();
|
||||||
}
|
}
|
||||||
|
var queryParameter = HttpUtility.ParseQueryString(string.Empty);
|
||||||
|
queryParameter.Add(@params);
|
||||||
|
queryParameter.Add(new NameValueCollection() {
|
||||||
|
{ "ref", string.Empty},
|
||||||
|
{ "cors", string.Empty},
|
||||||
|
{ "_", GetTimestamp().ToString()}
|
||||||
|
});
|
||||||
if (oauth)
|
if (oauth)
|
||||||
{
|
{
|
||||||
parameters += "&oauth=" + OauthKey;
|
queryParameter.Add("oauth", OauthKey);
|
||||||
}
|
}
|
||||||
if (cfid)
|
if (cfid)
|
||||||
{
|
{
|
||||||
parameters += "&csrf=" + CfidKey;
|
queryParameter.Add("csrf", CfidKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (wait != -1)
|
if (wait != -1)
|
||||||
{
|
{
|
||||||
parameters += "&returnafter=" + wait;
|
queryParameter.Add("returnafter", wait.ToString());
|
||||||
parameters += "&returnon=login%2Clogout%2Cplay%2Cpause%2Cerror%2Cap";
|
queryParameter.Add("returnon", "login%2Clogout%2Cplay%2Cpause%2Cerror%2Cap");
|
||||||
}
|
}
|
||||||
|
|
||||||
string address = $"{_config.HostUrl}:{_config.Port}/{request}{parameters}";
|
return queryParameter.ToString();
|
||||||
string response = "";
|
}
|
||||||
|
internal string Query(string baseUrl, bool oauth, bool cfid, int wait, NameValueCollection @params = null)
|
||||||
|
{
|
||||||
|
string parameters = BuildQueryString(oauth, cfid, wait, @params);
|
||||||
|
string address = $"{_config.HostUrl}:{_config.Port}/{baseUrl}{parameters}";
|
||||||
|
string response = string.Empty;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
using (var wc = new ExtendedWebClient())
|
using (var wc = new ExtendedWebClient())
|
||||||
@ -126,30 +151,10 @@ namespace SpotifyAPI.Local
|
|||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal async Task<string> QueryAsync(string request, bool oauth, bool cfid, int wait)
|
internal async Task<string> QueryAsync(string baseUrl, bool oauth, bool cfid, int wait, NameValueCollection @params = null)
|
||||||
{
|
{
|
||||||
string parameters = "?&ref=&cors=&_=" + GetTimestamp();
|
string parameters = BuildQueryString(oauth, cfid, wait, @params);
|
||||||
if (request.Contains("?"))
|
string address = $"{_config.HostUrl}:{_config.Port}/{baseUrl}{parameters}";
|
||||||
{
|
|
||||||
parameters = parameters.Substring(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (oauth)
|
|
||||||
{
|
|
||||||
parameters += "&oauth=" + OauthKey;
|
|
||||||
}
|
|
||||||
if (cfid)
|
|
||||||
{
|
|
||||||
parameters += "&csrf=" + CfidKey;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (wait != -1)
|
|
||||||
{
|
|
||||||
parameters += "&returnafter=" + wait;
|
|
||||||
parameters += "&returnon=login%2Clogout%2Cplay%2Cpause%2Cerror%2Cap";
|
|
||||||
}
|
|
||||||
|
|
||||||
string address = $"{_config.HostUrl}:{_config.Port}/{request}{parameters}";
|
|
||||||
string response = "";
|
string response = "";
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@ -166,7 +171,6 @@ namespace SpotifyAPI.Local
|
|||||||
|
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal int GetTimestamp()
|
internal int GetTimestamp()
|
||||||
{
|
{
|
||||||
return Convert.ToInt32((DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds);
|
return Convert.ToInt32((DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds);
|
||||||
|
Loading…
Reference in New Issue
Block a user