ellisvalentiner / AlphaVantage.jl
1

2
"""
3
Internal function that wraps Requests.get
4
"""
5 1
function _get_request(uri::String)
6 4
    resp = HTTP.get(uri)
7
    # Check if API limit reached
8 4
    for header in resp.headers
9 4
        if header[1] == "Content-Type" && header[2] == "application/json"
10 4
            body = _parse_response(resp, "json")
11
            # JSON object with 'Note' indicates the API limit was reached
12 4
            if haskey(body, "Note")
13 4
                error("API limit exceeded")
14
            end
15
        end
16
    end
17 4
    if resp.status != 200
18
        #desc = STATUS_CODES[status]
19 0
        error("Expected status code 200 but received $resp.status")
20
    end
21 4
    return resp
22
end
23

24
"""
25
Internal function that parses the response
26
"""
27 1
function _parse_response(data, datatype::String)
28 4
    if datatype == "csv"
29 4
        return readdlm(data.body, ',', header=true)
30 4
    elseif datatype == "json"
31 1
        body = copy(data.body)  # TODO: re-write to avoid copying
32 4
        return JSON.Parser.parse(String(body))
33
    end
34
end
35

36
"""
37
Internal function that helps forms the request uri
38
"""
39 1
function _form_uri_tail(client::AVClient, outputsize, datatype)
40 4
    a = "&apikey=" * key(client)
41 4
    a = outputsize === nothing ? a : "&outputsize=$outputsize" * a
42 4
    a = datatype === nothing ? a : "&datatype=$datatype" * a
43

44 4
    return a
45
end
46

47
function _form_uri_tail(client::AVClient)
48 4
    "&apikey=" * key(client)
49
end
50

51

52
"""
53
Internal function that helps forms the request uri
54
"""
55
function _form_uri_head(client::AVClient, func)
56 1
    uri = entry(client) * "query?"
57 1
    f = "function="* uppercase(func)
58 1
    uri * f
59
end
60

61
function _parse_params(params)
62 4
    if isempty(params)
63 4
        return ""
64
    else
65 1
        args = keys(params)
66 1
        values = collect(params)
67 4
        return mapreduce(i-> "&$(args[i])=$(params[i])", *, 1:length(params)) 
68
    end
69
end
70

71 1
_parser(p, datatype) = p
72
function _parser(p::AbstractString, datatype)
73 4
    if p == "default"
74 4
        return x -> _parse_response(x, datatype)
75
    else
76
        # if parser is unknown, then return raw data
77 0
        return identity
78
    end
79
end

Read our documentation on viewing source code .

Loading