This repository was archived by the owner on Sep 15, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathStatsEmailsSummaryData.swift
More file actions
94 lines (80 loc) · 3 KB
/
Copy pathStatsEmailsSummaryData.swift
File metadata and controls
94 lines (80 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import Foundation
public struct StatsEmailsSummaryData: Decodable, Equatable {
public let posts: [Post]
public init(posts: [Post]) {
self.posts = posts
}
private enum CodingKeys: String, CodingKey {
case posts = "posts"
}
public init(from decoder: any Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
posts = try container.decode([Post].self, forKey: .posts)
}
public struct Post: Codable, Equatable {
public let id: Int
public let link: URL
public let date: Date
public let title: String
public let type: PostType
public let opens: Int
public let clicks: Int
public init(id: Int, link: URL, date: Date, title: String, type: PostType, opens: Int, clicks: Int) {
self.id = id
self.link = link
self.date = date
self.title = title
self.type = type
self.opens = opens
self.clicks = clicks
}
public enum PostType: String, Codable {
case post = "post"
}
private enum CodingKeys: String, CodingKey {
case id = "id"
case link = "href"
case date = "date"
case title = "title"
case type = "type"
case opens = "opens"
case clicks = "clicks"
}
public init(from decoder: any Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
id = try container.decode(Int.self, forKey: .id)
link = try container.decode(URL.self, forKey: .link)
title = (try? container.decodeIfPresent(String.self, forKey: .title)) ?? ""
type = (try? container.decodeIfPresent(PostType.self, forKey: .type)) ?? .post
opens = (try? container.decodeIfPresent(Int.self, forKey: .opens)) ?? 0
clicks = (try? container.decodeIfPresent(Int.self, forKey: .clicks)) ?? 0
self.date = try container.decode(Date.self, forKey: .date)
}
}
}
extension StatsEmailsSummaryData {
public static var pathComponent: String {
return "stats/emails/summary"
}
public init?(jsonDictionary: [String: AnyObject]) {
do {
let jsonData = try JSONSerialization.data(withJSONObject: jsonDictionary, options: [])
let decoder = JSONDecoder.apiDecoder
self = try decoder.decode(Self.self, from: jsonData)
} catch {
return nil
}
}
public static func queryProperties(quantity: Int, sortField: SortField, sortOrder: SortOrder) -> [String: String] {
return ["quantity": String(quantity), "sort_field": sortField.rawValue, "sort_order": sortOrder.rawValue]
}
public enum SortField: String {
case opens = "opens"
case postId = "post_id"
case postDate = "post_date"
}
public enum SortOrder: String {
case descending = "desc"
case ascending = "ASC"
}
}