procedure SendRequest(contentToSend: Text; RequestMethod: Text; AdditionalURL: Text): text
var
SpotifyGeneralSetup: Record "SBC Spotify General Setup";
client: HttpClient;
request: HttpRequestMessage;
response: HttpResponseMessage;
contentHeaders: HttpHeaders;
content: HttpContent;
requestUri: Text;
responseText: Text;
errorBodyContent: Text;
begin
SpotifyGeneralSetup.get();
SpotifyGeneralSetup.TestField("Spotify API URL");
requestUri := SpotifyGeneralSetup."Spotify API URL" + AdditionalURL;
content.WriteFrom(contentToSend);
content.GetHeaders(contentHeaders);
contentHeaders.Clear();
contentHeaders.Add('Content-Type', 'application/json');
request.Content := content;
request.SetRequestUri(requestUri);
request.Method := RequestMethod;
client.DefaultRequestHeaders().Add(
'Authorization',
'Bearer ' + GetSpotifyAccessToken());
client.Send(request, response);
response.Content().ReadAs(responseText);
if not response.IsSuccessStatusCode() then begin
response.Content().ReadAs(errorBodyContent);
Error(RequestErr, response.HttpStatusCode(), errorBodyContent);
end;
exit(responseText);
end;
procedure GetSpotifyAccessToken(): Text
var
SpotifyGeneralSetup: Record "SBC Spotify General Setup";
OAuth20Appln: Record "OAuth 2.0 Application";
OAuth20AppHelper: Codeunit "OAuth 2.0 App. Helper";
MessageText: Text;
begin
SpotifyGeneralSetup.Get();
SpotifyGeneralSetup.TestField("Spotify OAuth settings");
OAuth20Appln.Get(SpotifyGeneralSetup."Spotify OAuth settings");
if not OAuth20AppHelper.RequestAccessToken(OAuth20Appln, MessageText) then
Error(MessageText);
exit(OAuth20AppHelper.GetAccessToken(OAuth20Appln));
end;
{
"name": "My Best Music"
}
procedure ModifyPlaylist(EntityName: Text; EntityValue: Text; PlaylistId: Text)
var
SBCSpotifyAPIMgt: Codeunit "SBC Spotify API Mgt";
contentToSend: Text;
JObject: JsonObject;
begin
JObject.Add(EntityName, EntityValue);
JObject.WriteTo(contentToSend);
SBCSpotifyAPIMgt.SendRequest(contentToSend, 'PUT', StrSubstNo('playlists/%1', PlaylistId));
end;
ModifyPlaylist(LowerCase(Rec.FieldName(Name)), Rec.Name, Rec.Id);
procedure GetNextPageID(Data: Text; PageScope: Text): Integer
var
JToken: JsonToken;
JToken2: JsonToken;
JObject: JsonObject;
ExitValue: Integer;
begin
if Data = '' then
exit;
JToken.ReadFrom(Data);
JObject := JToken.AsObject();
if PageScope <> '' then begin
JObject.SelectToken(PageScope, JToken2);
JObject := JToken2.AsObject();
end;
JObject.Get('next', JToken2);
if JToken2.AsValue().IsNull() then
ExitValue := 0
else begin
JObject.Get('limit', JToken2);
ExitValue := JToken2.AsValue().AsInteger();
JObject.Get('offset', JToken2);
ExitValue += JToken2.AsValue().AsInteger();
end;
exit(ExitValue);
end;
procedure GetMyPlaylists()
var
SBCJSONMgt: Codeunit "SBC JSON Mgt";
NextPageID: Integer;
NextPageExist: Boolean;
AddUrl: Text;
Data: Text;
HttpStatusCode: Integer;
begin
NextPageExist := true;
while NextPageExist do begin
AddUrl := StrSubstNo('me/playlists?limit=50&offset=%1', NextPageID);
if GetRequest(AddUrl, Data, HttpStatusCode, '', NextPageID) then
SBCJSONMgt.UpdateMyPlaylists(Data);
NextPageExist := NextPageID <> 0;
end;
end;