Skip to content

Commit df6939f

Browse files
committed
Initial commit
1 parent 17aaef6 commit df6939f

File tree

6 files changed

+137
-0
lines changed

6 files changed

+137
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,5 @@ fastlane/test_output
8888
# https://github.com/johnno1962/injectionforxcode
8989

9090
iOSInjectionProject/
91+
92+
.swiftpm/

Package.resolved

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// swift-tools-version:5.3
2+
// The swift-tools-version declares the minimum version of Swift required to build this package.
3+
4+
import PackageDescription
5+
6+
let package = Package(
7+
name: "PodcastAPI",
8+
platforms: [
9+
.macOS(.v10_12),
10+
],
11+
products: [
12+
// Products define the executables and libraries a package produces, and make them visible to other packages.
13+
.library(
14+
name: "PodcastAPI",
15+
targets: ["PodcastAPI"]),
16+
.executable(
17+
name: "Example",
18+
targets: ["Example"]),
19+
],
20+
dependencies: [
21+
.package(url: "https://github.com/Alamofire/Alamofire.git", .upToNextMajor(from: "5.2.0"))
22+
],
23+
targets: [
24+
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
25+
// Targets can depend on other targets in this package, and on products in packages this package depends on.
26+
.target(
27+
name: "PodcastAPI",
28+
dependencies: ["Alamofire"]),
29+
.testTarget(
30+
name: "PodcastAPITests",
31+
dependencies: ["PodcastAPI"]),
32+
.target(
33+
name: "Example",
34+
dependencies: ["PodcastAPI"]),
35+
]
36+
)

Sources/Example/main.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//
2+
// main.swift
3+
//
4+
// Sample code to use PodcastAPI
5+
//
6+
// Created by Wenbin Fang on 5/12/21.
7+
//
8+
9+
import Foundation
10+
import PodcastAPI
11+
12+
let apiKey = ProcessInfo.processInfo.environment["LISTEN_API_KEY", default: ""]
13+
14+
let client = PodcastAPI.Client(apiKey: apiKey)
15+
var parameters: [String: String] = [:]
16+
parameters["q"] = "startup"
17+
client.search(parameters: parameters)
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import Foundation
2+
import Alamofire
3+
4+
let BASE_URL_PROD = "https://listen-api.listennotes.com/api/v2"
5+
let BASE_URL_TEST = "https://listen-api-test.listennotes.com/api/v2"
6+
7+
//extension Request {
8+
// public func debugLog() -> Self {
9+
// #if DEBUG
10+
// debugPrint(self)
11+
// #endif
12+
// return self
13+
// }
14+
//}
15+
16+
public class Client {
17+
private var apiKey: String
18+
private var baseUrl: String = BASE_URL_PROD
19+
private var userAgent: String = "podcast-api-swift"
20+
private var responseTimeout: Int = 30000
21+
22+
public init(apiKey: String) {
23+
self.apiKey = apiKey
24+
25+
if apiKey.trimmingCharacters(in: .whitespacesAndNewlines).count == 0 {
26+
self.baseUrl = BASE_URL_TEST
27+
}
28+
}
29+
30+
public func setUserAgent(userAgent: String) {
31+
self.userAgent = userAgent;
32+
}
33+
34+
public func search(parameters: [String: String]) {
35+
let url = "\(self.baseUrl)/search"
36+
let result = self.query(address: url)
37+
print(result)
38+
}
39+
40+
func query(address: String) -> String {
41+
let url = URL(string: address)
42+
let semaphore = DispatchSemaphore(value: 0)
43+
44+
var result: String = ""
45+
46+
let task = URLSession.shared.dataTask(with: url!) {(data, response, error) in
47+
result = String(data: data!, encoding: String.Encoding.utf8)!
48+
semaphore.signal()
49+
}
50+
51+
task.resume()
52+
semaphore.wait()
53+
return result
54+
}
55+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import XCTest
2+
@testable import PodcastAPI
3+
4+
final class PodcastAPITests: XCTestCase {
5+
func testExample() {
6+
let client = PodcastAPI.Client(apiKey: "")
7+
var parameters: [String: String] = [:]
8+
parameters["q"] = "startup"
9+
client.search(parameters: parameters)
10+
}
11+
}

0 commit comments

Comments
 (0)