Skip to content

Commit

Permalink
Improved conventions, naming
Browse files Browse the repository at this point in the history
  • Loading branch information
bitwit committed Dec 12, 2020
1 parent f9d6403 commit b8ab666
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 97 deletions.
45 changes: 0 additions & 45 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -37,42 +37,6 @@
"version": "4.2.4"
}
},
{
"package": "fluent",
"repositoryURL": "https://github.com/vapor/fluent.git",
"state": {
"branch": null,
"revision": "e681c93df3201a2d8ceef15e8a9a0634578df233",
"version": "4.0.0"
}
},
{
"package": "fluent-kit",
"repositoryURL": "https://github.com/vapor/fluent-kit.git",
"state": {
"branch": null,
"revision": "31d96b547cc1f869f2885d932a8a9a7ae2103fc6",
"version": "1.10.0"
}
},
{
"package": "jwt",
"repositoryURL": "https://github.com/vapor/jwt.git",
"state": {
"branch": null,
"revision": "46eddb526fa70fd9d765a3bdffac73b2be487cfc",
"version": "4.0.0"
}
},
{
"package": "jwt-kit",
"repositoryURL": "https://github.com/vapor/jwt-kit.git",
"state": {
"branch": null,
"revision": "6055fe8439769aa3646a3c797d573ce7305849c8",
"version": "4.2.0"
}
},
{
"package": "Nimble",
"repositoryURL": "https://github.com/Quick/Nimble.git",
Expand Down Expand Up @@ -136,15 +100,6 @@
"version": "0.9.2"
}
},
{
"package": "sql-kit",
"repositoryURL": "https://github.com/vapor/sql-kit.git",
"state": {
"branch": null,
"revision": "ea9928b7f4a801b175a00b982034d9c54ecb6167",
"version": "3.7.0"
}
},
{
"package": "Stencil",
"repositoryURL": "https://github.com/stencilproject/Stencil.git",
Expand Down
8 changes: 2 additions & 6 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import PackageDescription

let package = Package(
name: "struct-vapor-endpoints",
name: "chill",
platforms: [
.macOS(.v10_15)
],
Expand All @@ -14,8 +14,6 @@ let package = Package(
dependencies: [
// 💧 A server-side Swift web framework.
.package(url: "https://github.com/vapor/vapor.git", from: "4.0.0"),
.package(url: "https://github.com/vapor/fluent.git", from: "4.0.0"),
.package(url: "https://github.com/vapor/jwt.git", from: "4.0.0"),

.package(url: "https://github.com/jpsim/SourceKitten.git", from: "0.3.0"),

Expand All @@ -30,9 +28,7 @@ let package = Package(
.target(
name: "APIRouting",
dependencies: [
.product(name: "Fluent", package: "fluent"),
.product(name: "Vapor", package: "vapor"),
.product(name: "JWT", package: "jwt")
.product(name: "Vapor", package: "vapor")
]
),

Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
import Foundation
import PromiseKit

public extension APIRequest where Self.ResponseType: Decodable {
func execute(with networking: Networking) -> Promise<ResponseType> {
public extension APIRequest where Self.ResponseBody: Decodable {
func execute(with networking: Networking) -> Promise<ResponseBody> {
return networking.call(self)
}
}

public extension APIRequest where Self.ResponseType == Void {
func execute(with networking: Networking) -> Promise<ResponseType> {
public extension APIRequest where Self.ResponseBody == Void {
func execute(with networking: Networking) -> Promise<ResponseBody> {
return networking.call(self)
}
}

public extension Array where Element: APIRequest, Element.ResponseType: Decodable {
func executeAll(with networking: Networking) -> Promise<[Element.ResponseType]> {
public extension Array where Element: APIRequest, Element.ResponseBody: Decodable {
func executeAll(with networking: Networking) -> Promise<[Element.ResponseBody]> {
return when(fulfilled: self.map(networking.call))
}
}

public extension Array where Element: APIRequest, Element.ResponseType == Void {
func executeAll(with networking: Networking) -> Promise<[Element.ResponseType]> {
public extension Array where Element: APIRequest, Element.ResponseBody == Void {
func executeAll(with networking: Networking) -> Promise<[Element.ResponseBody]> {
return when(fulfilled: self.map(networking.call))
}
}
12 changes: 7 additions & 5 deletions Sources/APIClientGenerator/Networking/APIRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,19 @@ public protocol AnyAPIRequest {
}

public protocol APIRequest: AnyAPIRequest {

associatedtype BodyType
associatedtype ResponseType

associatedtype Parameters
associatedtype Query
associatedtype Body
associatedtype ResponseBody

static var method: HTTPMethod { get }
static var requiresAuth: Bool { get }

var endpoint: String { get }
var additionalHeaders: [String: String] { get }

var body: BodyType { get }
var body: Body { get }

func getContentType() throws -> String?
func getBody() throws -> Data?
Expand All @@ -44,7 +46,7 @@ public extension APIRequest {
func getBody() throws -> Data? { nil }
}

public extension APIRequest where BodyType: Encodable {
public extension APIRequest where Body: Encodable {

func getContentType() -> String? { "application/json" }

Expand Down
8 changes: 4 additions & 4 deletions Sources/APIClientGenerator/Networking/Networking.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public final class Networking {

public func callAll<E: APIRequest>(
_ requests: [E]
) -> Promise<[E.ResponseType]> where E.ResponseType: Decodable {
) -> Promise<[E.ResponseBody]> where E.ResponseBody: Decodable {

guard let firstRequest = requests.first else {
return Promise(error: NetworkingError(type: .emptyRequestArray))
Expand All @@ -61,10 +61,10 @@ public final class Networking {

public func call<E: APIRequest>(
_ request: E
) -> Promise<E.ResponseType> where E.ResponseType: Decodable {
) -> Promise<E.ResponseBody> where E.ResponseBody: Decodable {

return confirmAuthValidIfNeededOrDie(request)
.then { _ -> Promise<E.ResponseType> in
.then { _ -> Promise<E.ResponseBody> in
let urlRequest: URLRequest
do {
urlRequest = try self.buildUrlRequest(request)
Expand All @@ -73,7 +73,7 @@ public final class Networking {
}

return Promise { (resolver) in
let task = self.urlSession.dataTask(with: urlRequest, completionHandler: self.handleDecodableResponse(completion: { (result: Swift.Result<E.ResponseType, NetworkingError>) in
let task = self.urlSession.dataTask(with: urlRequest, completionHandler: self.handleDecodableResponse(completion: { (result: Swift.Result<E.ResponseBody, NetworkingError>) in
switch result {
case .success(let data):
resolver.fulfill(data)
Expand Down
19 changes: 16 additions & 3 deletions Sources/APIClientGenerator/utilities/templates/endpoint.stencil
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,21 @@ import Foundation

public struct {{ endpoint.name }}: APIRequest {

{% if endpoint.parameters.count == 0 %}
public typealias Parameters = Void
{% endif %}

{% if endpoint.query.count == 0 %}
public typealias Query = Void
{% endif %}

{% if endpoint.bodyType != nil %}
public typealias BodyType = {{ endpoint.bodyType }}
public typealias Body = {{ endpoint.bodyType }}
{% else %}
public typealias BodyType = Void
public typealias Body = Void
{% endif %}
public typealias ResponseType = {{ endpoint.responseType }}

public typealias ResponseBody = {{ endpoint.responseType }}

{% if endpoint.parameters.count > 0 %}
public struct Parameters {
Expand Down Expand Up @@ -72,10 +81,14 @@ public struct {{ endpoint.name }}: APIRequest {

{% if endpoint.parameters.count > 0 %}
public var parameters: Parameters
{% else %}
public var parameters: Parameters = ()
{% endif %}

{% if endpoint.query.count > 0 %}
public var query: Query
{% else %}
public var query: Query = ()
{% endif %}

{% if endpoint.bodyType != nil %}
Expand Down
30 changes: 11 additions & 19 deletions Sources/APIRouting/APIRoutingEndpoint.swift
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
//
// APIEndpoint.swift
//
//
// Created by Kyle Newsome on 2020-10-23.
//

import Foundation
import Vapor
import FluentKit

public enum APIRoutingHTTPMethod: String {
case get
Expand All @@ -27,18 +19,18 @@ public protocol APIRoutingEndpoint {
associatedtype Parameters
associatedtype Query
associatedtype Body
associatedtype Response: ResponseEncodable
associatedtype ResponseBody: ResponseEncodable

static var method: APIRoutingHTTPMethod { get }
static var path: String { get }

static func buildAndRun(from request: Request) throws -> EventLoopFuture<Response>
static func buildAndRun(from request: Request) throws -> EventLoopFuture<ResponseBody>
static func run(
context: Context,
parameters: Parameters,
query: Query,
body: Body
) throws -> EventLoopFuture<Response>
) throws -> EventLoopFuture<ResponseBody>
}


Expand All @@ -64,7 +56,7 @@ public extension APIRoutingEndpoint {

// All
public extension APIRoutingEndpoint where Parameters: Decodable, Query: Decodable, Body: Decodable {
static func buildAndRun(from request: Request) throws -> EventLoopFuture<Response> {
static func buildAndRun(from request: Request) throws -> EventLoopFuture<ResponseBody> {
let requestParameters = try request.parameters.decode(Parameters.self)
let requestQuery = try request.query.decode(Query.self)
let requestBody = try request.content.decode(Body.self)
Expand All @@ -79,7 +71,7 @@ public extension APIRoutingEndpoint where Parameters: Decodable, Query: Decodabl

// None
public extension APIRoutingEndpoint where Parameters == Void, Query == Void, Body == Void {
static func buildAndRun(from request: Request) throws -> EventLoopFuture<Response> {
static func buildAndRun(from request: Request) throws -> EventLoopFuture<ResponseBody> {
return try self.run(
context: Context.createFrom(request: request),
parameters: (),
Expand All @@ -91,7 +83,7 @@ public extension APIRoutingEndpoint where Parameters == Void, Query == Void, Bod

// Param alone
public extension APIRoutingEndpoint where Parameters: Decodable, Query == Void, Body == Void {
static func buildAndRun(from request: Request) throws -> EventLoopFuture<Response> {
static func buildAndRun(from request: Request) throws -> EventLoopFuture<ResponseBody> {
let requestParameters = try request.parameters.decode(Parameters.self)
return try self.run(
context: Context.createFrom(request: request),
Expand All @@ -104,7 +96,7 @@ public extension APIRoutingEndpoint where Parameters: Decodable, Query == Void,

// Params & Query
public extension APIRoutingEndpoint where Parameters: Decodable, Query: Decodable, Body == Void {
static func buildAndRun(from request: Request) throws -> EventLoopFuture<Response> {
static func buildAndRun(from request: Request) throws -> EventLoopFuture<ResponseBody> {
let requestParameters = try request.parameters.decode(Parameters.self)
let requestQuery = try request.query.decode(Query.self)
return try self.run(
Expand All @@ -118,7 +110,7 @@ public extension APIRoutingEndpoint where Parameters: Decodable, Query: Decodabl

// Param & Body
public extension APIRoutingEndpoint where Parameters: Decodable, Query == Void, Body: Decodable {
static func buildAndRun(from request: Request) throws -> EventLoopFuture<Response> {
static func buildAndRun(from request: Request) throws -> EventLoopFuture<ResponseBody> {
let requestParameters = try request.parameters.decode(Parameters.self)
let requestBody = try request.content.decode(Body.self)
return try self.run(
Expand All @@ -132,7 +124,7 @@ public extension APIRoutingEndpoint where Parameters: Decodable, Query == Void,

// Query only
public extension APIRoutingEndpoint where Parameters == Void, Query: Decodable, Body == Void {
static func buildAndRun(from request: Request) throws -> EventLoopFuture<Response> {
static func buildAndRun(from request: Request) throws -> EventLoopFuture<ResponseBody> {
let requestQuery = try request.query.decode(Query.self)
return try self.run(
context: Context.createFrom(request: request),
Expand All @@ -145,7 +137,7 @@ public extension APIRoutingEndpoint where Parameters == Void, Query: Decodable,

// Query & Body
public extension APIRoutingEndpoint where Parameters == Void, Query: Decodable, Body: Decodable {
static func buildAndRun(from request: Request) throws -> EventLoopFuture<Response> {
static func buildAndRun(from request: Request) throws -> EventLoopFuture<ResponseBody> {
let requestQuery = try request.query.decode(Query.self)
let requestBody = try request.content.decode(Body.self)
return try self.run(
Expand All @@ -159,7 +151,7 @@ public extension APIRoutingEndpoint where Parameters == Void, Query: Decodable,

// Body alone
public extension APIRoutingEndpoint where Parameters == Void, Query == Void, Body: Decodable {
static func buildAndRun(from request: Request) throws -> EventLoopFuture<Response> {
static func buildAndRun(from request: Request) throws -> EventLoopFuture<ResponseBody> {
let requestBody = try request.content.decode(Body.self)
return try self.run(
context: Context.createFrom(request: request),
Expand Down
7 changes: 0 additions & 7 deletions Sources/APIRouting/APIRoutingParameters.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
//
// APIRoutingParameters.swift
//
//
// Created by Kyle Newsome on 2020-10-28.
//

import Foundation
import Vapor

Expand Down

0 comments on commit b8ab666

Please sign in to comment.