aboutsummarylogtreecommitdiffstats
path: root/api.h
blob: 6faf7959b9af9fe99d782b4726f2426e3b9bfcb4 (plain)
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/**
 * API data is taken from IEX Trading, Alpha Vantage, Coinmarketcap, News API, and Morningstar.
 * https://iextrading.com/developer/docs/
 * https://www.alphavantage.co/documentation/
 * https://coinmarketcap.com/api/
 * https://newsapi.org/docs
 * http://www.morningstar.com/
 */

#ifndef IEX_H
#define IEX_H

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
#include <stddef.h>
#include <json-c/json_tokener.h>

struct string {
    char* data;
    size_t len;
};

typedef struct string String;

typedef struct json_object Json;

/**
 * Creates and returns a STRING
 * object with size 1 and no data
 * @return STRING object
 */
String* api_string_init(void);

/**
 * GETs data from API server and returns it in a String
 * @param url API url to GET
 * @param post_field data needed for POST
 * @return NULL if no response from server. Otherwise, String containing data.
 */
String* api_curl_data(char* url, char* post_field);

/**
 * Returns current price of a stock or cryptocurrency.
 * Order:
 * 1. IEX -- NASDAQ/NYSE/NYSEARCA
 * 2. Alpha Vantage -- OTCMKTS
 * 3. Morningstar -- MUTF
 * 4. Coinmarketcap -- CRYPTO
 * @param ticker_name_string symbol
 * @return current price of stock
 */
double api_get_current_price(char* ticker_name_string);

/**
 * If it is a listed security, returns the close price of the previous day.
 * If it is a cryptocurrency, returns the price 24 hours ago
 * @param ticker_name_string symbol
 * @return one day ago's price
 */
double api_get_1d_price(char* ticker_name_string);

/**
 * writefunction for cURL HTTP GET
 * stolen from a nice man on stackoverflow
 */
size_t api_string_writefunc(void* ptr, size_t size, size_t nmemb, String* hString);

/**
 * Returns current price of a stock with data from IEX.
 * Tested for NASDAQ, NYSE, and NYSEARCA listed stocks/ETFs.
 * Fast -- should take less than one second per call.
 * @param ticker_name_string symbol
 * @return current price of stock
 */
double iex_get_current_price(char* ticker_name_string);

/**
 * Returns current price of a mutual fund with data from Morningstar
 * Tested for MUTF and OTCMKTS listed securities.
 * Fast -- should take less than one second per call.
 * @param ticker_name_string symbol
 * @param offset number of days ago to get price of (0 = today)
 * @return price of security
 */
double morningstar_get_price(char* ticker_name_string, int offset);

/**
 * Returns current price of a mutual fund or over-the-counter stock with data from Alpha Vantage.
 * Tested for MUTF and OTCMKTS listed securities.
 * Dreadfully slow -- may take up to ten seconds per call.
 * @param ticker_name_string symbol
 * @return current price of security
 */
double alphavantage_get_current_price(char* ticker_name_string);

/**
 * Returns current price of a cryptocurrency with data from Coinmarketcap.
 * All cryptocurrencies listed on Coinmarketcap will work.
 * Fast -- should take less than one second per call.
 * @param ticker_name_string symbol
 * @return current price of cryptocurrency
 */
double coinmarketcap_get_current_price(char* ticker_name_string);

/**
 * Returns previous close price of a stock with data from IEX.
 * Tested for NASDAQ, NYSE, and NYSEARCA listed stocks/ETFs.
 * Fast -- should take less than one second per call.
 * @param ticker_name_string symbol
 * @return current price of stock
 */
double iex_get_1d_price(char* ticker_name_string);

/**
 * Returns previous close price of a mutual fund or over-the-counter stock with data from Alpha Vantage.
 * Tested for MUTF and OTCMKTS listed securities.
 * Dreadfully slow -- may take up to ten seconds per call.
 * @param ticker_name_string symbol
 * @return current price of security
 */
double alphavantage_get_1d_price(char* ticker_name_string);

/**
 * Returns price 24 hours ago of a cryptocurrency with data from Coinmarketcap.
 * If the symbol is not on IEX, Alpha Vantage will be used
 * Tested for MUTF and OTCMKTS listed securities.
 * Fast -- should take less than one second per call.
 * @param ticker_name_string symbol
 * @return current price of cryptocurrency
 */
double coinmarketcap_get_1d_price(char* ticker_name_string);

/**
 * Prints top three news articles in the past week based on the given string
 * @param ticker_name_string the string
 */
void news_print_top_three(char* ticker_name_string);

/**
 * Given a JSON formatted string, print title, source, author, and url of articles
 * @param data the json formatted data
 */
void json_print_news(char* data);

/**
 * Given a url, returns a shorter link using goo.gl
 * @param url_string the link to shorten
 * @return the shortened link
 */
const char* google_shorten_link(char* url_string);

/**
 * Destroys String object and frees memory
 * @param phString the String to destroy
 */
void api_string_destroy(String** phString);

#endif