Skip to content
This repository has been archived by the owner on Dec 2, 2022. It is now read-only.

Commit

Permalink
WebAPI: Passes access token as Bearer Token (#200)
Browse files Browse the repository at this point in the history
* Added passing access token as a Bearer token.

* Removed superfluous code
  • Loading branch information
phynics authored Oct 5, 2021
1 parent a19f6b3 commit e178c37
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 127 deletions.
2 changes: 1 addition & 1 deletion SKWebAPI/Sources/Endpoint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public enum Endpoint: String {
case mpimList = "mpim.list"
case mpimMark = "mpim.mark"
case mpimOpen = "mpim.open"
case oauthAccess = "oauth.access"
case oauthAccess = "oauth.v2.access"
case pinsList = "pins.list"
case pinsAdd = "pins.add"
case pinsRemove = "pins.remove"
Expand Down
18 changes: 15 additions & 3 deletions SKWebAPI/Sources/NetworkInterface.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,23 @@ public struct NetworkInterface {

internal func request(
_ endpoint: Endpoint,
accessToken: String,
parameters: [String: Any?],
successClosure: @escaping ([String: Any]) -> Void,
errorClosure: @escaping (SlackError) -> Void
) {
guard !accessToken.isEmpty else {
errorClosure(.invalidAuth)
return
}

guard let url = requestURL(for: endpoint, parameters: parameters) else {
errorClosure(SlackError.clientNetworkError)
return
}
let request = URLRequest(url: url)

var request = URLRequest(url: url)
request.setValue("Bearer \(accessToken)", forHTTPHeaderField: "Authorization")

session.dataTask(with: request) {(data, response, publicError) in
do {
Expand Down Expand Up @@ -89,6 +97,7 @@ public struct NetworkInterface {

internal func customRequest(
_ url: String,
token: String,
data: Data,
success: @escaping (Bool) -> Void,
errorClosure: @escaping (SlackError) -> Void
Expand All @@ -99,11 +108,12 @@ public struct NetworkInterface {
}
var request = URLRequest(url:url)
request.httpMethod = "POST"
let contentType = "application/json"
let contentType = "application/json; charset: utf-8"
request.setValue(contentType, forHTTPHeaderField: "Content-Type")
request.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
request.httpBody = data

session.dataTask(with: request) {(_, _, publicError) in
session.dataTask(with: request) {(data, response, publicError) in
if publicError == nil {
success(true)
} else {
Expand All @@ -114,6 +124,7 @@ public struct NetworkInterface {

internal func uploadRequest(
data: Data,
accessToken: String,
parameters: [String: Any?],
successClosure: @escaping ([String: Any]) -> Void, errorClosure: @escaping (SlackError) -> Void
) {
Expand All @@ -130,6 +141,7 @@ public struct NetworkInterface {
let boundaryConstant = randomBoundary()
let contentType = "multipart/form-data; boundary=" + boundaryConstant
request.setValue(contentType, forHTTPHeaderField: "Content-Type")
request.setValue("Bearer \(accessToken)", forHTTPHeaderField: "Authorization")
request.httpBody = requestBodyData(data: data, boundaryConstant: boundaryConstant, filename: filename, filetype: filetype)

session.dataTask(with: request) {(data, response, publicError) in
Expand Down
Loading

0 comments on commit e178c37

Please sign in to comment.