Skip to content

Commit

Permalink
Small refactor to make working with service builders easier/clearer
Browse files Browse the repository at this point in the history
  • Loading branch information
atljeremy committed Aug 9, 2019
1 parent 4660332 commit df289e9
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 24 deletions.
18 changes: 12 additions & 6 deletions HTTPService.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

/* Begin PBXBuildFile section */
1971904022F6547700B3BDC5 /* HTTPServiceError.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1971903F22F6547700B3BDC5 /* HTTPServiceError.swift */; };
1971904322F654B300B3BDC5 /* HTTPServiceBuilder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1971904222F654B300B3BDC5 /* HTTPServiceBuilder.swift */; };
1971904322F654B300B3BDC5 /* HTTPServiceBuildable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1971904222F654B300B3BDC5 /* HTTPServiceBuildable.swift */; };
1971904622F654E900B3BDC5 /* HTTPAuthorization.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1971904522F654E900B3BDC5 /* HTTPAuthorization.swift */; };
1971904922F6555900B3BDC5 /* HTTPRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1971904822F6555900B3BDC5 /* HTTPRequest.swift */; };
1971904B22F655EE00B3BDC5 /* ServiceBuilder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1971904A22F655EE00B3BDC5 /* ServiceBuilder.swift */; };
Expand All @@ -17,7 +17,7 @@
1988FC7A22FA0B34000F70B3 /* HTTPRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1971904822F6555900B3BDC5 /* HTTPRequest.swift */; };
1988FC7B22FA0B34000F70B3 /* HTTPService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3F43768B1AD1A19300FFC40C /* HTTPService.swift */; };
1988FC7C22FA0B34000F70B3 /* HTTPAuthorization.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1971904522F654E900B3BDC5 /* HTTPAuthorization.swift */; };
1988FC7D22FA0B34000F70B3 /* HTTPServiceBuilder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1971904222F654B300B3BDC5 /* HTTPServiceBuilder.swift */; };
1988FC7D22FA0B34000F70B3 /* HTTPServiceBuildable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1971904222F654B300B3BDC5 /* HTTPServiceBuildable.swift */; };
1988FC7E22FA0B34000F70B3 /* ServiceBuilder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1971904A22F655EE00B3BDC5 /* ServiceBuilder.swift */; };
1988FC7F22FA0B34000F70B3 /* HTTPServiceError.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1971903F22F6547700B3BDC5 /* HTTPServiceError.swift */; };
1988FC8022FA0B5F000F70B3 /* PullRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3F98C32F1B0AAD5600AC23D4 /* PullRequest.swift */; };
Expand All @@ -29,6 +29,8 @@
1988FC9622FBA080000F70B3 /* GitHubGetPullRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1988FC9422FBA080000F70B3 /* GitHubGetPullRequest.swift */; };
1988FC9822FBA227000F70B3 /* HTTPServiceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1988FC9722FBA227000F70B3 /* HTTPServiceTests.swift */; };
1988FC9922FBA229000F70B3 /* HTTPServiceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1988FC9722FBA227000F70B3 /* HTTPServiceTests.swift */; };
1988FC9B22FCFD82000F70B3 /* ServiceBuildable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1988FC9A22FCFD82000F70B3 /* ServiceBuildable.swift */; };
1988FC9C22FCFD82000F70B3 /* ServiceBuildable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1988FC9A22FCFD82000F70B3 /* ServiceBuildable.swift */; };
3F43766A1AD1A14100FFC40C /* HTTPService.h in Headers */ = {isa = PBXBuildFile; fileRef = 3F4376691AD1A14100FFC40C /* HTTPService.h */; settings = {ATTRIBUTES = (Public, ); }; };
3F4376701AD1A14100FFC40C /* HTTPService.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3F4376641AD1A14100FFC40C /* HTTPService.framework */; };
3F43769D1AD1A19300FFC40C /* HTTPService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3F43768B1AD1A19300FFC40C /* HTTPService.swift */; };
Expand All @@ -54,7 +56,7 @@

