Skip to content

Commit a9edd34

Browse files
committed
add MicrocmsParameter and callback on main thread
1 parent fe3d910 commit a9edd34

File tree

3 files changed

+93
-17
lines changed

3 files changed

+93
-17
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import Foundation
2+
3+
/// Parameters that can be passed to the get function.
4+
public enum MicrocmsParameter {
5+
// MARK: - Common Parameters
6+
///
7+
/// Elements to be retrieved in the content.
8+
/// ex) .fields(["title", "main_image", "updatedAt", "author.name"]
9+
case fields([String])
10+
11+
/// Depth of the hierarchy from which to retrieve the reference content.
12+
/// Default value is 1, and maximum is 3.
13+
case depth(Int)
14+
15+
// MARK: - List Parameters
16+
17+
/// Number of retrievals, default value is 10.
18+
case limit(Int)
19+
20+
/// Offset, default value is 0.
21+
case offset(Int)
22+
23+
/// Sort the contents to be retrieved.
24+
/// ex) .orders(["publishedAt"])
25+
///
26+
/// If you want to specify descending order, prefix the field name with - (minus).
27+
/// .orders(["publishedAt", "-updatedAt"])
28+
///
29+
/// Only fields with "Date", "True/False", and "Numeric" can be sorted,
30+
/// and any other field specification will be ignored.
31+
case orders([String])
32+
33+
/// Performs a full-text search of the content.
34+
/// The fields to be searched are "text field", "text area", and "rich editor".
35+
case q(String)
36+
37+
/// Get only the target content by specifying the content id.
38+
/// ex) .ids(["first_id", "second_id", "third_id"])
39+
case ids([String])
40+
41+
/// Filters the content to be retrieved by specifying conditions.
42+
/// There are various options, so please refer to the API reference for available conditional expressions.
43+
/// https://document.microcms.io/content-api/get-list-contents#hdebbdc8e86
44+
case filters(String)
45+
46+
var queryItem: URLQueryItem {
47+
switch self {
48+
49+
case .fields(let value):
50+
return .init(name: "fields", value: value.joined(separator: ","))
51+
case .depth(let value):
52+
return .init(name: "depth", value: String(value))
53+
case .limit(let value):
54+
return .init(name: "limit", value: String(value))
55+
case .offset(let value):
56+
return .init(name: "offset", value: String(value))
57+
case .orders(let value):
58+
return .init(name: "orders", value: value.joined(separator: ","))
59+
case .q(let value):
60+
return .init(name: "q", value: value)
61+
case .ids(let value):
62+
return .init(name: "ids", value: value.joined(separator: ","))
63+
case .filters(let value):
64+
return .init(name: "filters", value: value)
65+
}
66+
}
67+
}

Sources/MicrocmsSDK/MicrocmsSDK.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public struct MicrocmsClient {
3131
public func makeRequest(
3232
endpoint: String,
3333
contentId: String?,
34-
params: [String: String]?) -> URLRequest? {
34+
params: [MicrocmsParameter]?) -> URLRequest? {
3535
var urlString = baseUrl + "/" + endpoint
3636
if let contentId = contentId {
3737
urlString += "/\(contentId)"
@@ -46,9 +46,7 @@ public struct MicrocmsClient {
4646
}
4747

4848
if let params = params {
49-
components.queryItems = params.map {
50-
URLQueryItem(name: $0.key, value: $0.value)
51-
}
49+
components.queryItems = params.map { $0.queryItem }
5250
}
5351

5452
var request = URLRequest(url: components.url!)
@@ -73,7 +71,7 @@ public struct MicrocmsClient {
7371
public func get(
7472
endpoint: String,
7573
contentId: String? = nil,
76-
params: [String: String]? = nil,
74+
params: [MicrocmsParameter]? = nil,
7775
completion: @escaping ((Result<Any, Error>) -> Void)) -> URLSessionTask? {
7876

7977
guard let request = makeRequest(
@@ -83,11 +81,13 @@ public struct MicrocmsClient {
8381

8482
let task = URLSession.shared.dataTask(with: request) { data, response, error in
8583
guard let data = data else { return }
86-
do {
87-
let object = try JSONSerialization.jsonObject(with: data, options: [])
88-
completion(.success(object))
89-
} catch let error {
90-
completion(.failure(error))
84+
DispatchQueue.main.async {
85+
do {
86+
let object = try JSONSerialization.jsonObject(with: data, options: [])
87+
completion(.success(object))
88+
} catch let error {
89+
completion(.failure(error))
90+
}
9191
}
9292
}
9393
task.resume()

Tests/MicrocmsSDKTests/MicrocmsSDKTests.swift

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,28 @@ final class MicrocmsSDKTests: XCTestCase {
3838
serviceDomain: "test-service",
3939
apiKey: "test-api-key")
4040

41-
let params: [String: String] = [
42-
"fields": "id,publishedAt",
43-
"filters": "createdAt[greater_than]2019-11",
44-
"offset": "1",
45-
"limit": "2",
41+
let params: [MicrocmsParameter] = [
42+
.fields(["id", "publishedAt"]),
43+
.depth(2),
44+
.limit(2),
45+
.offset(1),
46+
.orders(["-updatedAt"]),
47+
.q("test"),
48+
.ids(["first_id", "second_id"]),
49+
.filters("createdAt[greater_than]2019-11"),
4650
]
4751
let request = client.makeRequest(endpoint: "endpoint",
4852
contentId: nil,
4953
params: params)
5054
XCTAssertTrue(request?.url?.query?.contains("fields=id,publishedAt") == true)
51-
XCTAssertTrue(request?.url?.query?.contains("filters=createdAt%5Bgreater_than%5D2019-11") == true)
52-
XCTAssertTrue(request?.url?.query?.contains("offset=1") == true)
55+
XCTAssertTrue(request?.url?.query?.contains("depth=2") == true)
5356
XCTAssertTrue(request?.url?.query?.contains("limit=2") == true)
57+
XCTAssertTrue(request?.url?.query?.contains("offset=1") == true)
58+
XCTAssertTrue(request?.url?.query?.contains("orders=-updatedAt") == true)
59+
XCTAssertTrue(request?.url?.query?.contains("orders=-updatedAt") == true)
60+
XCTAssertTrue(request?.url?.query?.contains("q=test") == true)
61+
XCTAssertTrue(request?.url?.query?.contains("ids=first_id,second_id") == true)
62+
XCTAssertTrue(request?.url?.query?.contains("filters=createdAt%5Bgreater_than%5D2019-11") == true)
5463
}
5564

5665
func testMakeRequest_headers() {

0 commit comments

Comments
 (0)