1+ /**
2+ APIs provided by Alpha Vantage.
3+ */
4+ public struct AlphaVantageAPI {
5+ static let url = " https://www.alphavantage.co/query? "
6+
7+ private init ( ) { }
8+
9+ /// Data type returns from API.
10+ public enum DataType : String {
11+ case csv
12+ case json
13+ }
14+
15+ /// Stock data related APIs.
16+ public enum Stock {
17+ /**
18+ Construst the API query string by parameters.
19+
20+ - parameters:
21+ - function: The API function of your choice
22+ - symbol: The symbol of the global security of your choice.
23+ For example: `symbol=MSFT`
24+ - dataType: Data format to be returned from API requeset. Default
25+ format is `.json`
26+ - apiKey: Your API key. Claim your free API key
27+ [here](https://www.alphavantage.co/support/#api-key)
28+ */
29+ static func api( function: Function ,
30+ symbol: String ,
31+ dataType: DataType = . json,
32+ apiKey: String ) -> String {
33+ let temp = " \( url) function= "
34+ var api = " "
35+
36+ switch function {
37+ case let . intraday( interval) :
38+ api = " \( temp) \( Const . intraday) &interval= \( interval. rawValue) "
39+ case . daily:
40+ api = " \( temp) \( Const . daily) "
41+ case . dailyAdjusted:
42+ api = " \( temp) \( Const . dailyAdjusted) "
43+ case . weekly:
44+ api = " \( temp) \( Const . weekly) "
45+ case . weeklyAdjusted:
46+ api = " \( temp) \( Const . weeklyAdjusted) "
47+ case . monthly:
48+ api = " \( temp) \( Const . monthly) "
49+ case . monthlyAdjusted:
50+ api = " \( temp) \( Const . monthlyAdjusted) "
51+ case . quoteEndpoint:
52+ api = " \( temp) \( Const . quoteEndpoint) "
53+ case let . searchEndpoint( keyword) :
54+ api = " \( temp) \( Const . searchEndpoint) &keywords= \( keyword) "
55+ }
56+
57+ switch function {
58+ case . intraday,
59+ . daily,
60+ . dailyAdjusted:
61+ api += " &symbol= \( symbol) &ouputsize=full "
62+ case . weekly,
63+ . weeklyAdjusted,
64+ . monthly,
65+ . monthlyAdjusted,
66+ . quoteEndpoint:
67+ api += " &symbol= \( symbol) "
68+ case . searchEndpoint:
69+ break
70+ }
71+
72+ return " \( api) &datatype= \( dataType) &apikey= \( apiKey) "
73+ }
74+
75+ /**
76+ Supported time interval between two consecutive data points in
77+ intraday time series.
78+ */
79+ public enum IntradayInterval : String {
80+ /// 1 minute interval.
81+ case min1 = " 1min "
82+ /// 5 minutes interval.
83+ case min5 = " 5min "
84+ /// 15 minutes interval.
85+ case min15 = " 15min "
86+ /// 30 minutes interval.
87+ case min30 = " 30min "
88+ /// 1 hour interval.
89+ case min60 = " 60min "
90+ }
91+
92+ /**
93+ Available APIs provide realtime and historical global equity data.
94+ Daily, weekly, and monthly time series contain 20+ years of
95+ historical data.
96+ */
97+ public enum Function {
98+ /**
99+ Intraday time series (timestamp, open, high, low, close, volume)
100+ of the equity specified.
101+
102+ - interval: Time interval between two consecutive data points
103+ in the time series
104+ */
105+ case intraday( interval: IntradayInterval )
106+ /**
107+ Daily time series (date, daily open, daily high, daily low,
108+ daily close, daily volume) of the global equity specified.
109+ */
110+ case daily
111+ /**
112+ Daily time series (date, daily open, daily high, daily low,
113+ daily close, daily volume, daily adjusted close, and
114+ split/dividend events) of the global equity specified.
115+ */
116+ case dailyAdjusted
117+ /**
118+ Weekly adjusted time series (last trading day of each week,
119+ weekly open, weekly high, weekly low, weekly close, weekly
120+ adjusted close, weekly volume, weekly dividend) of the global
121+ equity specified.
122+ */
123+ case weekly
124+ /**
125+ Weekly adjusted time series (last trading day of each week,
126+ weekly open, weekly high, weekly low, weekly close, weekly
127+ adjusted close, weekly volume, weekly dividend) of the global
128+ equity specified.
129+ */
130+ case weeklyAdjusted
131+ /**
132+ Monthly time series (last trading day of each month, monthly open,
133+ monthly high, monthly low, monthly close, monthly volume) of the
134+ global equity specified.
135+ */
136+ case monthly
137+ /**
138+ Monthly adjusted time series (last trading day of each month,
139+ monthly open, monthly high, monthly low, monthly close, monthly
140+ adjusted close, monthly volume, monthly dividend) of the equity
141+ specified.
142+ */
143+ case monthlyAdjusted
144+ /**
145+ A lightweight alternative to the time series APIs, this service
146+ returns the latest price and volume information for a security of
147+ your choice.
148+ */
149+ case quoteEndpoint
150+ /**
151+ Returns the best-matching symbols and market information based on
152+ keywords of your choice. The search results also contain match
153+ scores that provide you with the full flexibility to develop your
154+ own search and filtering logic.
155+
156+ - keyword: A text string of your choice.
157+ For example: `keywords=microsoft`.
158+ */
159+ case searchEndpoint( keyword: String )
160+ }
161+
162+ enum Const : String {
163+ case intraday = " TIME_SERIES_INTRADAY "
164+ case daily = " TIME_SERIES_DAILY "
165+ case dailyAdjusted = " TIME_SERIES_DAILY_ADJUSTED "
166+ case weekly = " TIME_SERIES_WEEKLY "
167+ case weeklyAdjusted = " TIME_SERIES_WEEKLY_ADJUSTED "
168+ case monthly = " TIME_SERIES_MONTHLY "
169+ case monthlyAdjusted = " TIME_SERIES_MONTHLY_ADJUSTED "
170+ case quoteEndpoint = " GLOBAL_QUOTE "
171+ case searchEndpoint = " SYMBOL_SEARCH "
172+ }
173+ }
174+ }
0 commit comments