Skip to content

Commit

Permalink
add search endpoint to API
Browse files Browse the repository at this point in the history
  • Loading branch information
Lee Jun Kit committed Jun 20, 2021
1 parent aaad352 commit 8ef1333
Show file tree
Hide file tree
Showing 14 changed files with 68 additions and 10 deletions.
32 changes: 22 additions & 10 deletions Spottie.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

Expand Down Expand Up @@ -145,6 +146,7 @@
475EE247267D7784007BEBDC /* SearchField.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchField.swift; sourceTree = "<group>"; };
475EE249267DA451007BEBDC /* GreenPlayButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GreenPlayButton.swift; sourceTree = "<group>"; };
475EE24B267EBDE7007BEBDC /* SearchResultsResponse.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchResultsResponse.swift; sourceTree = "<group>"; };
475EE24E267EBFEA007BEBDC /* WebAPIPagingObject.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WebAPIPagingObject.swift; sourceTree = "<group>"; };
47C56F5F2679A789003EA20A /* PlayerCommands.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PlayerCommands.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -247,6 +249,7 @@
isa = PBXGroup;
children = (
4730619126591EAC001E3A1F /* Base */,
475EE24D267EBFD2007BEBDC /* WebAPI */,
4730619026591EA6001E3A1F /* Events */,
473061A22659FFDD001E3A1F /* API */,
473061712656629F001E3A1F /* Nothing.swift */,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -331,6 +324,24 @@
path = Components;
sourceTree = "<group>";
};
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 = "<group>";
};
47C56F5E2679A779003EA20A /* Commands */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -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;
};
Expand Down
21 changes: 21 additions & 0 deletions Spottie/Backend/SpotifyAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,25 @@ extension SpotifyAPI {

return client.run(req, decoder).map(\.value).eraseToAnyPublisher()
}

static func search() -> AnyPublisher<SearchResultsResponse, Error> {
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()
}
}
7 changes: 7 additions & 0 deletions Spottie/Backend/Types/API/SearchResultsResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,10 @@
//

import Foundation

struct SearchResultsResponse: Decodable {
let albums: WebAPIPagingObject<WebAPISimplifiedAlbumObject>
let artists: WebAPIPagingObject<WebAPISimplifiedAlbumObject>
let tracks: WebAPIPagingObject<WebAPITrackObject>
let playlists: WebAPIPagingObject<WebAPIPlaylistObject>
}
18 changes: 18 additions & 0 deletions Spottie/Backend/Types/WebAPI/WebAPIPagingObject.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// WebAPIPagingObject.swift
// Spottie
//
// Created by Lee Jun Kit on 20/6/21.
//

import Foundation

struct WebAPIPagingObject<T: Decodable>: Decodable {
let href: String
let items: [T]
let limit: Int
let offset: Int
let total: Int
let next: String
let previous: String
}

0 comments on commit 8ef1333

Please sign in to comment.