Skip to content
This repository has been archived by the owner on Aug 12, 2024. It is now read-only.

Commit

Permalink
if read from json file
Browse files Browse the repository at this point in the history
  • Loading branch information
Arthur authored and Arthur committed Apr 9, 2020
1 parent 277efa6 commit 5a3f56a
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 16 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/config_dev.json
/config_prod.json
/.idea/
/.idea/
/.DS_Store
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
module kucoin-api-test
module kucoin-api-demo

go 1.14

require (
github.com/Kucoin/kucoin-go-sdk v1.2.6
github.com/gorilla/websocket v1.4.0
github.com/pkg/errors v0.8.1
github.com/sirupsen/logrus v1.4.1
Expand Down
17 changes: 17 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gorilla/websocket v1.4.0 h1:WDFjx/TMzVgy9VdMMQi2K2Emtwi2QcUQsztZ/zLaH/Q=
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.4.1 h1:GL2rEmy6nsikmW0r8opw9JIRScdMF5hA8cOYLH7In1k=
github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33 h1:I6FyU15t786LL7oL/hn43zqTuEGr4PN7F4XJ1p4E3Y8=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
28 changes: 15 additions & 13 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"kucoin-api-test/sdk"
"kucoin-api-demo/sdk"
"log"
"os"
"reflect"
Expand All @@ -14,7 +14,7 @@ import (

var (
kucoin *sdk.ApiService
PATH = "./config.json.example"
PATH = "./config.json"
)

func main() {
Expand All @@ -23,23 +23,24 @@ func main() {
var apiSkipVerifyTls bool

// read config from json file
file, err := ioutil.ReadFile(PATH)
if err != nil {
log.Fatal("error when read configuration from json file:", err)
return
}
var config ApiConfig
// unmarshal config data
err = json.Unmarshal(file, &config)
if err != nil {
log.Fatal("error when unmarshal config from json file:", err)
return
file, er := ioutil.ReadFile(PATH)
if er != nil {
log.Println("Failed to read configuration from json file:", er)
log.Println("Now please input api information manually.")
}else{
// unmarshal config data
err := json.Unmarshal(file, &config)
if err != nil {
log.Fatal("error when unmarshal config from json file:", err)
return
}
}
// if we have configuration from json file
if len(config.ApiBaseURI) > 0 {
apiURI = config.ApiBaseURI
} else {
log.Println("please input api base URI,such as:https://api.kumex.com")
log.Println("please input api base URI,such as:https://api.kucoin.com")
fmt.Scanf("%s", &apiURI)
}

Expand Down Expand Up @@ -91,6 +92,7 @@ func main() {
call(apiMap, apiName, params)
}

// initial api configuration information from json file.
type ApiConfig struct {
ApiBaseURI string `json:ApiBaseURI`
ApiKey string `json:ApiKey`
Expand Down

0 comments on commit 5a3f56a

Please sign in to comment.