A simple class that makes REST API requests and mock these datas automatically (if you wanna).
If you don't want to use larger libraries than will takes up large spaces on your project, add this class and make all of your REST API requests and mock these datas when you need this.
With URLMockSession you can load local mocked datas without hard work.
Why use this class?
- Shortly code;
- Simple application;
- Works with threads;
- Mock your API responses;
- Easily switch to load mock or REST API data;
- This class takes up less space (only 14kb);
First set all of your configs
to apply URLMockSession basic informations:
var config = MockConfig()
config.baseURL = "https://viacep.com.br/ws/"
config.timeout = 60
config.headers = []
config.token = ""
config.mock = true
config.testingFail = false
config.testingSuccess = false
After complete class configs, make your REST API request:
let manager = URLMockSession(with: config)
manager.request(method: .get,
endpoint: "01001000/json/",
parameters: [:],
authenticated: false,
responseType: Dictionary<String, String>.self) { (response, code) in
print(String(describing: response))
}
If you want to see all mocked data, implement this script:
let mocks = MockManager.shared.all() // Array<Mock>
for item in mocks {
print(item.endpoint)
}
- iOS 10+
- Swift 5
- Xcode 10
- UIKit
- Decodable
- URLSession
- FileManager
- UserDefaults
- Date
- Dictionary
- URLResponse