Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moya - How enable sample data?

Tags:

ios

swift

moya

I started learning Moya of for Netwroking

It required during implementing TargetType

Is there is way to not provide sample data for every end point

And, I can't to find way how enable/disable sample data.

var sampleData: Data {
    switch self {
    case .zen:
        return "Half measures are as bad as nothing at all.".utf8Encoded
    case .showUser(let id):
        return "{\"id\": \(id), \"first_name\": \"Harry\", \"last_name\": \"Potter\"}".utf8Encoded
    case .createUser(let firstName, let lastName):
        return "{\"id\": 100, \"first_name\": \"\(firstName)\", \"last_name\": \"\(lastName)\"}".utf8Encoded
    case .updateUser(let id, let firstName, let lastName):
        return "{\"id\": \(id), \"first_name\": \"\(firstName)\", \"last_name\": \"\(lastName)\"}".utf8Encoded
    case .showAccounts:
        // Provided you have a file named accounts.json in your bundle.
        guard let url = Bundle.main.url(forResource: "accounts", withExtension: "json"),
            let data = try? Data(contentsOf: url) else {
                return Data()
        }
        return data
    }
}

How I can do it?

Answer

static let provider = MoyaProvider<VMAPI>.init(stubClosure: MoyaProvider<VMAPI>.immediatelyStub)
like image 911
IvanovDeveloper Avatar asked Jun 06 '18 10:06

IvanovDeveloper


1 Answers

For disable or enable sample Data in Moya, we should init provider with required state.

static let provider = MoyaProvider<VMAPI>.init(stubClosure: MoyaProvider<VMAPI>.immediatelyStub)
like image 130
IvanovDeveloper Avatar answered Sep 29 '22 06:09

IvanovDeveloper