forked from wikimedia/wikipedia-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMediaWikiApiErrors.swift
108 lines (84 loc) · 3.45 KB
/
MediaWikiApiErrors.swift
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import Foundation
/// An object that is passed through from fetchers to view controllers, for reference when displaying errors in a panel.
@objc public class MediaWikiAPIDisplayError: NSObject {
// Fully resolved html to display in the blocked panel.
@objc public let messageHtml: String
// Base url to be referenced when user taps a relative link within the messageHtml in the blocked panel.
public let linkBaseURL: URL
// Error code, passed through from original MediaWikiAPIError. Currently used for logging.
@objc public let code: String
public init(messageHtml: String, linkBaseURL: URL, code: String) {
self.messageHtml = messageHtml
self.linkBaseURL = linkBaseURL
self.code = code
}
public var loggingErrorReasonDomain: String {
return "MediaWikiAPIResponseError"
}
}
/// Represents errors that come in the MediaWiki API response.
/// See https://www.mediawiki.org/wiki/API:Errors_and_warnings
public struct MediaWikiAPIError: Codable {
public struct Data: Codable {
public struct BlockInfo: Codable {
let blockReason: String
let blockPartial: Bool
let blockedBy: String
let blockID: Int64
let blockExpiry: String
let blockedTimestamp: String
enum CodingKeys: String, CodingKey {
case blockReason = "blockreason"
case blockPartial = "blockpartial"
case blockedBy = "blockedby"
case blockID = "blockid"
case blockExpiry = "blockexpiry"
case blockedTimestamp = "blockedtimestamp"
}
init?(dict: [String: Any]) {
guard let blockReason = dict["blockreason"] as? String,
let blockPartial = dict["blockpartial"] as? Bool,
let blockedBy = dict["blockedby"] as? String,
let blockID = dict["blockid"] as? Int64,
let blockExpiry = dict["blockexpiry"] as? String,
let blockedTimestamp = dict["blockedtimestamp"] as? String else {
return nil
}
self.blockReason = blockReason
self.blockPartial = blockPartial
self.blockedBy = blockedBy
self.blockID = blockID
self.blockExpiry = blockExpiry
self.blockedTimestamp = blockedTimestamp
}
}
let blockInfo: BlockInfo?
enum CodingKeys: String, CodingKey {
case blockInfo = "blockinfo"
}
init?(dict: [String: Any]) {
guard let blockInfoDict = dict["blockinfo"] as? [String: Any] else {
self.blockInfo = nil
return
}
self.blockInfo = BlockInfo(dict: blockInfoDict)
}
}
public let code: String
let html: String
let data: Data?
init?(dict: [String: Any]) {
guard let code = dict["code"] as? String,
let html = dict["html"] as? String
else {
return nil
}
self.code = code
self.html = html
guard let dataDict = dict["data"] as? [String: Any] else {
self.data = nil
return
}
self.data = Data(dict: dataDict)
}
}