Skip to content

Commit

Permalink
add json endpoints to HTTP API
Browse files Browse the repository at this point in the history
  • Loading branch information
BenediktAlkin committed Nov 6, 2021
1 parent 7659c36 commit 40d7dd8
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 2 deletions.
2 changes: 2 additions & 0 deletions BackendAPI/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ public static class Constants
public const string NONE = "NONE";
public const string SOME = "SOME";
public const string ALL = "ALL";

public const string JSON_RESULT = "result";
}
}
9 changes: 9 additions & 0 deletions BackendAPI/Controllers/AlbumController.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Backend;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

Expand Down Expand Up @@ -45,6 +46,9 @@ public async Task<bool[]> AssignTag(string tag, [FromQuery] string id)
return success;
}

[HttpPost("json/tags/{tag}/album")]
public async Task<Dictionary<string, bool[]>> JsonAssignTag(string tag, [FromQuery] string id) => new() { { Constants.JSON_RESULT, await AssignTag(tag, id) } };


[HttpDelete("tags/{tag}/album")]
public async Task<bool[]> DeleteAssignment(string tag, [FromQuery] string id)
Expand Down Expand Up @@ -72,6 +76,9 @@ public async Task<bool[]> DeleteAssignment(string tag, [FromQuery] string id)

return success;
}
[HttpDelete("json/tags/{tag}/album")]
public async Task<Dictionary<string, bool[]>> JsonDeleteAssignment(string tag, [FromQuery] string id) => new() { { Constants.JSON_RESULT, await DeleteAssignment(tag, id) } };


[HttpGet("tags/{tag}/album")]
public async Task<string> IsTagged(string tag, [FromQuery] string id)
Expand Down Expand Up @@ -103,5 +110,7 @@ public async Task<string> IsTagged(string tag, [FromQuery] string id)
return tracksAreTagged;
}

[HttpGet("json/tags/{tag}/album")]
public async Task<Dictionary<string, string>> JsonIsTagged(string tag, [FromQuery] string id) => new() { { Constants.JSON_RESULT, await IsTagged(tag, id) } };
}
}
4 changes: 4 additions & 0 deletions BackendAPI/Controllers/ConnectionController.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Backend;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

Expand Down Expand Up @@ -43,5 +44,8 @@ public string UserId()
timer.DetailMessage = $"userId={user.Id}";
return user.Id;
}

[HttpGet("json/connection/userid")]
public Dictionary<string, string> JsonUserId() => new() { { Constants.JSON_RESULT, UserId() } };
}
}
9 changes: 8 additions & 1 deletion BackendAPI/Controllers/PlaylistController.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Backend;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

Expand Down Expand Up @@ -44,6 +45,8 @@ public async Task<bool[]> AssignTag(string tag, [FromQuery] string id)

return success;
}
[HttpPost("json/tags/{tag}/playlist")]
public async Task<Dictionary<string, bool[]>> JsonAssignTag(string tag, [FromQuery] string id) => new() { { Constants.JSON_RESULT, await AssignTag(tag, id) } };


[HttpDelete("tags/{tag}/playlist")]
Expand Down Expand Up @@ -72,6 +75,9 @@ public async Task<bool[]> DeleteAssignment(string tag, [FromQuery] string id)

return success;
}
[HttpDelete("json/tags/{tag}/playlist")]
public async Task<Dictionary<string, bool[]>> JsonDeleteAssignment(string tag, [FromQuery] string id) => new() { { Constants.JSON_RESULT, await DeleteAssignment(tag, id) } };


[HttpGet("tags/{tag}/playlist")]
public async Task<string> IsTagged(string tag, [FromQuery] string id)
Expand Down Expand Up @@ -102,6 +108,7 @@ public async Task<string> IsTagged(string tag, [FromQuery] string id)
timer.DetailMessage = $"result={tracksAreTagged}";
return tracksAreTagged;
}

[HttpGet("json/tags/{tag}/playlist")]
public async Task<Dictionary<string, string>> JsonIsTagged(string tag, [FromQuery] string id) => new() { { Constants.JSON_RESULT, await IsTagged(tag, id) } };
}
}
4 changes: 4 additions & 0 deletions BackendAPI/Controllers/TagController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public string[] GetTags()
timer.DetailMessage = $"result={string.Join(',', allTags)}";
return allTags;
}
[HttpGet("json/tags")]
public Dictionary<string, string[]> JsonGetTags() => new() { { Constants.JSON_RESULT, GetTags() } };

[HttpGet("taggroups")]
public Dictionary<string, string[]> GetTagGroups()
Expand All @@ -42,5 +44,7 @@ public Dictionary<string, string[]> GetTagGroups()
timer.DetailMessage = $"result={{{msg}}}";
return groups;
}
[HttpGet("json/taggroups")]
public Dictionary<string, Dictionary<string, string[]>> JsonGetTagGroups() => new() { { Constants.JSON_RESULT, GetTagGroups() } };
}
}
8 changes: 7 additions & 1 deletion BackendAPI/Controllers/TrackController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public async Task<bool[]> AssignTag(string tag, [FromQuery(Name = "id")] string[

return success;
}
[HttpPost("json/tags/{tag}/tracks")]
public async Task<Dictionary<string, bool[]>> JsonAssignTag(string tag, [FromQuery(Name = "id")] string[] ids) => new() { { Constants.JSON_RESULT, await AssignTag(tag, ids) } };


[HttpDelete("tags/{tag}/tracks")]
Expand All @@ -63,6 +65,9 @@ public bool[] DeleteAssignment(string tag, [FromQuery(Name = "id")] string[] ids

return success;
}
[HttpDelete("json/tags/{tag}/tracks")]
public Dictionary<string, bool[]> JsonDeleteAssignment(string tag, [FromQuery(Name = "id")] string[] ids) => new() { { Constants.JSON_RESULT, DeleteAssignment(tag, ids) } };


[HttpGet("tags/{tag}/tracks")]
public string IsTagged(string tag, [FromQuery(Name = "id")] string[] ids)
Expand All @@ -85,6 +90,7 @@ public string IsTagged(string tag, [FromQuery(Name = "id")] string[] ids)
timer.DetailMessage = $"result={tracksAreTagged}";
return tracksAreTagged;
}

[HttpGet("json/tags/{tag}/tracks")]
public Dictionary<string, string> JsonIsTagged(string tag, [FromQuery(Name = "id")] string[] ids) => new() { { Constants.JSON_RESULT, IsTagged(tag, ids) } };
}
}
4 changes: 4 additions & 0 deletions BackendAPI/documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

Running "BackendAPI.exe" will start a HTTP server on http://localhost:63848/ with the following endpoints.

## JSON format
All endpoints described here use text/plain as return type. The return value is wrapped into a json object when an endpoint is prefixed with json/.
E.g. If the endpoint GET connection/userid returns *exampleuserid* the endpoint GET json/connection/userid will return *{"result":"exampleuserid"}*

## Connection
The API does need access to the SpotifyAPI. Upon starting the API it will check if a user is already logged in. If no user is logged in it will open a browser window for the user to login. The login for the API is independent of the login for the main app. A user remains logged into the API until the logout endpoint is called.

Expand Down

0 comments on commit 40d7dd8

Please sign in to comment.