Skip to content

Commit

Permalink
Add milestones method
Browse files Browse the repository at this point in the history
  • Loading branch information
rudrankriyam committed Apr 6, 2023
1 parent 069e560 commit 7228ef9
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,20 @@
import Foundation

/// A milestone object representing a music summary milestone.
struct MusicSummaryMilestone: Codable {

///
/// A MusicSummaryMilestone object represents a milestone that a user has achieved in their music listening history, such as the number of unique songs or artists listened to.
///
/// It includes the unique identifier for the milestone, the amount of time the user spent listening to music to reach the milestone, the date the milestone was reached, the value associated with the milestone, and the type of music item associated with the milestone.
///
/// Example usage:
///
/// let milestone: MusicSummaryMilestone = ...
/// print("ID: \(milestone.id), Listen Time: \(milestone.listenTimeInMinutes)")
/// print("Date Reached: \(milestone.dateReached), Value: \(milestone.value)")
/// print("Kind: \(milestone.kind)")
///
public struct MusicSummaryMilestone: Codable {

/// The unique identifier for the music summary milestone.
let id: String

Expand Down Expand Up @@ -41,7 +53,7 @@ extension MusicSummaryMilestone {
}

extension MusicSummaryMilestone {
init(from decoder: Decoder) throws {
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
id = try container.decode(String.self, forKey: .id)

Expand All @@ -52,7 +64,7 @@ extension MusicSummaryMilestone {
kind = try attributesContainer.decode(MusicSummaryMilestoneKind.self, forKey: .kind)
}

func encode(to encoder: Encoder) throws {
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(id, forKey: .id)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// MusicSummaryMilestones.swift
// MusanovaKit
//
// Created by Rudrank Riyam on 07/04/23.
//

/// A collection of `MusicSummaryMilestone` objects.
///
/// The `MusicSummaryMilestones` typealias is a convenient way to represent a collection
/// of `MusicSummaryMilestone` objects. It is an alias for an array of `MusicSummaryMilestone`.
///
/// Example usage:
///
/// let milestones: MusicSummaryMilestones = ...
///
/// for milestone in milestones {
/// print("ID: \(milestone.id), Listen Time: \(milestone.listenTimeInMinutes)")
/// print("Date Reached: \(milestone.dateReached), Value: \(milestone.value)")
/// print("Kind: \(milestone.kind)")
/// }
///
public typealias MusicSummaryMilestones = [MusicSummaryMilestone]
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Foundation
/// An enumeration of the different types of music items that can be included in a music summary milestone.
///
/// Use this enumeration to specify the type of music items to include in a music summary milestone, such as the user's top artists, songs, or albums.
enum MusicSummaryMilestonesMusicItemsType: String {
public enum MusicSummaryMilestonesMusicItemsType: String {

/// The user's top artists.
case topArtists = "top-artists"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,42 @@

import Foundation

public extension MSummaries {

/// Fetches music summary milestones for a specified year and list of music item types.
///
/// Use this method to fetch music summary milestones for a specific year and types of music items.
/// The music summary milestones include counts for items such as songs, albums, and playlists that a user has played during a specified year.
///
/// Example usage:
///
/// do {
/// let milestones = try await MSummaries.milestones(forYear: 2023, developerToken: "your_developer_token")
///
/// for milestone in milestones {
/// print("ID: \(milestone.id), Listen Time: \(milestone.listenTimeInMinutes)")
/// print("Date Reached: \(milestone.dateReached), Value: \(milestone.value)")
/// print("Kind: \(milestone.kind)")
/// }
/// } catch {
/// print(error)
/// }
///
/// - Parameters:
/// - year: The year to fetch music summary milestones for.
/// - musicItemTypes: The types of music items to include in the music summary milestones.
/// - developerToken: The developer token used to authorize the request.
///
/// - Returns: An array of `MusicSummaryMilestone` objects containing the fetched music summary milestones.
///
/// - Throws: An error of type `URLError` or `DecodingError` if the request fails or the response cannot be decoded.
static func milestones(forYear year: MusicYearID, musicItemTypes: [MusicSummaryMilestonesMusicItemsType] = [], developerToken: String) async throws -> MusicSummaryMilestones {
let request = MusicSummaryMilestonesRequest(year: year, types: musicItemTypes, developerToken: developerToken)
let response = try await request.response()
return response
}
}

/// A request object used to fetch music summary milestones for a specified year and list of music item types.
struct MusicSummaryMilestonesRequest {

Expand All @@ -33,14 +69,13 @@ struct MusicSummaryMilestonesRequest {

/// Sends the request and returns a response object containing the fetched music summary milestones.
///
/// - Returns: A `MusicSummaryMilestonesResponse` object.
public func response() async throws -> MusicSummaryMilestonesResponse {
/// - Returns: A `MusicSummaryMilestones` object.
public func response() async throws -> MusicSummaryMilestones {
let url = try musicSummariesMilestonesEndpointURL
let request = MusicPrivilegedDataRequest(url: url, developerToken: developerToken)
let response = try await request.response()
let milestonesResponse = try JSONDecoder().decode(MusicSummaryMilestonesResponse.self, from: response.data)
print(milestonesResponse)
return milestonesResponse
return milestonesResponse.milestones
}
}

Expand Down

0 comments on commit 7228ef9

Please sign in to comment.