/* Begin PBXFileReference section */
1971903F22F6547700B3BDC5 /* HTTPServiceError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HTTPServiceError.swift; sourceTree = "<group>"; };
1971904222F654B300B3BDC5 /* HTTPServiceBuilder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HTTPServiceBuilder.swift; sourceTree = "<group>"; };
1971904222F654B300B3BDC5 /* HTTPServiceBuildable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HTTPServiceBuildable.swift; sourceTree = "<group>"; };
1971904522F654E900B3BDC5 /* HTTPAuthorization.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HTTPAuthorization.swift; sourceTree = "<group>"; };
1971904822F6555900B3BDC5 /* HTTPRequest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HTTPRequest.swift; sourceTree = "<group>"; };
1971904A22F655EE00B3BDC5 /* ServiceBuilder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ServiceBuilder.swift; sourceTree = "<group>"; };
Expand All @@ -67,6 +69,7 @@
1988FC9022FB83CE000F70B3 /* GitHubService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GitHubService.swift; sourceTree = "<group>"; };
1988FC9422FBA080000F70B3 /* GitHubGetPullRequest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GitHubGetPullRequest.swift; sourceTree = "<group>"; };
1988FC9722FBA227000F70B3 /* HTTPServiceTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HTTPServiceTests.swift; sourceTree = "<group>"; };
1988FC9A22FCFD82000F70B3 /* ServiceBuildable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ServiceBuildable.swift; sourceTree = "<group>"; };
3F4376641AD1A14100FFC40C /* HTTPService.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = HTTPService.framework; sourceTree = BUILT_PRODUCTS_DIR; };
3F4376681AD1A14100FFC40C /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
3F4376691AD1A14100FFC40C /* HTTPService.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = HTTPService.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -122,8 +125,9 @@
1971904122F654A400B3BDC5 /* Builders */ = {
isa = PBXGroup;
children = (
1971904222F654B300B3BDC5 /* HTTPServiceBuilder.swift */,
1971904222F654B300B3BDC5 /* HTTPServiceBuildable.swift */,
1971904A22F655EE00B3BDC5 /* ServiceBuilder.swift */,
1988FC9A22FCFD82000F70B3 /* ServiceBuildable.swift */,
);
name = Builders;
sourceTree = "<group>";
Expand Down Expand Up @@ -431,11 +435,12 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
1988FC9C22FCFD82000F70B3 /* ServiceBuildable.swift in Sources */,
1988FC7A22FA0B34000F70B3 /* HTTPRequest.swift in Sources */,
1988FC7C22FA0B34000F70B3 /* HTTPAuthorization.swift in Sources */,
1988FC7F22FA0B34000F70B3 /* HTTPServiceError.swift in Sources */,
1988FC7B22FA0B34000F70B3 /* HTTPService.swift in Sources */,
1988FC7D22FA0B34000F70B3 /* HTTPServiceBuilder.swift in Sources */,
1988FC7D22FA0B34000F70B3 /* HTTPServiceBuildable.swift in Sources */,
1988FC7E22FA0B34000F70B3 /* ServiceBuilder.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand All @@ -456,8 +461,9 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
1988FC9B22FCFD82000F70B3 /* ServiceBuildable.swift in Sources */,
1971904022F6547700B3BDC5 /* HTTPServiceError.swift in Sources */,
1971904322F654B300B3BDC5 /* HTTPServiceBuilder.swift in Sources */,
1971904322F654B300B3BDC5 /* HTTPServiceBuildable.swift in Sources */,
1971904922F6555900B3BDC5 /* HTTPRequest.swift in Sources */,
3F43769D1AD1A19300FFC40C /* HTTPService.swift in Sources */,
1971904622F654E900B3BDC5 /* HTTPAuthorization.swift in Sources */,
Expand Down
11 changes: 1 addition & 10 deletions HTTPService/HTTPService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public typealias HTTPResult<T> = Result<T?, HTTPServiceError>

public protocol HTTPService: class {

associatedtype Builder: HTTPServiceBuilder
associatedtype Builder: HTTPServiceBuildable
associatedtype Authorization: HTTPAuthorization

var urlSession: URLSession { get }
Expand Down Expand Up @@ -52,15 +52,6 @@ public protocol HTTPService: class {
}

extension HTTPService {

var urlSession: URLSession {
return URLSession.shared
}

var headers: HTTPHeaders? {
return nil
}

private func logRequestInfo(for request: URLRequest) {
var info = """
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,4 @@
// Copyright © 2019 Jeremy Fox. All rights reserved.
//

public protocol HTTPServiceBuilder {
static func build<T>() -> T?
}
public protocol HTTPServiceBuildable: ServiceBuildable where Service: HTTPService {}
13 changes: 13 additions & 0 deletions HTTPService/ServiceBuildable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// ServiceBuildable.swift
// HTTPService
//
// Created by Jeremy Fox on 8/8/19.
// Copyright © 2019 Jeremy Fox. All rights reserved.
//

public protocol ServiceBuildable {
associatedtype Service

static func build() -> Service?
}
4 changes: 2 additions & 2 deletions HTTPService/ServiceBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ public class ServiceBuilder<T: HTTPService> {
return cachedService!
}

let service: T? = T.Builder.build()
let service = T.Builder.build()
if let service = service {
ServiceCache.shared.set(service: service, for: key)
}

return service
return service as? T
}
}

Expand Down
8 changes: 5 additions & 3 deletions HTTPServiceTests/Test Service/GitHubService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ final class GitHubService: HTTPService {
}
}

extension GitHubService: HTTPServiceBuilder {
static func build<T>() -> T? {
extension GitHubService: HTTPServiceBuildable {
typealias Service = GitHubService

static func build() -> GitHubService? {
let auth = HTTPTokenAuthorization(token: UUID().uuidString)
return GitHubService(authorization: auth) as? T
return self.init(authorization: auth)
}
}

Expand Down

0 comments on commit df289e9

Please sign in to comment.