This repository has been archived by the owner on Apr 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFeedHandler.swift
132 lines (122 loc) · 6.31 KB
/
FeedHandler.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
//
// FeedHandler.swift
// mobile
//
// Created by cb on 06.09.23.
//
import Foundation
class FeedHandler {
/// Queries jobs based on search criteria and access token.
///
/// This function initiates a network request to query jobs based on the specified search criteria, including query keywords,
/// job type, and sorting options. It constructs the request URL with query parameters for search criteria and access token,
/// then delegates the request to the `performRequest` function for execution. The response is processed, and the result is
/// returned in the completion closure.
///
/// - Parameters:
/// - query: The query string containing search keywords.
/// - jobType: The type of jobs to search for (e.g., "full-time," "part-time").
/// - sortBy: The sorting criteria for search results (e.g., "relevance," "date").
/// - accessToken: The access token required for authentication with the API.
/// - completion: A closure that receives a `Result` containing either a `JobsResponse` or an API error.
///
/// Example usage:
///
/// ```swift
/// let query = "software engineer"
/// let jobType = "full-time"
/// let sortBy = "relevance"
/// let accessToken = "your_access_token"
///
/// FeedHandler.queryJobs(query: query, jobType: jobType, sortBy: sortBy, accessToken: accessToken) { result in
/// switch result {
/// case .success(let jobs):
/// // Handle successful response data (e.g., display job listings)
/// case .failure(let error):
/// // Handle API error (e.g., display error message to the user)
/// print("API Error: \(error)")
/// }
/// }
/// ```
///
/// - SeeAlso: `performRequest` for the underlying request implementation.
/// - SeeAlso: `JobsResponse` for the response that contains the decoded `Job` array.
/// - SeeAlso: `APIError` for the possible API error types.
/// - SeeAlso: `Result` for the result type that contains either the decoded response data or an API error.
static func queryJobs(query: String, jobType: String, sortBy: String, accessToken: String, completion: @escaping (Result<JobsResponse, APIError>) -> Void) {
print("Started querying jobs with: \nquery: \(query)\njobType: \(jobType)\nsortBy: \(sortBy)\naccess_token: \(accessToken)")
guard var urlComponents = URLComponents(string: Routes.ROOT_URL + Routes.JOBS_FIND_PATH) else {
completion(.failure(APIError.invalidURL))
return
}
urlComponents.queryItems = [
URLQueryItem(name: "query", value: query),
URLQueryItem(name: "job_type", value: jobType),
URLQueryItem(name: "sortBy", value: sortBy)
]
guard let url = urlComponents.url else {
completion(.failure(APIError.invalidURL))
return
}
print("URL: \(url)")
RequestHandler.performRequest(url: url, httpMethod: HTTPMethod.GET, accessToken: accessToken, responseType: JobsResponse.self, completion: completion)
}
/// Fetches a feed of jobs based on geographical coordinates and access token.
///
/// This function initiates a network request to retrieve a feed of jobs near the specified latitude and longitude.
/// It constructs the request URL with query parameters for geographical location and access token, then delegates
/// the request to the `performRequest` function for execution. The response is processed, and the result is
/// returned in the completion closure.
///
/// - Parameters:
/// - longitude: The longitude coordinate for the job search location.
/// - latitude: The latitude coordinate for the job search location.
/// - page: The page number for paginating results (currently not implemented).
/// - accessToken: The access token required for authentication with the API.
/// - completion: A closure that receives a `Result` containing either a `FeedResult` or an API error.
///
/// - Note: This function does not currently implement pagination with the `page` parameter, but the TODO comment
/// indicates the intent to add pagination in the future. Ensure that the API supports pagination when
/// implementing this feature.
///
/// Example usage:
///
/// let longitude: Float = 123.456
/// let latitude: Float = 45.678
/// let page: Int = 1
/// let accessToken = "your_access_token"
///
/// FeedHandler.fetchFeed(longitude: longitude, latitude: latitude, page: page, accessToken: accessToken) { result in
/// switch result {
/// case .success(let jobs):
/// // Handle successful response data (e.g., display job listings)
/// case .failure(let error):
/// // Handle API error (e.g., display error message to the user)
/// print("API Error: \(error)")
/// }
/// }
///
/// - SeeAlso: `performRequest` for the underlying request implementation.
/// - SeeAlso: `FeedResponse` for the response that contains the decoded `Job` array.
/// - SeeAlso: `APIError` for the possible API error types.
/// - SeeAlso: `Result` for the result type that contains either the decoded response data or an API error.
static func fetchFeed(longitude: Float, latitude: Float, page: Int, accessToken: String, completion: @escaping (Result<FeedResponse, APIError>) -> Void) {
print("Started fetching feed with: \npage: \(page)\naccess_token: \(accessToken)")
guard var urlComponents = URLComponents(string: Routes.ROOT_URL + Routes.JOBS_FEED_PATH) else {
completion(.failure(APIError.invalidURL))
return
}
urlComponents.queryItems = [
URLQueryItem(name: "latitude", value: "\(latitude)"),
URLQueryItem(name: "longitude", value: "\(longitude)"),
// TODO: Add pagination for /jobs to Rails API:
// URLQueryItem(name: "page", value: "\(page)")
]
guard let url = urlComponents.url else {
completion(.failure(APIError.invalidURL))
return
}
print("URL: \(url)")
RequestHandler.performRequest(url: url, httpMethod: HTTPMethod.GET, accessToken: accessToken, responseType: FeedResponse.self, completion: completion)
}
}