Skip to content

Commit

Permalink
💥 run with generator 7.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
ras0q committed Oct 21, 2023
1 parent 74aefa3 commit e3ec8e1
Show file tree
Hide file tree
Showing 78 changed files with 1,242 additions and 4,851 deletions.
21 changes: 8 additions & 13 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Created by https://www.toptal.com/developers/gitignore/api/swift,xcode
# Edit at https://www.toptal.com/developers/gitignore?templates=swift,xcode
# Created by https://www.toptal.com/developers/gitignore/api/xcode,swift
# Edit at https://www.toptal.com/developers/gitignore?templates=xcode,swift

### Swift ###
# Xcode
Expand Down Expand Up @@ -64,9 +64,9 @@ playground.xcworkspace

Carthage/Build/

# Add this lines if you are using Accio dependency management (Deprecated since Xcode 12)
# Dependencies/
# .accio/
# Accio dependency management
Dependencies/
.accio/

# fastlane
# It is recommended to not store the screenshots in the git repo.
Expand All @@ -86,22 +86,17 @@ fastlane/test_output
iOSInjectionProject/

### Xcode ###
# Xcode
# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore




## Gcc Patch
/*.gcno
## Xcode 8 and earlier

### Xcode Patch ###
*.xcodeproj/*
!*.xcodeproj/project.pbxproj
!*.xcodeproj/xcshareddata/
!*.xcworkspace/contents.xcworkspacedata
/*.gcno
**/xcshareddata/WorkspaceSettings.xcsettings

# End of https://www.toptal.com/developers/gitignore/api/swift,xcode
# End of https://www.toptal.com/developers/gitignore/api/xcode,swift
Packages/
openapi-generator-cli.jar
2 changes: 2 additions & 0 deletions .openapi-generator/FILES
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.gitignore
.swiftformat
Cartfile
Package.swift
README.md
Expand Down Expand Up @@ -154,6 +155,7 @@ Sources/Traq/Models/Webhook.swift
Sources/Traq/OpenISO8601DateFormatter.swift
Sources/Traq/SynchronizedDictionary.swift
Sources/Traq/URLSessionImplementations.swift
Sources/Traq/Validation.swift
Traq.podspec
docs/ActiveOAuth2Token.md
docs/ActivityAPI.md
Expand Down
2 changes: 1 addition & 1 deletion .openapi-generator/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7.0.1
7.0.1
46 changes: 45 additions & 1 deletion .swiftformat
Original file line number Diff line number Diff line change
@@ -1 +1,45 @@
--disable enumNamespaces
# This file is auto-generated by OpenAPI Generator: https://openapi-generator.tech/
#
# For rules on SwiftFormat, please refer to https://github.com/nicklockwood/SwiftFormat/blob/master/Rules.md
#
# file options

# uncomment below to exclude files, folders
#--exclude path/to/test1.swift,Snapshots,Build

# format options

--allman false
--binarygrouping 4,8
--commas always
--comments indent
--decimalgrouping 3,6
--elseposition same-line
--empty void
--exponentcase lowercase
--exponentgrouping disabled
--fractiongrouping disabled
--header ignore
--hexgrouping 4,8
--hexliteralcase uppercase
--ifdef indent
--indent 4
--indentcase false
--importgrouping testable-bottom
--linebreaks lf
--maxwidth none
--octalgrouping 4,8
--operatorfunc spaced
--patternlet hoist
--ranges spaced
--self remove
--semicolons inline
--stripunusedargs always
--swiftversion 5.4
--trimwhitespace always
--wraparguments preserve
--wrapcollections preserve

# rules

