-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrypto.go
227 lines (196 loc) · 5.34 KB
/
crypto.go
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package main
import (
"encoding/json"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/dynamodb"
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
"time"
"github.com/jamespearly/loggly"
)
const DB_TABLE_NAME = "Maldonado-CryptoBro"
type Currency_t struct {
Id string `json:"id"`
Rank string `json:"rank"`
Symbol string `json:"symbol"`
Name string `json:"name"`
Supply string `json:"supply"`
MaxSupply string `json:"maxSupply"`
MarketCapUsd string `json:"marketCapUsd"`
VolumeUsd24Hr string `json:"volumeUsd24Hr"`
PriceUsd string `json:"priceUsd"`
ChangePercent24Hr string `json:"changePercent24Hr"`
Vwap24Hr string `json:"vwap24Hr"`
Explorer string `json:"explorer"`
}
func tableExists(tableName string, tableNames []*string) bool {
for _, name := range tableNames {
if tableName == *name {
return true
}
}
return false
}
func createTable(db *dynamodb.DynamoDB) {
_, err := db.CreateTable(&dynamodb.CreateTableInput{
AttributeDefinitions: []*dynamodb.AttributeDefinition{
{
AttributeName: aws.String("Id"),
AttributeType: aws.String("S"),
},
},
KeySchema: []*dynamodb.KeySchemaElement{
{
AttributeName: aws.String("Id"),
KeyType: aws.String("HASH"),
},
},
ProvisionedThroughput: &dynamodb.ProvisionedThroughput{
ReadCapacityUnits: aws.Int64(1),
WriteCapacityUnits: aws.Int64(1),
},
TableName: aws.String("Maldonado-CryptoBro"),
})
if err != nil {
log.Println(err)
throwLogError("Could not create table in DynamoDB.")
}
}
func PutItem(currency Currency_t, tableName string, db *dynamodb.DynamoDB) {
_, err := db.PutItem(&dynamodb.PutItemInput{
Item: map[string]*dynamodb.AttributeValue{
"Id": {
S: aws.String(currency.Id),
},
"Rank": {
S: aws.String(currency.Rank),
},
"Symbol": {
S: aws.String(currency.Symbol),
},
"Name": {
S: aws.String(currency.Name),
},
"Supply": {
S: aws.String(currency.Supply),
},
"MaxSupply": {
S: aws.String(currency.MaxSupply),
},
"MarketCapUsd": {
S: aws.String(currency.MarketCapUsd),
},
"VolumeUsd24Hr": {
S: aws.String(currency.VolumeUsd24Hr),
},
"PriceUsd": {
S: aws.String(currency.PriceUsd),
},
"ChangePercent24Hr": {
S: aws.String(currency.ChangePercent24Hr),
},
"Vwap24Hr": {
S: aws.String(currency.Vwap24Hr),
},
},
TableName: aws.String(tableName),
})
if err != nil {
log.Println(err)
throwLogError("Could not make a DynamoDB table entry.")
}
}
type jsonData struct {
Data []Currency_t `json:"data"`
Timestamp int64 `json:"timestamp"`
}
/**
@param:
- msg: error message to log to loggly
*/
func throwLogError(msg string) {
client := loggly.New("CryptoApi")
logErr := client.EchoSend("error", msg)
if logErr != nil {
os.Exit(1)
}
}
/**
@param:
- body: string json response from the endpoint
@return:
- []Currency_t: an array of currency_t structs representing the json response info
*/
func extractJsonData(body string) []Currency_t {
var jsonData jsonData
err := json.Unmarshal([]byte(body), &jsonData)
if err != nil {
throwLogError("Could not parse String into Json format.")
log.Fatalln(err)
}
return jsonData.Data
}
func main() {
for {
fmt.Println("-----==== Starting HTTP worker ====-----")
// GET request on Endpoint
resp, err := http.Get("https://api.coincap.io/v2/assets")
if err != nil {
throwLogError("Could not pull data from API.")
log.Fatalln(err)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
throwLogError("Could not read data from the API.")
log.Fatalln(err)
}
bodyStr := string(body)
jsonData := extractJsonData(bodyStr)
for i := 0; i < len(jsonData); i++ {
fmt.Printf("Id:\t\t\t%s\n", jsonData[i].Id)
fmt.Printf("Rank:\t\t\t%s\n", jsonData[i].Rank)
fmt.Printf("Symbol:\t\t\t%s\n", jsonData[i].Symbol)
fmt.Printf("Name:\t\t\t%s\n", jsonData[i].Name)
fmt.Printf("Supply:\t\t\t%s\n", jsonData[i].Supply)
fmt.Printf("Max Supply:\t\t%s\n", jsonData[i].MaxSupply)
fmt.Printf("Market Cap (USD):\t%s\n", jsonData[i].MarketCapUsd)
fmt.Printf("Volume 24 Hours (USD):\t%s\n", jsonData[i].VolumeUsd24Hr)
fmt.Printf("Price (USD):\t\t%s\n", jsonData[i].PriceUsd)
fmt.Printf("Change Percent 24 Hr: \t%s\n", jsonData[i].ChangePercent24Hr)
fmt.Printf("VWAP 24 Hours:\t\t%s\n\n", jsonData[i].Vwap24Hr)
}
// Send a Success message to Loggly
client := loggly.New("CryptoApi")
logErr := client.EchoSend("info", "Data polled. "+strconv.Itoa(len(jsonData)))
if logErr != nil {
println(logErr)
return
}
// Open a new DynamoDB session
db := dynamodb.New(session.Must(session.NewSession(&aws.Config{
Region: aws.String("us-east-1"),
Endpoint: aws.String("https://dynamodb.us-east-1.amazonaws.com"),
})))
tables, _ := db.ListTables(&dynamodb.ListTablesInput{})
tables.String()
if !tableExists(DB_TABLE_NAME, tables.TableNames) {
createTable(db)
}
for _, currency := range jsonData {
PutItem(currency, DB_TABLE_NAME, db)
}
// Send a Success message to Loggly
client = loggly.New("CryptoApi")
logErr = client.EchoSend("info", "Entered all currencies into database.")
if logErr != nil {
println(logErr)
return
}
time.Sleep(60 * time.Hour)
}
}