forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSerpAPISearchService.swift
More file actions
67 lines (53 loc) · 2.08 KB
/
SerpAPISearchService.swift
File metadata and controls
67 lines (53 loc) · 2.08 KB
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
import Foundation
struct SerpAPIResponse: Codable {
var organic_results: [OrganicResult]
struct OrganicResult: Codable {
var position: Int?
var title: String?
var link: String?
var snippet: String?
func toWebSearchResult() -> WebSearchResult.WebPage? {
guard let link, let title else { return nil }
return WebSearchResult.WebPage(urlString: link, title: title, snippet: snippet ?? "")
}
}
func toWebSearchResult() -> WebSearchResult {
return WebSearchResult(webPages: organic_results.compactMap { $0.toWebSearchResult() })
}
}
struct SerpAPISearchService: SearchService {
let engine: WebSearchProvider.SerpAPIEngine
let endpoint: URL = .init(string: "https://serpapi.com/search.json")!
let apiKey: String
init(engine: WebSearchProvider.SerpAPIEngine, apiKey: String) {
self.engine = engine
self.apiKey = apiKey
}
func search(query: String) async throws -> WebSearchResult {
var request = URLRequest(url: endpoint)
request.httpMethod = "GET"
var urlComponents = URLComponents(url: endpoint, resolvingAgainstBaseURL: true)!
urlComponents.queryItems = [
URLQueryItem(name: "q", value: query),
URLQueryItem(name: "engine", value: engine.rawValue),
URLQueryItem(name: "api_key", value: apiKey)
]
guard let url = urlComponents.url else {
throw URLError(.badURL)
}
request = URLRequest(url: url)
request.httpMethod = "GET"
let (data, response) = try await URLSession.shared.data(for: request)
guard let httpResponse = response as? HTTPURLResponse, httpResponse.statusCode == 200 else {
throw URLError(.badServerResponse)
}
// Parse the response into WebSearchResult
let decoder = JSONDecoder()
do {
let searchResponse = try decoder.decode(SerpAPIResponse.self, from: data)
return searchResponse.toWebSearchResult()
} catch {
throw error
}
}
}