@@ -28,131 +28,60 @@ import Foundation
28
28
29
29
internal struct StudyplusAPIRequest {
30
30
31
- private let apiVersion : Int = 1
31
+ private static let endPoint : String = " https://external-api.studyplus.jp "
32
+ private static let path : String = " /v1/study_record "
33
+ private let studyRecordUrl : URL = URL ( string: endPoint + path) !
34
+
32
35
private let accessToken : String
36
+ private var encoder : JSONEncoder {
37
+ let encoder = JSONEncoder ( )
38
+ encoder. keyEncodingStrategy = . convertToSnakeCase
39
+ encoder. dateEncodingStrategy = . iso8601
40
+
41
+ return encoder
42
+ }
33
43
34
44
internal init ( accessToken: String ) {
35
45
self . accessToken = accessToken
36
46
}
37
47
38
- internal func post( path: String ,
39
- params: [ String : Any ] ,
40
- success: @escaping ( _ response: [ AnyHashable : Any ] ) -> Void ,
48
+ internal func post( _ record: StudyplusRecord ,
49
+ success: @escaping ( ) -> Void ,
41
50
failure: @escaping ( _ error: StudyplusError ) -> Void ) {
42
-
43
- start ( path: path, method: " POST " , body: params, success: { ( response) in
44
-
51
+ studyRecord ( record, success: {
45
52
DispatchQueue . main. async {
46
- success ( response )
53
+ success ( )
47
54
}
48
-
49
- } , failure: { statusCode, response in
50
-
55
+ } , failure: { error in
51
56
DispatchQueue . main. async {
52
- if let message: String = response ? [ " message " ] as? String {
53
- failure ( StudyplusError ( statusCode, message) )
54
- } else {
55
- failure ( . unknownReason( " Not connected to the network or StudyplusAPIRequest Error " ) )
56
- }
57
+ failure ( error)
57
58
}
58
59
} )
59
60
}
60
61
61
- // MARK: - private
62
-
63
- private func start( path: String ,
64
- method: String ,
65
- body: [ String : Any ] ,
66
- success: @escaping ( _ response: [ AnyHashable : Any ] ) -> Void ,
67
- failure: @escaping ( _ statusCode: Int , _ response: [ String : Any ] ? ) -> Void ) {
68
-
69
- guard let url = buildUrl ( path: path) else {
70
- failure ( 0 , nil )
71
- return
72
- }
73
-
74
- var request = URLRequest ( url: url)
75
- request. httpMethod = method
76
-
77
- do {
78
- let data = try JSONSerialization . data ( withJSONObject: body, options: . prettyPrinted)
79
- request. addValue ( " application/json; charaset=utf-8 " , forHTTPHeaderField: " Content-Type " )
80
- request. httpBody = data
81
- } catch {
82
- failure ( 0 , nil )
83
- return
84
- }
85
-
62
+ private func studyRecord( _ record: StudyplusRecord ,
63
+ success: @escaping ( ) -> Void ,
64
+ failure: @escaping ( _ error: StudyplusError ) -> Void ) {
65
+ var request = URLRequest ( url: studyRecordUrl)
66
+ request. httpMethod = " POST "
67
+ request. addValue ( " application/json; charaset=utf-8 " , forHTTPHeaderField: " Content-Type " )
86
68
request. addValue ( " OAuth " + accessToken, forHTTPHeaderField: " Authorization " )
69
+ request. httpBody = try ? encoder. encode ( record)
87
70
88
- let task = URLSession . shared. dataTask ( with: request) { ( data, response, error) in
89
-
90
- guard error == nil else {
91
- failure ( 0 , nil )
71
+ let task = URLSession . shared. dataTask ( with: request) { data, response, error in
72
+ guard data != nil , error == nil , let response = response as? HTTPURLResponse else {
73
+ failure ( . postRecordFailed)
92
74
return
93
75
}
94
76
95
- guard let httpResponse : HTTPURLResponse = response as? HTTPURLResponse else {
96
- failure ( 0 , nil )
77
+ guard ( 200 ... 204 ) . contains ( response. statusCode ) else {
78
+ failure ( StudyplusError ( response . statusCode , " " ) )
97
79
return
98
80
}
99
81
100
- switch httpResponse. statusCode {
101
- case 200 , 201 , 202 :
102
- guard let data = data else {
103
- failure ( 0 , nil )
104
- return
105
- }
106
-
107
- do {
108
- let jsonObject = try JSONSerialization . jsonObject ( with: data, options: . allowFragments)
109
- guard let obj = jsonObject as? [ String : Any ] else {
110
- failure ( 0 , nil )
111
- return
112
- }
113
-
114
- success ( obj)
115
-
116
- } catch {
117
- #if DEBUG
118
- print ( " -- StudyplusAPIRequest Json Error Path: \( url. absoluteString) , Method: \( method) , Description: \( error. localizedDescription) -- " )
119
- #endif
120
- failure ( httpResponse. statusCode, [ " message " : error. localizedDescription] )
121
- }
122
- case 204 :
123
- success ( [ : ] )
124
- return
125
- default :
126
- #if DEBUG
127
- print ( " -- StudyplusAPIRequest Path: \( url. absoluteString) , Method: \( method) , StatusCode: \( httpResponse. statusCode) -- " )
128
- #endif
129
- guard let data = data else {
130
- failure ( httpResponse. statusCode, nil )
131
- return
132
- }
133
-
134
- do {
135
- let jsonObject = try JSONSerialization . jsonObject ( with: data, options: . allowFragments)
136
- failure ( httpResponse. statusCode, jsonObject as? [ String : Any ] )
137
- return
138
-
139
- } catch let jsonError {
140
- failure ( httpResponse. statusCode, [ " message " : jsonError. localizedDescription] )
141
- }
142
- }
82
+ success ( )
143
83
}
144
84
145
- #if DEBUG
146
- NSLog ( " StudyplusAPIRequest path:%@ method:%@ " , url. absoluteString, method)
147
- #endif
148
-
149
85
task. resume ( )
150
86
}
151
-
152
- private func buildUrl( path: String ) -> URL ? {
153
-
154
- let fullPath : String = " https://external-api.studyplus.jp/v \( apiVersion) / \( path) "
155
- guard let url = URL ( string: fullPath) else { return nil }
156
- return url
157
- }
158
87
}
0 commit comments