From 8ef13336b4afbea1c42af0497efd0f78736eba1f Mon Sep 17 00:00:00 2001 From: Lee Jun Kit Date: Sun, 20 Jun 2021 09:19:13 +0800 Subject: [PATCH] add search endpoint to API --- Spottie.xcodeproj/project.pbxproj | 32 +++++++++++++------ Spottie/Backend/SpotifyAPI.swift | 21 ++++++++++++ .../Types/API/SearchResultsResponse.swift | 7 ++++ .../{Base => WebAPI}/WebAPIArtistObject.swift | 0 .../{Base => WebAPI}/WebAPIDeviceObject.swift | 0 .../{Base => WebAPI}/WebAPIImageObject.swift | 0 .../Types/WebAPI/WebAPIPagingObject.swift | 18 +++++++++++ .../WebAPIPlaylistObject.swift | 0 .../WebAPIPlaylistTrackObject.swift | 0 .../WebAPIPlaylistTracksRefObject.swift | 0 .../WebAPIPublicUserObject.swift | 0 .../WebAPISimplifiedAlbumObject.swift | 0 .../WebAPISimplifiedArtistObject.swift | 0 .../{Base => WebAPI}/WebAPITrackObject.swift | 0 14 files changed, 68 insertions(+), 10 deletions(-) rename Spottie/Backend/Types/{Base => WebAPI}/WebAPIArtistObject.swift (100%) rename Spottie/Backend/Types/{Base => WebAPI}/WebAPIDeviceObject.swift (100%) rename Spottie/Backend/Types/{Base => WebAPI}/WebAPIImageObject.swift (100%) create mode 100644 Spottie/Backend/Types/WebAPI/WebAPIPagingObject.swift rename Spottie/Backend/Types/{Base => WebAPI}/WebAPIPlaylistObject.swift (100%) rename Spottie/Backend/Types/{Base => WebAPI}/WebAPIPlaylistTrackObject.swift (100%) rename Spottie/Backend/Types/{Base => WebAPI}/WebAPIPlaylistTracksRefObject.swift (100%) rename Spottie/Backend/Types/{Base => WebAPI}/WebAPIPublicUserObject.swift (100%) rename Spottie/Backend/Types/{Base => WebAPI}/WebAPISimplifiedAlbumObject.swift (100%) rename Spottie/Backend/Types/{Base => WebAPI}/WebAPISimplifiedArtistObject.swift (100%) rename Spottie/Backend/Types/{Base => WebAPI}/WebAPITrackObject.swift (100%) diff --git a/Spottie.xcodeproj/project.pbxproj b/Spottie.xcodeproj/project.pbxproj index a84b018..0b879d2 100644 --- a/Spottie.xcodeproj/project.pbxproj +++ b/Spottie.xcodeproj/project.pbxproj @@ -73,6 +73,7 @@ 475EE248267D7784007BEBDC /* SearchField.swift in Sources */ = {isa = PBXBuildFile; fileRef = 475EE247267D7784007BEBDC /* SearchField.swift */; }; 475EE24A267DA451007BEBDC /* GreenPlayButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 475EE249267DA451007BEBDC /* GreenPlayButton.swift */; }; 475EE24C267EBDE7007BEBDC /* SearchResultsResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = 475EE24B267EBDE7007BEBDC /* SearchResultsResponse.swift */; }; + 475EE24F267EBFEA007BEBDC /* WebAPIPagingObject.swift in Sources */ = {isa = PBXBuildFile; fileRef = 475EE24E267EBFEA007BEBDC /* WebAPIPagingObject.swift */; }; 47C56F602679A789003EA20A /* PlayerCommands.swift in Sources */ = {isa = PBXBuildFile; fileRef = 47C56F5F2679A789003EA20A /* PlayerCommands.swift */; }; /* End PBXBuildFile section */ @@ -145,6 +146,7 @@ 475EE247267D7784007BEBDC /* SearchField.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchField.swift; sourceTree = ""; }; 475EE249267DA451007BEBDC /* GreenPlayButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GreenPlayButton.swift; sourceTree = ""; }; 475EE24B267EBDE7007BEBDC /* SearchResultsResponse.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchResultsResponse.swift; sourceTree = ""; }; + 475EE24E267EBFEA007BEBDC /* WebAPIPagingObject.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WebAPIPagingObject.swift; sourceTree = ""; }; 47C56F5F2679A789003EA20A /* PlayerCommands.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PlayerCommands.swift; sourceTree = ""; }; /* End PBXFileReference section */ @@ -247,6 +249,7 @@ isa = PBXGroup; children = ( 4730619126591EAC001E3A1F /* Base */, + 475EE24D267EBFD2007BEBDC /* WebAPI */, 4730619026591EA6001E3A1F /* Events */, 473061A22659FFDD001E3A1F /* API */, 473061712656629F001E3A1F /* Nothing.swift */, @@ -290,16 +293,6 @@ 4730618826591E16001E3A1F /* DateObject.swift */, 4730618A26591E36001E3A1F /* CoverGroupObject.swift */, 4730618C26591E50001E3A1F /* ImageObject.swift */, - 473061A5265B4A10001E3A1F /* WebAPITrackObject.swift */, - 470201B0265B54720030ECA9 /* WebAPISimplifiedAlbumObject.swift */, - 470201B2265B55F30030ECA9 /* WebAPISimplifiedArtistObject.swift */, - 470201B4265B56350030ECA9 /* WebAPIImageObject.swift */, - 470201B6265B56860030ECA9 /* WebAPIArtistObject.swift */, - 474BAFB926687AB60006EB16 /* WebAPIDeviceObject.swift */, - 475798CC266F0F7F00AADF2F /* WebAPIPlaylistObject.swift */, - 475798CE266F101600AADF2F /* WebAPIPublicUserObject.swift */, - 475798D0266F103100AADF2F /* WebAPIPlaylistTrackObject.swift */, - 475798D4266F1B9B00AADF2F /* WebAPIPlaylistTracksRefObject.swift */, 474BAFBB2668B0170006EB16 /* RepeatMode.swift */, ); path = Base; @@ -331,6 +324,24 @@ path = Components; sourceTree = ""; }; + 475EE24D267EBFD2007BEBDC /* WebAPI */ = { + isa = PBXGroup; + children = ( + 473061A5265B4A10001E3A1F /* WebAPITrackObject.swift */, + 470201B0265B54720030ECA9 /* WebAPISimplifiedAlbumObject.swift */, + 470201B2265B55F30030ECA9 /* WebAPISimplifiedArtistObject.swift */, + 470201B4265B56350030ECA9 /* WebAPIImageObject.swift */, + 470201B6265B56860030ECA9 /* WebAPIArtistObject.swift */, + 474BAFB926687AB60006EB16 /* WebAPIDeviceObject.swift */, + 475798CC266F0F7F00AADF2F /* WebAPIPlaylistObject.swift */, + 475798CE266F101600AADF2F /* WebAPIPublicUserObject.swift */, + 475798D0266F103100AADF2F /* WebAPIPlaylistTrackObject.swift */, + 475798D4266F1B9B00AADF2F /* WebAPIPlaylistTracksRefObject.swift */, + 475EE24E267EBFEA007BEBDC /* WebAPIPagingObject.swift */, + ); + path = WebAPI; + sourceTree = ""; + }; 47C56F5E2679A779003EA20A /* Commands */ = { isa = PBXGroup; children = ( @@ -478,6 +489,7 @@ 475798CD266F0F7F00AADF2F /* WebAPIPlaylistObject.swift in Sources */, 475EE248267D7784007BEBDC /* SearchField.swift in Sources */, 4730619526591EF9001E3A1F /* PlaybackResumedEvent.swift in Sources */, + 475EE24F267EBFEA007BEBDC /* WebAPIPagingObject.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/Spottie/Backend/SpotifyAPI.swift b/Spottie/Backend/SpotifyAPI.swift index 3c6b2da..d30d640 100644 --- a/Spottie/Backend/SpotifyAPI.swift +++ b/Spottie/Backend/SpotifyAPI.swift @@ -136,4 +136,25 @@ extension SpotifyAPI { return client.run(req, decoder).map(\.value).eraseToAnyPublisher() } + + static func search() -> AnyPublisher { + let queryItems = [ + URLQueryItem(name: "q", value: "akmu"), + URLQueryItem(name: "type", value: "album,artist,playlist,track"), + URLQueryItem(name: "market", value: "from_token") + ] + + var urlComponents = URLComponents( + url: base.appendingPathComponent("/web-api/v1/search"), + resolvingAgainstBaseURL: false)! + urlComponents.queryItems = queryItems + + var req = URLRequest(url: urlComponents.url!) + req.httpMethod = "GET" + + let decoder = JSONDecoder() + decoder.keyDecodingStrategy = .convertFromSnakeCase; + + return client.run(req, decoder).map(\.value).eraseToAnyPublisher() + } } diff --git a/Spottie/Backend/Types/API/SearchResultsResponse.swift b/Spottie/Backend/Types/API/SearchResultsResponse.swift index aeee5f0..f70ae4d 100644 --- a/Spottie/Backend/Types/API/SearchResultsResponse.swift +++ b/Spottie/Backend/Types/API/SearchResultsResponse.swift @@ -6,3 +6,10 @@ // import Foundation + +struct SearchResultsResponse: Decodable { + let albums: WebAPIPagingObject + let artists: WebAPIPagingObject + let tracks: WebAPIPagingObject + let playlists: WebAPIPagingObject +} diff --git a/Spottie/Backend/Types/Base/WebAPIArtistObject.swift b/Spottie/Backend/Types/WebAPI/WebAPIArtistObject.swift similarity index 100% rename from Spottie/Backend/Types/Base/WebAPIArtistObject.swift rename to Spottie/Backend/Types/WebAPI/WebAPIArtistObject.swift diff --git a/Spottie/Backend/Types/Base/WebAPIDeviceObject.swift b/Spottie/Backend/Types/WebAPI/WebAPIDeviceObject.swift similarity index 100% rename from Spottie/Backend/Types/Base/WebAPIDeviceObject.swift rename to Spottie/Backend/Types/WebAPI/WebAPIDeviceObject.swift diff --git a/Spottie/Backend/Types/Base/WebAPIImageObject.swift b/Spottie/Backend/Types/WebAPI/WebAPIImageObject.swift similarity index 100% rename from Spottie/Backend/Types/Base/WebAPIImageObject.swift rename to Spottie/Backend/Types/WebAPI/WebAPIImageObject.swift diff --git a/Spottie/Backend/Types/WebAPI/WebAPIPagingObject.swift b/Spottie/Backend/Types/WebAPI/WebAPIPagingObject.swift new file mode 100644 index 0000000..0c61a5c --- /dev/null +++ b/Spottie/Backend/Types/WebAPI/WebAPIPagingObject.swift @@ -0,0 +1,18 @@ +// +// WebAPIPagingObject.swift +// Spottie +// +// Created by Lee Jun Kit on 20/6/21. +// + +import Foundation + +struct WebAPIPagingObject: Decodable { + let href: String + let items: [T] + let limit: Int + let offset: Int + let total: Int + let next: String + let previous: String +} diff --git a/Spottie/Backend/Types/Base/WebAPIPlaylistObject.swift b/Spottie/Backend/Types/WebAPI/WebAPIPlaylistObject.swift similarity index 100% rename from Spottie/Backend/Types/Base/WebAPIPlaylistObject.swift rename to Spottie/Backend/Types/WebAPI/WebAPIPlaylistObject.swift diff --git a/Spottie/Backend/Types/Base/WebAPIPlaylistTrackObject.swift b/Spottie/Backend/Types/WebAPI/WebAPIPlaylistTrackObject.swift similarity index 100% rename from Spottie/Backend/Types/Base/WebAPIPlaylistTrackObject.swift rename to Spottie/Backend/Types/WebAPI/WebAPIPlaylistTrackObject.swift diff --git a/Spottie/Backend/Types/Base/WebAPIPlaylistTracksRefObject.swift b/Spottie/Backend/Types/WebAPI/WebAPIPlaylistTracksRefObject.swift similarity index 100% rename from Spottie/Backend/Types/Base/WebAPIPlaylistTracksRefObject.swift rename to Spottie/Backend/Types/WebAPI/WebAPIPlaylistTracksRefObject.swift diff --git a/Spottie/Backend/Types/Base/WebAPIPublicUserObject.swift b/Spottie/Backend/Types/WebAPI/WebAPIPublicUserObject.swift similarity index 100% rename from Spottie/Backend/Types/Base/WebAPIPublicUserObject.swift rename to Spottie/Backend/Types/WebAPI/WebAPIPublicUserObject.swift diff --git a/Spottie/Backend/Types/Base/WebAPISimplifiedAlbumObject.swift b/Spottie/Backend/Types/WebAPI/WebAPISimplifiedAlbumObject.swift similarity index 100% rename from Spottie/Backend/Types/Base/WebAPISimplifiedAlbumObject.swift rename to Spottie/Backend/Types/WebAPI/WebAPISimplifiedAlbumObject.swift diff --git a/Spottie/Backend/Types/Base/WebAPISimplifiedArtistObject.swift b/Spottie/Backend/Types/WebAPI/WebAPISimplifiedArtistObject.swift similarity index 100% rename from Spottie/Backend/Types/Base/WebAPISimplifiedArtistObject.swift rename to Spottie/Backend/Types/WebAPI/WebAPISimplifiedArtistObject.swift diff --git a/Spottie/Backend/Types/Base/WebAPITrackObject.swift b/Spottie/Backend/Types/WebAPI/WebAPITrackObject.swift similarity index 100% rename from Spottie/Backend/Types/Base/WebAPITrackObject.swift rename to Spottie/Backend/Types/WebAPI/WebAPITrackObject.swift