Skip to content

Commit 5db4d66

Browse files
Add Content-Type x-www-form-urlencoded (#29)
* add support to content type x-www-form-urlencoded * add verbose mode * fix API generator, set parameters * fix snapshots * add snapshot testing for new content type - www-form-urlencoded * improve template to make parameteres for sending operation * fix: update to bow 0.8 templates
1 parent ce79651 commit 5db4d66

File tree

43 files changed

+498
-206
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+498
-206
lines changed

BowOpenAPI.xcodeproj/project.pbxproj

Lines changed: 130 additions & 99 deletions
Large diffs are not rendered by default.

BowOpenAPI/BowOpenAPICommand.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,7 @@ struct BowOpenAPICommand: ParsableCommand {
1414

1515
@Option(help: ArgumentHelp("Path where the Swift package containing the network client will be generated. ex. `/home`.", valueName: "output path"))
1616
var output: String
17+
18+
@ArgumentParser.Flag (help: "Run in verbose mode")
19+
var verbose: Bool
1720
}

BowOpenAPI/Console.swift

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright © 2019 The Bow Authors.
2+
3+
import Foundation
4+
import Bow
5+
import BowEffects
6+
import OpenApiGenerator
7+
8+
public extension Kleisli where F == IOPartial<APIClientError> {
9+
10+
func reportStatus(
11+
_ failure: @escaping (APIClientError) -> String,
12+
_ success: @escaping (A) -> String)
13+
-> EnvIO<D, APIClientError, A> {
14+
15+
self.foldM(
16+
{ e in
17+
ConsoleIO.print("☠️ \(failure(e))").env().followedBy(.raiseError(e))^
18+
},
19+
{ a in
20+
ConsoleIO.print("🙌 \(success(a))").env().followedBy(.pure(a))^
21+
}
22+
)^
23+
}
24+
}
25+
26+
public extension Kleisli where F == IOPartial<APIClientError> {
27+
func finish() -> EnvIO<D, APIClientError, Void> {
28+
self.foldM(
29+
{ _ in
30+
EnvIO.invoke { _ in Darwin.exit(-1) }
31+
},
32+
{ _ in
33+
EnvIO.invoke { _ in Darwin.exit(0) }
34+
})
35+
}
36+
}

BowOpenAPI/Utils/String+File.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright © 2020 The Bow Authors.
2+
3+
import Foundation
4+
5+
extension String {
6+
var contentOfFile: String {
7+
let fileURL = URL(fileURLWithPath: expandingTildeInPath)
8+
let content = try? String(contentsOf: fileURL)
9+
return content ?? ""
10+
}
11+
}

BowOpenAPI/main.swift

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,37 @@
22

33
import Foundation
44
import OpenApiGenerator
5+
import Bow
6+
import BowEffects
57

68
let prodEnv = Environment(logPath: "/tmp/bow-openapi.log",
79
fileSystem: MacFileSystem(),
810
generator: SwaggerClientGenerator())
911

1012
extension BowOpenAPICommand {
1113
func run() throws {
12-
guard FileManager.default.fileExists(atPath: schema) else {
13-
Console.exit(failure: "received invalid schema path")
14-
}
15-
16-
APIClient.bow(moduleName: name, scheme: schema, output: output)
17-
.provide(prodEnv)
18-
.unsafeRunSyncEither()
19-
.mapLeft { apiError in "could not generate api client for schema '\(schema)'\ninformation: \(apiError)" }
20-
.fold(Console.exit(failure:), Console.exit(success:))
14+
try run().provide(prodEnv).unsafeRunSync()
15+
}
16+
17+
func run() -> EnvIO<Environment, APIClientError, Void> {
18+
APIClient.bow(moduleName: name, schema: schema, output: output)
19+
.reportStatus(
20+
{ apiError in
21+
"""
22+
Could not generate API client:
23+
• SCHEMA '\(self.schema)'
24+
25+
\(apiError)
26+
27+
\(self.verbose ?
28+
" • LOG \n\n\(prodEnv.logPath.contentOfFile)\n" :
29+
" • LOG: \(prodEnv.logPath)")
30+
"""
31+
},
32+
{ success in
33+
"\(success)"
34+
}
35+
).finish()
2136
}
2237
}
2338

OpenApiGenerator/APIClient.swift

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@ import Bow
55
import BowEffects
66

77
public enum APIClient {
8-
public static func bow(moduleName: String, scheme: String, output: String) -> EnvIO<Environment, APIClientError, String> {
8+
public static func bow(moduleName: String, schema: String, output: String) -> EnvIO<Environment, APIClientError, String> {
99
EnvIO { env in
1010
let template = IO<APIClientError, String>.var()
11+
let schema = schema.expandingTildeInPath
12+
let output = output.expandingTildeInPath
13+
1114
return binding(
15+
|<-validate(schema: schema),
1216
template <- getTemplatePath(),
13-
|<-bow(moduleName: moduleName, scheme: scheme, output: output, templatePath: template.get).provide(env),
17+
|<-bow(moduleName: moduleName, scheme: schema, output: output, templatePath: template.get).provide(env),
1418
yield: "RENDER SUCCEEDED")^
1519
}
1620
}
@@ -32,6 +36,15 @@ public enum APIClient {
3236
}
3337

3438
// MARK: attributes
39+
private static func validate(schema: String) -> IO<APIClientError, Void> {
40+
IO.invoke {
41+
guard FileManager.default.fileExists(atPath: schema) else {
42+
throw APIClientError(operation: "validate(schema:output:)",
43+
error: GeneratorError.invalidParameters)
44+
}
45+
}
46+
}
47+
3548
private static func getTemplatePath() -> IO<APIClientError, String> {
3649
let libPath = "/usr/local/lib"
3750
let templateSource1 = Bundle(path: "bow/openapi/templates").toOption()
@@ -54,7 +67,7 @@ public enum APIClient {
5467
.followedBy(fileSystem.createDirectory(atPath: parentPath))^
5568
.followedBy(fileSystem.createDirectory(atPath: outputPath.sources))^
5669
.followedBy(fileSystem.createDirectory(atPath: outputPath.tests))^
57-
.mapLeft { _ in APIClientError(operation: "createStructure(atPath:)", error: GeneratorError.structure) }
70+
.mapError { _ in APIClientError(operation: "createStructure(atPath:)", error: GeneratorError.structure) }
5871
}
5972
}
6073

OpenApiGenerator/Algebra/FileSystem.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public extension FileSystem {
4040
func moveFile(from origin: String, to destination: String) -> IO<FileSystemError, Void> {
4141
copy(itemPath: origin, toPath: destination)
4242
.followedBy(removeFiles(origin))^
43-
.mapLeft { _ in .move(from: origin, to: destination) }
43+
.mapError { _ in .move(from: origin, to: destination) }
4444
}
4545

4646
func moveFiles(in input: String, to output: String) -> IO<FileSystemError, ()> {
@@ -51,7 +51,7 @@ public extension FileSystem {
5151
|<-self.copy(items: items.get, from: input, to: output),
5252
|<-self.removeDirectory(input),
5353
yield: ()
54-
)^.mapLeft { _ in .move(from: input, to: output) }
54+
)^.mapError { _ in .move(from: input, to: output) }
5555
}
5656

5757
func rename(_ newName: String, itemAt: String) -> IO<FileSystemError, ()> {

OpenApiGenerator/Environment.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
import Foundation
44

55
public struct Environment {
6-
let logPath: String
7-
let fileSystem: FileSystem
8-
let generator: ClientGenerator
6+
public let logPath: String
7+
public let fileSystem: FileSystem
8+
public let generator: ClientGenerator
99

1010
public init(logPath: String, fileSystem: FileSystem, generator: ClientGenerator) {
1111
self.logPath = logPath

OpenApiGenerator/Error/GeneratorError.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import Foundation
44

55
/// General information about the errors in the generator
66
enum GeneratorError: Error {
7+
case invalidParameters
78
case templateNotFound
89
case structure
910
case generator
@@ -12,6 +13,8 @@ enum GeneratorError: Error {
1213
extension GeneratorError: CustomStringConvertible {
1314
var description: String {
1415
switch self {
16+
case .invalidParameters:
17+
return "received invalid parameters"
1518
case .templateNotFound:
1619
return "templates for generating Bow client have not been found"
1720
case .structure:

0 commit comments

Comments
 (0)