@@ -11,7 +11,7 @@ import Alamofire
1111protocol WebServicesContract : AnyObject {
1212 var endpoint : String { get }
1313 func fetchAnimals( completion: @escaping ( Result < [ Animal ] , Error > ) -> ( ) )
14- func registerAnimal( with parameters: [ String : Any ] , handler : @escaping ( ( ) -> Void ) )
14+ func registerAnimal( with parameters: Animal , completion : @escaping ( Result < Void , Error > ) -> Void )
1515}
1616
1717class WebServices : WebServicesContract {
@@ -31,34 +31,18 @@ class WebServices: WebServicesContract {
3131 }
3232 }
3333
34- func registerAnimal( with parameters: [ String : Any ] , handler: @escaping ( ( ) -> Void ) ) {
35- AF . request ( endpoint, method: . post, parameters: parameters, encoding: JSONEncoding . default, headers: nil ) . validate ( statusCode: 200 ..< 299 ) . responseData { response in
36- switch response. result {
37- case . success( let data) :
38- handler ( )
39- do {
40- guard let jsonObject = try JSONSerialization . jsonObject ( with: data) as? [ String : Any ] else {
41- print ( " Error: Cannot convert data to JSON object " )
42- return
43- }
44- guard let prettyJsonData = try ? JSONSerialization . data ( withJSONObject: jsonObject, options: . prettyPrinted) else {
45- print ( " Error: Cannot convert JSON object to Pretty JSON data " )
46- return
47- }
48- guard let prettyPrintedJson = String ( data: prettyJsonData, encoding: . utf8) else {
49- print ( " Error: Could print JSON in String " )
50- return
51- }
52-
53- print ( prettyPrintedJson)
54- } catch {
55- print ( " Error: Trying to convert JSON data to string " )
56- return
57- }
58- case . failure( let error) :
59- handler ( )
60- print ( error)
61- }
34+ func registerAnimal( with parameters: Animal , completion: @escaping ( Result < Void , Error > ) -> Void ) {
35+
36+ AF . request ( endpoint,
37+ method: . post,
38+ parameters: parameters,
39+ encoder: JSONParameterEncoder . default) . response { response in
40+ switch response. result {
41+ case . success:
42+ completion ( . success( ( ) ) )
43+ case . failure( let error) :
44+ completion ( . failure( error) )
6245 }
46+ }
6347 }
6448}
0 commit comments