-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
45e45e3
commit 4f57d7a
Showing
6 changed files
with
173 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package client | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/gomaestro-org/go-sdk/utils" | ||
) | ||
|
||
type OperationalCertificate struct { | ||
HotVkey string `json:"hot_vkey"` | ||
KesPeriod int64 `json:"kes_period"` | ||
KesSignature string `json:"kes_signature"` | ||
SequenceNumber int64 `json:"sequence_number"` | ||
} | ||
|
||
type TotalExUnits struct { | ||
Mem int64 `json:"mem"` | ||
Steps int64 `json:"steps"` | ||
} | ||
|
||
type BlockInfo struct { | ||
Data struct { | ||
AbsoluteSlot int64 `json:"absolute_slot"` | ||
BlockProducer string `json:"block_producer"` | ||
Confirmations int64 `json:"confirmations"` | ||
Epoch int64 `json:"epoch"` | ||
EpochSlot int64 `json:"epoch_slot"` | ||
Era string `json:"era"` | ||
Hash string `json:"hash"` | ||
Height string `json:"height"` | ||
OperationalCertificate OperationalCertificate `json:"operational_certificate"` | ||
PreviousBlock string `json:"previous_block"` | ||
ProtocolVersion []int `json:"protocol_version"` | ||
ScriptInvocations int32 `json:"script_invocations"` | ||
Size int32 `json:"size"` | ||
Timestamp string `json:"timestamp"` | ||
TotalExUnits TotalExUnits `json:"total_ex_units"` | ||
TotalFees int64 `json:"total_fees"` | ||
TotalOutputLovelace []string `json:"total_output_lovelace"` | ||
TxHashes []string `json:"tx_hashes"` | ||
VrfKey string `json:"vrf_key"` | ||
} `json:"data"` | ||
LastUpdated utils.LastUpdated `json:"last_updated"` | ||
} | ||
|
||
func (c *Client) GetBlockInfo(blockHeight int64) (*BlockInfo, error) { | ||
req, err := http.NewRequest("GET", fmt.Sprintf("%s/blocks/%d", c.baseUrl, blockHeight), nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
req.Header.Set("Content-Type", "application/json; charset=utf-8") | ||
|
||
res := BlockInfo{} | ||
if err := c.sendRequest(req, &res); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &res, nil | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package client | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
"path" | ||
"strings" | ||
"time" | ||
|
||
"github.com/gomaestro-org/go-sdk/config" | ||
) | ||
|
||
type Client struct { | ||
apiKey string | ||
network string | ||
version string | ||
HTTPClient *http.Client | ||
baseUrl string | ||
} | ||
|
||
func NewClient(apiKey string, network string) *Client { | ||
cfg := config.GetConfig() | ||
paths := []string{"https://", fmt.Sprintf("%s.gomaestro-api.org", strings.ToLower(network)), cfg.Client.Version} | ||
return &Client{ | ||
apiKey: apiKey, | ||
network: network, | ||
version: cfg.Client.Version, | ||
HTTPClient: &http.Client{ | ||
Timeout: time.Duration(cfg.Client.Timeout), | ||
}, | ||
baseUrl: path.Join(paths...), | ||
} | ||
} | ||
|
||
type errorResponse struct { | ||
Code int `json:"code"` | ||
Message string `json:"message"` | ||
} | ||
|
||
type successResponse struct { | ||
Code int `json:"code"` | ||
Data interface{} `json:"data"` | ||
} | ||
|
||
func (c *Client) sendRequest(req *http.Request, v interface{}) error { | ||
req.Header.Set("Accept", "application/json; charset=utf-8") | ||
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.apiKey)) | ||
|
||
res, err := c.HTTPClient.Do(req) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
defer res.Body.Close() | ||
|
||
// Try to unmarshall into errorResponse | ||
if res.StatusCode != http.StatusOK { | ||
var errRes errorResponse | ||
if err = json.NewDecoder(res.Body).Decode(&errRes); err == nil { | ||
return errors.New(errRes.Message) | ||
} | ||
|
||
return fmt.Errorf("unknown error, status code: %d", res.StatusCode) | ||
} | ||
|
||
// Unmarshall and populate v | ||
fullResponse := successResponse{ | ||
Data: v, | ||
} | ||
if err = json.NewDecoder(res.Body).Decode(&fullResponse); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package config | ||
|
||
type Config struct { | ||
Client ClientConfig | ||
} | ||
|
||
type ClientConfig struct { | ||
Version string | ||
Timeout int64 | ||
} | ||
|
||
var globalConfig = &Config{ | ||
Client: ClientConfig{ | ||
Version: "v1", | ||
Timeout: 10, | ||
}, | ||
} | ||
|
||
func GetConfig() *Config { | ||
return globalConfig | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package utils | ||
|
||
type LastUpdated struct { | ||
BlockHash string `json:"block_hash"` | ||
BlockSlot int64 `json:"block_slot"` | ||
Timestamp string `json:"timestamp"` | ||
} |