Skip to content

Commit

Permalink
Update api.
Browse files Browse the repository at this point in the history
  • Loading branch information
Arrow-Li committed Aug 11, 2022
1 parent b7aae68 commit 9e44370
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
Binary file modified convert
Binary file not shown.
34 changes: 26 additions & 8 deletions convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,38 @@ func request(api, key, model string) (map[string]interface{}, error) {
func convert(model, amount string) string {
var rate float64

retry:=false
data, err := request(API_URL_1, API_KEY_1, model)
if err != nil {
if err ==nil{
if rates,ok:=data["quotes"].(map[string]interface{});ok{
u2c_rate := rates["USDCNY"].(float64)
if model == "USD"{
rate = 1.0 / u2c_rate
}else{
base_rate := rates["USD"+model].(float64)
rate = base_rate / u2c_rate
}
}else{
retry=true
}
}else{
retry=true
}

if retry{
data, err = request(API_URL_2, API_KEY_2, model)
if err != nil {
return ""
}
u2c_rate := data["rates"].(map[string]interface{})["CNY"].(float64)
base_rate := data["rates"].(map[string]interface{})[model].(float64)
rate = base_rate / u2c_rate
} else {
u2c_rate := data["quotes"].(map[string]interface{})["USDCNY"].(float64)
base_rate := data["quotes"].(map[string]interface{})["USD"+model].(float64)
rate = base_rate / u2c_rate
if rates,ok:=data["rates"].(map[string]interface{});ok{
u2c_rate := rates["CNY"].(float64)
base_rate := rates[model].(float64)
rate = base_rate / u2c_rate
}else{
return ""
}
}

result, _ := strconv.ParseFloat(amount, 64)
result /= rate

Expand Down

0 comments on commit 9e44370

Please sign in to comment.