fetch.go 1017 Bytes
Newer Older
Loïck Bonniot's avatar
Loïck Bonniot committed
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
package sign

import (
	"io/ioutil"

	"dfss/dfssc/common"
	"dfss/dfssc/security"
	"dfss/dfssp/api"
	"dfss/net"
	"golang.org/x/net/context"
)

// FetchContract tries to download contract metadata from specified uuid, and stores the resulting json at path
func FetchContract(fileCA, fileCert, fileKey, addrPort, passphrase, uuid, path string) error {
	auth := security.NewAuthContainer(fileCA, fileCert, fileKey, addrPort, passphrase)
	ca, cert, key, err := auth.LoadFiles()
	if err != nil {
		return err
	}

	conn, err := net.Connect(auth.AddrPort, cert, key, ca)
	if err != nil {
		return err
	}

	request := &api.GetContractRequest{
		Uuid: uuid,
	}
	client := api.NewPlatformClient(conn)
30
	ctx, cancel := context.WithTimeout(context.Background(), net.DefaultTimeout)
Loïck Bonniot's avatar
Loïck Bonniot committed
31
32
33
34
35
36
37
38
39
40
41
42
43
	defer cancel()
	response, err := client.GetContract(ctx, request)
	if err != nil {
		return err
	}

	err = common.EvaluateErrorCodeResponse(response.ErrorCode)
	if err != nil {
		return err
	}

	return ioutil.WriteFile(path, response.Json, 0600)
}