Skip to content

Commit

Permalink
create transaction type is now working
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfpack94 committed Jul 17, 2019
1 parent e1f645e commit 6f31c9d
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 52 deletions.
101 changes: 56 additions & 45 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,13 +249,13 @@ func (client *Client) DeleteContract(smartContractID string) (*Response, error)
func (client *Client) GetTransaction(txnID string) (*Response, error) {
path := "/transaction"
uri := fmt.Sprintf("%s%s/%s", client.apiBaseURL, path, txnID)
fmt.Println(uri)
body := []byte("")

req, err := http.NewRequest("GET", uri, bytes.NewBuffer([]byte("")))
req, err := http.NewRequest("GET", uri, bytes.NewBuffer(body))
if err != nil {
return nil, err
}
resp, err := client.performRequest(req, []byte(""))
resp, err := client.performRequest(req, body)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -285,9 +285,8 @@ func (client *Client) CreateTransaction(txn *CreateTransaction) (_ *Response, er
if err != nil {
return nil, err
}
fmt.Printf("before typing: %s\n", string(resp.Response.([]byte)))
// Handle conversion of Response from an interface{} to Transaction for the user.
transaction := make(map[string]CreateTransactionResponse)
var transaction CreateTransactionResponse
if err := json.Unmarshal(resp.Response.([]byte), &transaction); err != nil {
return nil, err
}
Expand All @@ -309,35 +308,38 @@ func (client *Client) CreateBulkTransaction(txn []*CreateTransaction) (_ *Respon
return nil, err
}

resp, err := client.httpClient.Post(uri, "content/json", bytes.NewBuffer(txnBytes))
req, err := http.NewRequest("POST", uri, bytes.NewBuffer(txnBytes))
if err != nil {
return nil, err
}
defer resp.Body.Close()
var statusMessage []byte
statusMessage, err = ioutil.ReadAll(resp.Body)

resp, err := client.performRequest(req, txnBytes)
if err != nil {
return nil, err
}
response := Response{
Response: statusMessage,
Status: resp.StatusCode,
OK: 200 <= resp.StatusCode && 300 > resp.StatusCode,

var response CreateBulkTransactionResponse
if err := json.Unmarshal(resp.Response.([]byte), &response); err != nil {
return nil, err
}
return &response, err

resp.Response = response

return resp, err
}

// QueryBlocks gets all blocks matching the given query.
func (client *Client) QueryBlocks(query *Query) (*Response, error) {
path := "/block"
uri := fmt.Sprintf("%s%s", client.apiBaseURL, path)
body := []byte("")

req, err := http.NewRequest("GET", uri, bytes.NewBuffer([]byte("")))
req, err := http.NewRequest("GET", uri, bytes.NewBuffer(body))
if err != nil {
return nil, err
}
buildQuery(req, query)
resp, err := client.performRequest(req, []byte(""))
resp, err := client.performRequest(req, body)
if err != nil {
return nil, err
}
Expand All @@ -354,12 +356,13 @@ func (client *Client) QueryBlocks(query *Query) (*Response, error) {
func (client *Client) GetBlock(blockID string) (*Response, error) {
path := "/block"
uri := fmt.Sprintf("%s%s/%s", client.apiBaseURL, path, blockID)
body := []byte("")

req, err := http.NewRequest("GET", uri, bytes.NewBuffer([]byte("")))
req, err := http.NewRequest("GET", uri, bytes.NewBuffer(body))
if err != nil {
return nil, err
}
resp, err := client.performRequest(req, []byte(""))
resp, err := client.performRequest(req, body)
if err != nil {
return nil, err
}
Expand All @@ -375,8 +378,9 @@ func (client *Client) GetBlock(blockID string) (*Response, error) {
func (client *Client) GetVerifications(blockID string, level int) (*Response, error) {
path := "/verifications"
uri := fmt.Sprintf("%s%s/%s", client.apiBaseURL, path, blockID)
body := []byte("")

req, err := http.NewRequest("GET", uri, bytes.NewBuffer([]byte("")))
req, err := http.NewRequest("GET", uri, bytes.NewBuffer(body))
if err != nil {
return nil, err
}
Expand All @@ -385,7 +389,7 @@ func (client *Client) GetVerifications(blockID string, level int) (*Response, er
q.Add("level", string(level))
req.URL.RawQuery = q.Encode()
}
resp, err := client.performRequest(req, []byte(""))
resp, err := client.performRequest(req, body)
if err != nil {
return nil, err
}
Expand All @@ -410,14 +414,15 @@ func (client *Client) GetVerifications(blockID string, level int) (*Response, er
func (client *Client) QueryTransactions(query *Query) (*Response, error) {
path := "/transaction"
uri := fmt.Sprintf("%s%s", client.apiBaseURL, path)
body := []byte("")

req, err := http.NewRequest("GET", uri, bytes.NewBuffer([]byte("")))
req, err := http.NewRequest("GET", uri, bytes.NewBuffer(body))
if err != nil {
return nil, err
}

buildQuery(req, query)
resp, err := client.performRequest(req, []byte(""))
resp, err := client.performRequest(req, body)
if err != nil {
return nil, err
}
Expand All @@ -437,12 +442,13 @@ func (client *Client) GetSmartContractObject(key, smartContractID string) (*Resp

path := "/get"
uri := fmt.Sprintf("%s%s/%s/%s", client.apiBaseURL, path, smartContractID, key)
body := []byte("")

req, err := http.NewRequest("GET", uri, bytes.NewBuffer([]byte("")))
req, err := http.NewRequest("GET", uri, bytes.NewBuffer(body))
if err != nil {
return nil, err
}
resp, err := client.performRequest(req, []byte(""))
resp, err := client.performRequest(req, body)
if err != nil {
return nil, err
}
Expand All @@ -458,6 +464,7 @@ func (client *Client) ListSmartContractObjects(folder, smartContractID string) (

path := "/list"
uri := fmt.Sprintf("%s%s/%s/", client.apiBaseURL, path, smartContractID)
body := []byte("")

if len(folder) > 0 {
if strings.HasSuffix(folder, "/") {
Expand All @@ -466,11 +473,11 @@ func (client *Client) ListSmartContractObjects(folder, smartContractID string) (
uri = fmt.Sprintf("%s%s", uri, folder)
}

req, err := http.NewRequest("GET", uri, bytes.NewBuffer([]byte("")))
req, err := http.NewRequest("GET", uri, bytes.NewBuffer(body))
if err != nil {
return nil, err
}
resp, err := client.performRequest(req, []byte(""))
resp, err := client.performRequest(req, body)
if err != nil {
return nil, err
}
Expand All @@ -481,12 +488,13 @@ func (client *Client) ListSmartContractObjects(folder, smartContractID string) (
func (client *Client) GetTransactionType(transactionType string) (*Response, error) {
path := "/transaction-type"
uri := fmt.Sprintf("%s%s/%s", client.apiBaseURL, path, transactionType)
body := []byte("")

req, err := http.NewRequest("GET", uri, bytes.NewBuffer([]byte("")))
req, err := http.NewRequest("GET", uri, bytes.NewBuffer(body))
if err != nil {
return nil, err
}
resp, err := client.performRequest(req, []byte(""))
resp, err := client.performRequest(req, body)
if err != nil {
return nil, err
}
Expand All @@ -502,12 +510,13 @@ func (client *Client) GetTransactionType(transactionType string) (*Response, err
func (client *Client) ListTransactionTypes() (*Response, error) {
path := "/transaction-types"
uri := fmt.Sprintf("%s%s", client.apiBaseURL, path)
body := []byte("")

req, err := http.NewRequest("GET", uri, bytes.NewBuffer([]byte("")))
req, err := http.NewRequest("GET", uri, bytes.NewBuffer(body))
if err != nil {
return nil, err
}
resp, err := client.performRequest(req, []byte(""))
resp, err := client.performRequest(req, body)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -552,34 +561,37 @@ func (client *Client) UpdateTransactionType(transactionType string, customIndexe
func (client *Client) CreateTransactionType(transactionType string, customIndexes []CustomIndexStructure) (_ *Response, err error) {
path := "/transaction-type"
uri := fmt.Sprintf("%s%s", client.apiBaseURL, path)

var params TransactionType
params.Version = "1"
params.Type = transactionType
params.CustomIndexes = customIndexes
if len(customIndexes) > 0 {
params.CustomIndexes = customIndexes
}

b, err := json.Marshal(params)
if err != nil {
return nil, err
}

resp, err := client.httpClient.Post(uri, "content/json", bytes.NewBuffer(b))
req, err := http.NewRequest("POST", uri, bytes.NewBuffer(b))
if err != nil {
return nil, err
}
defer resp.Body.Close()
var chainResp Response
respBytes, err := ioutil.ReadAll(resp.Body)

resp, err := client.performRequest(req, b)
if err != nil {
return nil, err
}
if err := json.Unmarshal(respBytes, &chainResp.Response); err != nil {

var success map[string]bool
if err := json.Unmarshal(resp.Response.([]byte), &success); err != nil {
return nil, err
}
chainResp.Status = resp.StatusCode
if 200 <= resp.StatusCode && resp.StatusCode < 300 {
chainResp.OK = true
}
return &chainResp, err

resp.Response = success

return resp, err
}

// DeleteTransactionType removes the specified transaction type. It will not affect transactions that have already been processed.
Expand Down Expand Up @@ -736,12 +748,13 @@ func (client *Client) GetAPIKey(keyID string) (*Response, error) {
// ListAPIKeys for a chain.
func (client *Client) ListAPIKeys() (*Response, error) {
uri := fmt.Sprintf("%s%s", client.apiBaseURL, "/api-key")
body := []byte("")

req, err := http.NewRequest("GET", uri, bytes.NewBuffer([]byte("")))
req, err := http.NewRequest("GET", uri, bytes.NewBuffer(body))
if err != nil {
return nil, err
}
resp, err := client.performRequest(req, []byte(""))
resp, err := client.performRequest(req, body)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -770,7 +783,6 @@ func (client *Client) CreateAPIKey(configuration *APIKeyConfiguration) (*Respons
if err != nil {
return nil, err
}
fmt.Printf("HERE: %+v\n", string(resp.Response.([]byte)))
var apiKey APIKey
if err := json.Unmarshal(resp.Response.([]byte), &apiKey); err != nil {
return nil, err
Expand Down Expand Up @@ -839,7 +851,6 @@ func (client *Client) setHeaders(req *http.Request, httpVerb, path, contentType
req.Header.Set("Dragonchain", client.creds.GetDragonchainID())
req.Header.Set("Timestamp", fmt.Sprintf("%s", now))
req.Header.Set("Authorization", client.creds.GetAuthorization(httpVerb, path, now, contentType, content))
fmt.Printf("%+v\n", req.Header)
return nil
}

Expand Down
6 changes: 2 additions & 4 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,8 @@ import (
func TestMain(t *testing.T) {
creds, _ := NewCredentials("23qqLjVBWpgeZHf9gXRCernSDMXk8TbmPR9w2DnqTiijx", "haf4Ku9Pk5AfZSHKI3F5ZSZycrh2RzhgPR1nqw2WLey", "EDGJEFKFBXGK", "")
client := NewClient(creds, "https://281bd112-2dda-49f4-97a3-6456c08c5b2e.api.dragonchain.com", nil)
apiKey := &APIKeyConfiguration{
Nickname: "banana",
}
resp, _ := client.CreateAPIKey(apiKey)

resp, _ := client.CreateTransactionType("banana", []CustomIndexStructure{})
fmt.Printf("RESPONSE: %+v\n", resp.Response)
}

Expand Down
11 changes: 8 additions & 3 deletions transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,13 @@ type Transaction struct {

// CreateTransactionResponse defines the response from creating a transaction
type CreateTransactionResponse struct {
TransactionID string `json:transaction_id,omitempty`
TransactionID string `json:"transaction_id"`
}

// CreateBulkTransactionResponse defines the response from creating a bulk transaction
type CreateBulkTransactionResponse struct {
Valid []string `json:"201,omitempty"`
Invalid []interface{} `json:"400,omitempty"`
}

// GetSmartContractHeap defines the request format for getting a key from a Smart Contract's heap.
Expand All @@ -60,8 +66,7 @@ type GetSmartContractHeap struct {
type TransactionType struct {
Version string `json:"version"`
Type string `json:"txn_type"`
CustomIndexes []CustomIndexStructure `json:"custom_indexes"`
IsContract bool `json:"is_contract"`
CustomIndexes []CustomIndexStructure `json:"custom_indexes,omitempty"`
}

// CustomIndexStructure defines the valid format of custom indexes on a transaction type.
Expand Down

0 comments on commit 6f31c9d

Please sign in to comment.