@@ -28,131 +28,60 @@ import Foundation
2828
2929internal struct StudyplusAPIRequest {
3030
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+
3235 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+ }
3343
3444 internal init ( accessToken: String ) {
3545 self . accessToken = accessToken
3646 }
3747
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 ,
4150 failure: @escaping ( _ error: StudyplusError ) -> Void ) {
42-
43- start ( path: path, method: " POST " , body: params, success: { ( response) in
44-
51+ studyRecord ( record, success: {
4552 DispatchQueue . main. async {
46- success ( response )
53+ success ( )
4754 }
48-
49- } , failure: { statusCode, response in
50-
55+ } , failure: { error in
5156 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)
5758 }
5859 } )
5960 }
6061
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 " )
8668 request. addValue ( " OAuth " + accessToken, forHTTPHeaderField: " Authorization " )
69+ request. httpBody = try ? encoder. encode ( record)
8770
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)
9274 return
9375 }
9476
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 , " " ) )
9779 return
9880 }
9981
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 ( )
14383 }
14484
145- #if DEBUG
146- NSLog ( " StudyplusAPIRequest path:%@ method:%@ " , url. absoluteString, method)
147- #endif
148-
14985 task. resume ( )
15086 }
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- }
15887}
0 commit comments