Moya: How can i pass custom parameters to an endpoint?

Created on 1 Dec 2015  路  7Comments  路  Source: Moya/Moya

I want to add to my headers a credentials, so i created an endpoint:

var endpointClosure = { (target: TestApi) -> Endpoint<TestApi> in
    let endpoint: Endpoint<TestApi> = Endpoint<TestApi>(URL: url(target), sampleResponseClosure: {.NetworkResponse(200, target.sampleData)}, method: target.method, parameters: target.parameters)
     let credentialData = "userString:passwordString".dataUsingEncoding(NSUTF8StringEncoding)!
     let base64Credentials = credentialData.base64EncodedStringWithOptions([])
     return endpoint.endpointByAddingHTTPHeaderFields(["Authorization": "Basic \(base64Credentials)"])
}

And then i created my provider:

let TestApiProvider = MoyaProvider<TestApi>(endpointClosure: endpointClosure)
enum TestApi {
     case Login
}

How can i'm supposed to pass parameters to my closure to provide userString and passwordString?
I created a GIST with the code, basically in my viewController im calling a function with an user and pass
func testLogin(user: String, password: String) {
And i need to provide those string to my header.

question

Most helpful comment

Ah! I see. You can switch on the target and return a different endpoint.

var endpointClosure = { (target: TestApi) -> Endpoint<TestApi> in
    let endpoint: Endpoint<TestApi> = Endpoint<TestApi>(URL: url(target), sampleResponseClosure: {.NetworkResponse(200, target.sampleData)}, method: target.method, parameters: target.parameters)


    switch target {
        case Login(let userString, let passwordString):    
         let credentialData = "\(userString):\(passwordString)".dataUsingEncoding(NSUTF8StringEncoding)!
         let base64Credentials = credentialData.base64EncodedStringWithOptions([])
         return endpoint.endpointByAddingHTTPHeaderFields(["Authorization": "Basic \(base64Credentials)"])
    }

    return endpoint
}

All 7 comments

Hey, good question! Moya uses Swift's associated values on enums in order to pass in parameters.

enum TestApi {
     case Login(userString: String, passwordString: String)
}

let endpoint = TestApi.Login(userString: "Ash", passwordString: "password")
target.request(endpoint)

Does that make sense?

Nope, i want to pass those parameters to my endpointClosure cause i need to modify my headers
See my endPointClousure which uses "userString" and "passwordString"
let credentialData = "userString:passwordString".dataUsingEncoding(NSUTF8StringEncoding)!

Ah! I see. You can switch on the target and return a different endpoint.

var endpointClosure = { (target: TestApi) -> Endpoint<TestApi> in
    let endpoint: Endpoint<TestApi> = Endpoint<TestApi>(URL: url(target), sampleResponseClosure: {.NetworkResponse(200, target.sampleData)}, method: target.method, parameters: target.parameters)


    switch target {
        case Login(let userString, let passwordString):    
         let credentialData = "\(userString):\(passwordString)".dataUsingEncoding(NSUTF8StringEncoding)!
         let base64Credentials = credentialData.base64EncodedStringWithOptions([])
         return endpoint.endpointByAddingHTTPHeaderFields(["Authorization": "Basic \(base64Credentials)"])
    }

    return endpoint
}

Awesome!
Edit suggest: case .Login

@ashfurrow Just curious if this is still the best solution doing HTTP Basic Auth with Moya. Didn't found a way to set NSURLAuthenticationMethodHTTPBasic

Is it correct that I can't use the credentialsPlugin doing this?

Thanks

Yup, that's right!

@ashfurrow Alright, thank you for your fast response!

Was this page helpful?
0 / 5 - 0 ratings