     
     // Metoda sprawdza czy odpowiedź serwera zawiera pole 'message' - jeśli tak oznacza to, że coś poszło nie tak,
     // w przypadku jego braku dostajemy dane o które prosiliśmy
-    private func wrongRequest(responseValue rawJson: AnyObject) -> ServerResultType<JSON> {
-        let json = JSON(rawJson)
-        if let message = json.dictionary?["message"]?.string {
-            return .Error(err: ServerError.ErrorWithMessage(text: message))
-        }
-        return .Success(obj: json)
+    private func handleResponse<T>(responseResult result: Alamofire.Result<AnyObject, NSError>,
+                                               completion: (ServerResultType<T>)->(),
+                                               successAction: ((JSON) -> (T))) {
+        switch result {
+        case .Success(let val):
+            let json = JSON(val)
+            if let message = json.dictionary?["message"]?.string {
+                completion(.Error(err: ServerError.ErrorWithMessage(text: message)))
+            } else {
+                completion(.Success(obj: successAction(json)))
+            }
+            
+        case .Failure(let err):
+            completion(.Error(err: err))
+        }
+        
+    }
+    
+    private func handleResponse(responseResult result: Alamofire.Result<AnyObject, NSError>,
+                                completion: (ServerResultType<JSON>)->(),
+                                successAction: ((JSON) -> ())? = nil ) {
+        handleResponse(responseResult: result, completion: completion) { json in
+            return json
+        }
     }
     
     // Jeśli udało się zalogować metoda zwróci ServerResultType.Success z obiektem nil,
     // w przeciwnym wypadku obiekt to String z odpowiedzią serwera (powód błędu logowania)
     public func login(username: String, password: String, completion: (ServerResultType<JSON>)->()) {
         request(.GET, url: url(fromRouter: Router.User(child: .Current))).responseJSON {
-            
-            switch $0.result {
-            case .Success(let value):
-                
-                let wrongRequest = self.wrongRequest(responseValue: value)
-                
-                if case .Success = wrongRequest {
-                    self.username = username
-                    self.password = password
-                    self.defaults.setObject(username, forKey: UserDefaultsKeys.LoggedUserUsername.rawValue)
-                    self.defaults.setObject(password, forKey: UserDefaultsKeys.LoggedUserPassword.rawValue)
-                }
-                completion(wrongRequest)
-                
-            case .Failure(let error):
-                completion(.Error(err: error))
-            }
-        }
-    }
-    
-    
-    func deck(deckID: String?, completion: (ServerResultType<JSON>) -> ()) {
-        var router: Router!
-        if let deckID = deckID {
-            router = Router.Deck(child: .Single(id: deckID))
-        } else {
-            router = Router.Deck(child: .All)
-        }
+            self.handleResponse(responseResult: $0.result, completion: completion) { json in
+                self.username = username
+                self.password = password
+                self.defaults.setObject(username, forKey: UserDefaultsKeys.LoggedUserUsername.rawValue)
+                self.defaults.setObject(password, forKey: UserDefaultsKeys.LoggedUserPassword.rawValue)
+                return json
+            }
+        }
+    }
+    
+    
+    func deck(deckID: String, completion: (ServerResultType<JSON>) -> ()) {
+        let router = Router.Deck(child: .Single(id: deckID))
         request(.GET, url: url(fromRouter: router)).responseJSON {
-            switch $0.result {
-            case .Success(let value):
-                completion(self.wrongRequest(responseValue: value))
-            case .Failure(let error):
-                completion(.Error(err: error))
-            }
-            
+            self.handleResponse(responseResult: $0.result, completion: completion)
         }
     }
     

    let localDataManager = LocalDataManager()
    
    
    private func complicatedMethod<ServerResponseObject, DataManaterResponseObject>(mode: ManagerMode,
                                   localFetch:() -> (DataManaterResponseObject?),
                                   remoteFetch: ((ServerResultType<ServerResponseObject>) -> ())->(),
                                   remoteParsing: (obj: ServerResponseObject) -> (DataManaterResponseObject?),
                                   completion: (DataManagerResponse<DataManaterResponseObject>) -> ()) {
        
        var localBlock:(()-> (DataManagerResponse<DataManaterResponseObject>))?
        
        /// Obsluga roznych trybow - tylko lokalne dane, tylko dane z serwera, badz dane lokalne w przypadku braku dostepu do danych z serwera
        if mode.contains(.Local) {

                                                "flashcardsCount": flashcardsCount,
                                                "name": name]
        request(.GET, url: url(fromRouter: Router.Deck(child: .All)), parameters: parameters).responseJSON {
            self.handleResponse(responseResult: $0.result, completion: completion) { json in
                return json.arrayValue
            }
        }
    }
    
    func addFlashcard(deckId: String, flashcardInJSON json: JSON, completion: (ServerResultType<JSON>) -> ()) {
        
        request(.POST, url: url(fromRouter: Router.Flashcards(child: .Post, deckId: deckId))).responseJSON {
            self.handleResponse(responseResult: $0.result, completion: completion)
        }
        
    }
