Skip to content

Commit 01c8502

Browse files
authored
Merge pull request #10 from adborbas/adborbas/support_rapidapi
Adding support for API exposed on RapidAPI
2 parents f8ace12 + 38c3a24 commit 01c8502

18 files changed

+361
-96
lines changed

README.md

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# AlphaSwiftage
22

3-
Lightweight Swift library to access the [Alpha Vantage API](https://www.alphavantage.co/documentation/).
3+
Lightweight Swift library to access the [Alpha Vantage API](https://www.alphavantage.co/documentation/). The library does also support the [Alpha Vantage API exposed on RapidAPI](https://rapidapi.com/alphavantage/api/alpha-vantage).
44

55
## Installation
66

@@ -14,20 +14,27 @@ dependencies: [
1414

1515
## Usage
1616

17+
Using the native Alpha Vantage API:
18+
1719
```swift
1820
import AlphaSwiftage
1921

2022
let service = AlphaVantageService(apiKey: "{YOUR_API_KEY}")
2123
let symbols = try await service.symbolSearch(keywords: "VWCE")
2224
```
2325

26+
Alpha Vantage API exposed on RapidAPI:
27+
28+
```swift
29+
import AlphaSwiftage
30+
31+
let service = AlphaVantageService(serviceType: .rapidAPI(apiKey: "{YOUR_API_KEY}"))
32+
let symbols = try await service.symbolSearch(keywords: "VWCE")
33+
```
34+
2435
## Supported routes
2536

2637
- [Symbol Search](https://www.alphavantage.co/documentation/#symbolsearch)
2738
- [Quote](https://www.alphavantage.co/documentation/#latestprice)
2839
- [Exchange Rates](https://www.alphavantage.co/documentation/#currency-exchange)
2940
- [Time Series - Daily Adjusted](https://www.alphavantage.co/documentation/#dailyadj)
30-
31-
## API Key
32-
33-
You can get a free API Key [here](https://www.alphavantage.co/support/#api-key).
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import Alamofire
2+
import Foundation
3+
4+
protocol APIHost {
5+
var baseURL: URL { get }
6+
var headers: HTTPHeaders { get }
7+
var baseParameters: Parameters { get }
8+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import Foundation
2+
3+
class APIHostFactory {
4+
private init () {}
5+
6+
static let shared = APIHostFactory()
7+
8+
func host(for serviceType: AlphaVantageServiceType) -> APIHost {
9+
switch serviceType {
10+
case .native(let apiKey):
11+
return NativeAPIHost(apiKey: apiKey)
12+
case .rapidAPI(let apiKey):
13+
return RapidAPIHost(apiKey: apiKey)
14+
}
15+
}
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import Alamofire
2+
import Foundation
3+
4+
class NativeAPIHost: APIHost {
5+
private let apiKey: String
6+
7+
let baseURL = URL(string: "https://www.alphavantage.co/query")!
8+
let headers = HTTPHeaders()
9+
lazy var baseParameters: Parameters = {
10+
return ["apikey": apiKey]
11+
}()
12+
13+
init(apiKey: String) {
14+
self.apiKey = apiKey
15+
}
16+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import Alamofire
2+
import Foundation
3+
4+
class RapidAPIHost: APIHost {
5+
private let apiKey: String
6+
7+
let baseURL = URL(string: "https://alpha-vantage.p.rapidapi.com/query")!
8+
lazy var headers: HTTPHeaders = {
9+
return HTTPHeaders([
10+
"x-rapidapi-host": "alpha-vantage.p.rapidapi.com",
11+
"x-rapidapi-key": apiKey
12+
])
13+
}()
14+
let baseParameters: Parameters = [:]
15+
16+
init(apiKey: String) {
17+
self.apiKey = apiKey
18+
}
19+
}

Sources/AlphaSwiftage/Private/AlphaVantageResponseSerializer.swift renamed to Sources/AlphaSwiftage/Private/Alamofire+AlphaSwiftage/AlphaVantageResponseSerializer.swift

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
//
2-
// AlphaVantageResponseSerializer.swift
3-
//
4-
//
5-
// Created by Adam Borbas on 22/03/2024.
6-
//
7-
81
import Foundation
92
import Alamofire
103

Sources/AlphaSwiftage/Private/AlphaVantageAPI.swift

Lines changed: 0 additions & 67 deletions
This file was deleted.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import Foundation
2+
3+
enum AlphaVantageAPI {
4+
enum Function: String {
5+
case globalQuote = "GLOBAL_QUOTE"
6+
case currencyExchangeRate = "CURRENCY_EXCHANGE_RATE"
7+
case symbolSearch = "SYMBOL_SEARCH"
8+
case dailyAdjustedTimeSeries = "TIME_SERIES_DAILY_ADJUSTED"
9+
}
10+
11+
enum Parameter: String {
12+
case symbol
13+
case apiKey = "apikey"
14+
case fromCurrency = "from_currency"
15+
case toCurrency = "to_currency"
16+
case keywords
17+
}
18+
}

0 commit comments

Comments
 (0)