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

WebAPI: Passes access token as Bearer Token #200

Merged
merged 2 commits into from
Oct 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
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