Skip to content
This repository has been archived by the owner on Jan 28, 2019. It is now read-only.

Commit

Permalink
Merge pull request #5 from SlackKit/exclude-members
Browse files Browse the repository at this point in the history
Exclude members
  • Loading branch information
pvzig committed Jul 14, 2017
2 parents 69eefe9 + 1c0ef19 commit 6e5eada
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 35 deletions.
54 changes: 28 additions & 26 deletions Sources/NetworkInterface.swift
Original file line number Diff line number Diff line change
Expand Up @@ -130,33 +130,8 @@ public struct NetworkInterface {
request.httpMethod = "POST"
let boundaryConstant = randomBoundary()
let contentType = "multipart/form-data; boundary=" + boundaryConstant
let boundaryStart = "--\(boundaryConstant)\r\n"
let boundaryEnd = "--\(boundaryConstant)--\r\n"
let contentDispositionString = "Content-Disposition: form-data; name=\"file\"; filename=\"\(filename)\"\r\n"
let contentTypeString = "Content-Type: \(filetype)\r\n\r\n"
let dataEnd = "\r\n"

guard
let boundaryStartData = boundaryStart.data(using: .utf8),
let dispositionData = contentDispositionString.data(using: .utf8),
let contentTypeData = contentTypeString.data(using: .utf8),
let boundaryEndData = boundaryEnd.data(using: .utf8),
let dataEndData = dataEnd.data(using: .utf8)
else {
errorClosure(SlackError.clientNetworkError)
return
}

var requestBodyData = Data()
requestBodyData.append(contentsOf: boundaryStartData)
requestBodyData.append(contentsOf: dispositionData)
requestBodyData.append(contentsOf: contentTypeData)
requestBodyData.append(contentsOf: data)
requestBodyData.append(contentsOf: dataEndData)
requestBodyData.append(contentsOf: boundaryEndData)

request.setValue(contentType, forHTTPHeaderField: "Content-Type")
request.httpBody = requestBodyData as Data
request.httpBody = requestBodyData(data: data, boundaryConstant: boundaryConstant, filename: filename, filetype: filetype)

session.dataTask(with: request) {(data, response, publicError) in
do {
Expand Down Expand Up @@ -201,6 +176,33 @@ public struct NetworkInterface {
}
}

private func requestBodyData(data: Data, boundaryConstant: String, filename: String, filetype: String) -> Data? {
let boundaryStart = "--\(boundaryConstant)\r\n"
let boundaryEnd = "--\(boundaryConstant)--\r\n"
let contentDispositionString = "Content-Disposition: form-data; name=\"file\"; filename=\"\(filename)\"\r\n"
let contentTypeString = "Content-Type: \(filetype)\r\n\r\n"
let dataEnd = "\r\n"

guard
let boundaryStartData = boundaryStart.data(using: .utf8),
let dispositionData = contentDispositionString.data(using: .utf8),
let contentTypeData = contentTypeString.data(using: .utf8),
let boundaryEndData = boundaryEnd.data(using: .utf8),
let dataEndData = dataEnd.data(using: .utf8)
else {
return nil
}

var requestBodyData = Data()
requestBodyData.append(contentsOf: boundaryStartData)
requestBodyData.append(contentsOf: dispositionData)
requestBodyData.append(contentsOf: contentTypeData)
requestBodyData.append(contentsOf: data)
requestBodyData.append(contentsOf: dataEndData)
requestBodyData.append(contentsOf: boundaryEndData)
return requestBodyData
}

private func randomBoundary() -> String {
#if os(Linux)
return "slackkit.boundary.\(Int(random()))\(Int(random()))"
Expand Down
39 changes: 30 additions & 9 deletions Sources/WebAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,13 @@ extension WebAPI {
}
}

public func channelsList(excludeArchived: Bool = false, success: ((_ channels: [[String: Any]]?) -> Void)?, failure: FailureClosure?) {
list(.channelsList, type:.channel, excludeArchived: excludeArchived, success: {(channels) in
public func channelsList(
excludeArchived: Bool = false,
excludeMembers: Bool = false,
success: ((_ channels: [[String: Any]]?) -> Void)?,
failure: FailureClosure?
) {
list(.channelsList, type:.channel, excludeArchived: excludeArchived, excludeMembers: excludeMembers, success: {(channels) in
success?(channels)
}) {(error) in
failure?(error)
Expand Down Expand Up @@ -473,8 +478,13 @@ extension WebAPI {
}
}

public func groupsList(excludeArchived: Bool = false, success: ((_ channels: [[String: Any]]?) -> Void)?, failure: FailureClosure?) {
list(.groupsList, type:.group, excludeArchived: excludeArchived, success: {(channels) in
public func groupsList(
excludeArchived: Bool = false,
excludeMembers: Bool = false,
success: ((_ channels: [[String: Any]]?) -> Void)?,
failure: FailureClosure?
) {
list(.groupsList, type:.group, excludeArchived: excludeArchived, excludeMembers: excludeMembers, success: {(channels) in
success?(channels)
}) {(error) in
failure?(error)
Expand Down Expand Up @@ -549,8 +559,13 @@ extension WebAPI {
}
}

public func imsList(excludeArchived: Bool = false, success: ((_ channels: [[String: Any]]?) -> Void)?, failure: FailureClosure?) {
list(.imList, type:.im, excludeArchived: excludeArchived, success: {(channels) in
public func imsList(
excludeArchived: Bool = false,
excludeMembers: Bool = false,
success: ((_ channels: [[String: Any]]?) -> Void)?,
failure: FailureClosure?
) {
list(.imList, type:.im, excludeArchived: excludeArchived, excludeMembers: excludeMembers, success: {(channels) in
success?(channels)
}) {(error) in
failure?(error)
Expand Down Expand Up @@ -610,8 +625,13 @@ extension WebAPI {
}
}

public func mpimsList(excludeArchived: Bool = false, success: ((_ channels: [[String: Any]]?) -> Void)?, failure: FailureClosure?) {
list(.mpimList, type:.group, excludeArchived: excludeArchived, success: {(channels) in
public func mpimsList(
excludeArchived: Bool = false,
excludeMembers: Bool = false,
success: ((_ channels: [[String: Any]]?) -> Void)?,
failure: FailureClosure?
) {
list(.mpimList, type:.group, excludeArchived: excludeArchived, excludeMembers: excludeMembers, success: {(channels) in
success?(channels)
}) {(error) in
failure?(error)
Expand Down Expand Up @@ -1096,10 +1116,11 @@ extension WebAPI {
_ endpoint: Endpoint,
type: ChannelType,
excludeArchived: Bool = false,
excludeMembers: Bool = false,
success: ((_ channels: [[String: Any]]?) -> Void)?,
failure: FailureClosure?
) {
let parameters: [String: Any] = ["token": token, "exclude_archived": excludeArchived]
let parameters: [String: Any] = ["token": token, "exclude_archived": excludeArchived, "exclude_members": excludeMembers]
networkInterface.request(endpoint, parameters: parameters, successClosure: {(response) in
success?(response[type.rawValue+"s"] as? [[String: Any]])
}) {(error) in
Expand Down

0 comments on commit 6e5eada

Please sign in to comment.