--enable isEmpty
2 changes: 1 addition & 1 deletion Cartfile
Original file line number Diff line number Diff line change
@@ -1 +1 @@
github "Flight-School/AnyCodable" ~> 0.6.1
github "Flight-School/AnyCodable" ~> 0.6
10 changes: 5 additions & 5 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import PackageDescription
let package = Package(
name: "Traq",
platforms: [
.iOS(.v9),
.macOS(.v10_11),
.tvOS(.v9),
.watchOS(.v3),
.iOS(.v11),
.macOS(.v10_13),
.tvOS(.v11),
.watchOS(.v4),
],
products: [
// Products define the executables and libraries produced by a package, and make them visible to other packages.
Expand All @@ -19,7 +19,7 @@ let package = Package(
],
dependencies: [
// Dependencies declare other packages that this package depends on.
.package(url: "https://github.com/Flight-School/AnyCodable", from: "0.6.1"),
.package(url: "https://github.com/Flight-School/AnyCodable", .upToNextMajor(from: "0.6.1")),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
Expand Down
23 changes: 14 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -352,10 +352,20 @@ Class | Method | HTTP request | Description
- [Webhook](docs/Webhook.md)


<a id="documentation-for-authorization"></a>
## Documentation For Authorization


## OAuth2
Authentication schemes defined for the API:
<a id="cookieAuth"></a>
### cookieAuth

- **Type**: API key
- **API key parameter name**: r_session
- **Location**:

<a id="OAuth2"></a>
### OAuth2

- **Type**: OAuth
- **Flow**: accessCode
Expand All @@ -365,15 +375,10 @@ Class | Method | HTTP request | Description
- **write**: 書き込みスコープ
- **manage_bot**: bot関連読み書きスコープ

## bearerAuth
<a id="bearerAuth"></a>
### bearerAuth

- **Type**: HTTP basic authentication

## cookieAuth

- **Type**: API key
- **API key parameter name**: r_session
- **Location**:
- **Type**: HTTP Bearer Token authentication


## Author
Expand Down
64 changes: 48 additions & 16 deletions Sources/Traq/APIHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import Foundation

public struct APIHelper {
public enum APIHelper {
public static func rejectNil(_ source: [String: Any?]) -> [String: Any]? {
let destination = source.reduce(into: [String: Any]()) { result, item in
if let value = item.value {
Expand All @@ -21,16 +21,13 @@ public struct APIHelper {
}

public static func rejectNilHeaders(_ source: [String: Any?]) -> [String: String] {
return source.reduce(into: [String: String]()) { result, item in
source.reduce(into: [String: String]()) { result, item in
if let collection = item.value as? [Any?] {
result[item.key] = collection
.compactMap { value in
guard let value = value else { return nil }
return "\(value)"
}
.compactMap { value in convertAnyToString(value) }
.joined(separator: ",")
} else if let value: Any = item.value {
result[item.key] = "\(value)"
result[item.key] = convertAnyToString(value)
}
}
}
Expand All @@ -50,31 +47,66 @@ public struct APIHelper {
}
}

public static func convertAnyToString(_ value: Any?) -> String? {
guard let value = value else { return nil }
if let value = value as? any RawRepresentable {
return "\(value.rawValue)"
} else {
return "\(value)"
}
}

public static func mapValueToPathItem(_ source: Any) -> Any {
if let collection = source as? [Any?] {
return collection
.compactMap { value in
guard let value = value else { return nil }
return "\(value)"
}
.compactMap { value in convertAnyToString(value) }
.joined(separator: ",")
}
return source
}

/// maps all values from source to query parameters
///
/// explode attribute is respected: collection values might be either joined or split up into separate key value pairs
public static func mapValuesToQueryItems(_ source: [String: (wrappedValue: Any?, isExplode: Bool)]) -> [URLQueryItem]? {
let destination = source.filter { $0.value.wrappedValue != nil }.reduce(into: [URLQueryItem]()) { result, item in
if let collection = item.value.wrappedValue as? [Any?] {
let collectionValues: [String] = collection.compactMap { value in convertAnyToString(value) }

if !item.value.isExplode {
result.append(URLQueryItem(name: item.key, value: collectionValues.joined(separator: ",")))
} else {
collectionValues
.forEach { value in
result.append(URLQueryItem(name: item.key, value: value))
}
}

} else if let value = item.value.wrappedValue {
result.append(URLQueryItem(name: item.key, value: convertAnyToString(value)))
}
}

if destination.isEmpty {
return nil
}
return destination
}

/// maps all values from source to query parameters
///
/// collection values are always exploded
public static func mapValuesToQueryItems(_ source: [String: Any?]) -> [URLQueryItem]? {
let destination = source.filter { $0.value != nil }.reduce(into: [URLQueryItem]()) { result, item in
if let collection = item.value as? [Any?] {
collection
.compactMap { value in
guard let value = value else { return nil }
return "\(value)"
}
.compactMap { value in convertAnyToString(value) }
.forEach { value in
result.append(URLQueryItem(name: item.key, value: value))
}

} else if let value = item.value {
result.append(URLQueryItem(name: item.key, value: "\(value)"))
result.append(URLQueryItem(name: item.key, value: convertAnyToString(value)))
}
}

Expand Down
36 changes: 32 additions & 4 deletions Sources/Traq/APIs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
//

import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif
open class TraqAPI {
public static var basePath = "https://q.trap.jp/api/v3"
public static var customHeaders: [String: String] = [:]
Expand All @@ -20,17 +23,17 @@ open class RequestBuilder<T> {
public let method: String
public let URLString: String
public let requestTask: RequestTask = .init()
public let requiresAuthentication: Bool

/// Optional block to obtain a reference to the request's progress instance when available.
/// With the URLSession http client the request's progress only works on iOS 11.0, macOS 10.13, macCatalyst 13.0, tvOS 11.0, watchOS 4.0.
/// If you need to get the request's progress in older OS versions, please use Alamofire http client.
public var onProgressReady: ((Progress) -> Void)?

public required init(method: String, URLString: String, parameters: [String: Any]?, headers: [String: String] = [:]) {
public required init(method: String, URLString: String, parameters: [String: Any]?, headers: [String: String] = [:], requiresAuthentication: Bool) {
self.method = method
self.URLString = URLString
self.parameters = parameters
self.headers = headers
self.requiresAuthentication = requiresAuthentication

addHeaders(TraqAPI.customHeaders)
}
Expand All @@ -43,7 +46,32 @@ open class RequestBuilder<T> {

@discardableResult
open func execute(_: DispatchQueue = TraqAPI.apiResponseQueue, _: @escaping (_ result: Swift.Result<Response<T>, ErrorResponse>) -> Void) -> RequestTask {
return requestTask
requestTask
}

@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
@discardableResult
open func execute() async throws -> Response<T> {
try await withTaskCancellationHandler {
try Task.checkCancellation()
return try await withCheckedThrowingContinuation { continuation in
guard !Task.isCancelled else {
continuation.resume(throwing: CancellationError())
return
}

self.execute { result in
switch result {
case let .success(response):
continuation.resume(returning: response)
case let .failure(error):
continuation.resume(throwing: error)
}
}
}
} onCancel: {
self.requestTask.cancel()
}
}

public func addHeader(name: String, value: String) -> Self {
Expand Down
Loading

0 comments on commit e3ec8e1

Please sign in to comment.