Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds username AUTH, disconnect methods. #84

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion Sources/SwiftRedis/Redis.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import Foundation
public class Redis {

/// Redis Serialization Protocol handle
private var respHandle: RedisResp?
public var respHandle: RedisResp?
scuml marked this conversation as resolved.
Show resolved Hide resolved

/// Whether the client is connected or not.
/// Does not reflect state changes in the event of a disconnect.
Expand All @@ -48,9 +48,15 @@ public class Redis {
callback(nil)
}
}

/// Disconnects from a redis server
public func disconnect(){
respHandle?.disconnect()
}

/// Authenticate against the server
///
/// - Parameter username: String for the username (Redis 6+ only).
scuml marked this conversation as resolved.
Show resolved Hide resolved
/// - Parameter pswd: String for the password.
/// - Parameter callback: callback function that is called after authenticating,
/// NSError will be nil if successful.
Expand All @@ -61,6 +67,16 @@ public class Redis {
callback(error)
}
}

public func auth(_ username: String, _ pswd: String, callback: (NSError?) -> Void) {
if username == "" {
return auth(pswd, callback: callback)
}
issueCommand("AUTH", username, pswd) {(response: RedisResponse) in
let (_, error) = self.redisOkResponseHandler(response, nilOk: false)
callback(error)
}
}

/// Select the database to use
///
Expand Down
8 changes: 6 additions & 2 deletions Sources/SwiftRedis/RedisResp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ enum RedisRespStatus {
case connected
}

class RedisResp {
public class RedisResp {
mbarnach marked this conversation as resolved.
Show resolved Hide resolved
///
/// Socket used to talk with the server
private var socket: Socket?
public var socket: Socket?
scuml marked this conversation as resolved.
Show resolved Hide resolved

// Mark: Prebuilt constant UTF8 strings (these strings are all proper UTF-8 strings)
private static let asterisk = RedisString("*").asData
Expand All @@ -46,6 +46,10 @@ class RedisResp {
try? socket?.connect(to: host, port: port)
}

func disconnect(){
socket?.close()
}

func issueCommand(_ stringArgs: [String], callback: (RedisResponse) -> Void) {
guard let socket = socket else { return }

Expand Down