Skip to content

Commit

Permalink
Support Post api without singleton configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
rocxteady committed Aug 1, 2024
1 parent 4c9e3dc commit 798c69c
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 16 deletions.
6 changes: 3 additions & 3 deletions Sources/WPSwift/API/WPClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,21 @@ public struct WPClient<RequestModel: Encodable, Response: Decodable> {
switch configuration.parameterType {
case .object(let dictionary):
requestConfig = RequestConfiguration(
urlString: try .initialize(with: configuration.endpoint),
urlString: try .initialize(with: configuration.endpointType),
method: configuration.method,
parameters: dictionary,
headers: configuration.headers, encoding: configuration.encoding
)
case .model(let encodable):
requestConfig = RequestConfiguration(
urlString: try .initialize(with: configuration.endpoint),
urlString: try .initialize(with: configuration.endpointType),
method: configuration.method,
body: try JSONEncoder.initialize().encode(encodable),
headers: configuration.headers, encoding: configuration.encoding
)
case .none:
requestConfig = RequestConfiguration(
urlString: try .initialize(with: configuration.endpoint),
urlString: try .initialize(with: configuration.endpointType),
method: configuration.method,
headers: configuration.headers, encoding: configuration.encoding
)
Expand Down
36 changes: 32 additions & 4 deletions Sources/WPSwift/API/WPClientConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,19 @@ struct WPClientConfiguration {
case object([String: Any])
case model(Encodable)
}

let endpoint: String

enum EndpointType {
case base(baseURL: String, endpoint: String)
case endpoint(String)
}
let endpointType: EndpointType
let method: HTTPMethod
let encoding: HTTPEncoding
let headers: [String: String]?
let parameterType: ParameterType?

init(endpoint: String, method: HTTPMethod = .get, parameters: [String : Any]?, encoding: HTTPEncoding = .urlEncoded, headers: [String: String]? = nil) {
self.endpoint = endpoint
self.endpointType = .endpoint(endpoint)
self.method = method
self.encoding = encoding
if let parameters {
Expand All @@ -33,7 +37,31 @@ struct WPClientConfiguration {
}

init(endpoint: String, method: HTTPMethod = .post, requestModel: Encodable?, encoding: HTTPEncoding = .json, headers: [String: String]? = nil) {
self.endpoint = endpoint
self.endpointType = .endpoint(endpoint)
self.method = method
self.encoding = encoding
if let requestModel {
self.parameterType = .model(requestModel)
} else {
self.parameterType = nil
}
self.headers = headers
}

init(baseURL: String, endpoint: String, method: HTTPMethod = .get, parameters: [String : Any]?, encoding: HTTPEncoding = .urlEncoded, headers: [String: String]? = nil) {
self.endpointType = .base(baseURL: baseURL, endpoint: endpoint)
self.method = method
self.encoding = encoding
if let parameters {
self.parameterType = .object(parameters)
} else {
self.parameterType = nil
}
self.headers = headers
}

init(baseURL: String, endpoint: String, method: HTTPMethod = .post, requestModel: Encodable?, encoding: HTTPEncoding = .json, headers: [String: String]? = nil) {
self.endpointType = .base(baseURL: baseURL, endpoint: endpoint)
self.method = method
self.encoding = encoding
if let requestModel {
Expand Down
23 changes: 16 additions & 7 deletions Sources/WPSwift/Extensions/URL+Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,23 @@
import Foundation

extension String {
static func initialize(with endpoint: String) throws -> String {
let configuration = try WPSwift.configuration
guard var url = URL(string: configuration.route) else {
throw NetworkError.urlMalformed
static func initialize(with endpointType: WPClientConfiguration.EndpointType) throws -> String {
switch endpointType {
case .base(let baseURL, let endpoint):
guard var url = URL(string: baseURL) else {
throw NetworkError.urlMalformed
}
url.append(path: endpoint)
return url.absoluteString
case .endpoint(let endpoint):
let configuration = try WPSwift.configuration
guard var url = URL(string: configuration.route) else {
throw NetworkError.urlMalformed
}
url.append(path: configuration.namespace)
url.append(path: endpoint)
return url.absoluteString
}
url.append(path: configuration.namespace)
url.append(path: endpoint)
return url.absoluteString
}

static func initializeRoute() throws -> String {
Expand Down
33 changes: 33 additions & 0 deletions Sources/WPSwift/Repositories/PostWithSourceRepository.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//
// PostWithSourceRepository.swift
//
//
// Created by Ulaş Sancak on 6.10.2023.
//

import Foundation
import Resting

public struct PostWithSourceRepository {
public init() {}

public func getPostsClient(page: Int = 1, perPage: Int = 10, order: OrderType = .descending, categories: [Int]? = nil, categoriesToExclude: [Int]? = nil, tags: [Int]? = nil, tagsToExclude: [Int]? = nil, include: [Int]? = nil) throws -> WPClient<EmptyModel, [Post]> {
try .init(.init(endpoint: WPEndpoint.Posts.posts.path, parameters: .createParamsForPosts(page: page, perPage: perPage, order: order, categories: categories, categoriesToExclude: categoriesToExclude, tags: tags, tagsToExclude: tagsToExclude, include: include)))
}

public func getSearchPostsClient(term: String, page: Int = 1, perPage: Int = 10) throws -> WPClient<EmptyModel, [SimplePost]> {
try .init(.init(endpoint: WPEndpoint.Posts.search.path, parameters: .createParamsForSearchPosts(term: term, page: page, perPage: perPage)))
}

public func getPostClient(by id: Int) throws -> WPClient<EmptyModel, Post> {
try .init(.init(endpoint: WPEndpoint.Posts.post(id: id).path, parameters: .createParamsForPost()))
}

public func createPostClient(by post: PostToCreate) throws -> WPClient<PostToCreate, Post> {
try .init(.init(endpoint: WPEndpoint.Posts.posts.path, method: .post, requestModel: post))
}

public func updatePostClient(by id: Int, post: PostToUpdate) throws -> WPClient<PostToUpdate, Post> {
try .init(.init(endpoint: WPEndpoint.Posts.post(id: id).path, method: .post, requestModel: post))
}
}
26 changes: 24 additions & 2 deletions Sources/WPSwift/Repositories/PostsRepository.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// File.swift
//
// !isPresented.swift
//
//
// Created by Ulaş Sancak on 6.10.2023.
//
Expand Down Expand Up @@ -31,3 +31,25 @@ public struct PostsRepository {
try .init(.init(endpoint: WPEndpoint.Posts.post(id: id).path, method: .post, requestModel: post))
}
}

extension PostsRepository {
public func getPostsClient(baseURL: String, page: Int = 1, perPage: Int = 10, order: OrderType = .descending, categories: [Int]? = nil, categoriesToExclude: [Int]? = nil, tags: [Int]? = nil, tagsToExclude: [Int]? = nil, include: [Int]? = nil) throws -> WPClient<EmptyModel, [Post]> {
try .init(.init(baseURL: baseURL, endpoint: WPEndpoint.Posts.posts.path, parameters: .createParamsForPosts(page: page, perPage: perPage, order: order, categories: categories, categoriesToExclude: categoriesToExclude, tags: tags, tagsToExclude: tagsToExclude, include: include)))
}

public func getSearchPostsClient(baseURL: String, term: String, page: Int = 1, perPage: Int = 10) throws -> WPClient<EmptyModel, [SimplePost]> {
try .init(.init(baseURL: baseURL, endpoint: WPEndpoint.Posts.search.path, parameters: .createParamsForSearchPosts(term: term, page: page, perPage: perPage)))
}

public func getPostClient(baseURL: String, by id: Int) throws -> WPClient<EmptyModel, Post> {
try .init(.init(baseURL: baseURL, endpoint: WPEndpoint.Posts.post(id: id).path, parameters: .createParamsForPost()))
}

public func createPostClient(baseURL: String, by post: PostToCreate) throws -> WPClient<PostToCreate, Post> {
try .init(.init(baseURL: baseURL, endpoint: WPEndpoint.Posts.posts.path, method: .post, requestModel: post))
}

public func updatePostClient(baseURL: String, by id: Int, post: PostToUpdate) throws -> WPClient<PostToUpdate, Post> {
try .init(.init(baseURL: baseURL, endpoint: WPEndpoint.Posts.post(id: id).path, method: .post, requestModel: post))
}
}

0 comments on commit 798c69c

Please sign in to comment.