|
| 1 | +package sdk |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "github.com/dustin/go-humanize" |
| 7 | + "time" |
| 8 | +) |
| 9 | + |
| 10 | +type TokenLocation struct { |
| 11 | + City string |
| 12 | + Region string |
| 13 | + Country string |
| 14 | + TimeZone string |
| 15 | + CountryName string |
| 16 | +} |
| 17 | + |
| 18 | +type TokenData struct { |
| 19 | + ID string |
| 20 | + Token string |
| 21 | + Source string |
| 22 | + Label string |
| 23 | + Icon string |
| 24 | + Ip string |
| 25 | + Agent string |
| 26 | + IsCurrent bool |
| 27 | + IsIssused bool |
| 28 | + Location TokenLocation |
| 29 | + CreatedAt string `json:"created_at"` |
| 30 | + UpdatedAt string `json:"updated_at"` |
| 31 | +} |
| 32 | + |
| 33 | +type TokenResult struct { |
| 34 | + Benchmark float64 `json:"_benchmark"` |
| 35 | + Meta struct { |
| 36 | + Timestamp string `json:"timestamp"` |
| 37 | + Limit int `json:"limit"` |
| 38 | + TotalDocuments float64 `json:"total_documents"` |
| 39 | + Sort string `json:"sort"` |
| 40 | + Order string `json:"order"` |
| 41 | + Page int `json:"page"` |
| 42 | + TotalPages int `json:"total_pages"` |
| 43 | + MaxPages int `json:"max_pages"` |
| 44 | + FirstItem int `json:"first_item"` |
| 45 | + LastItem int `json:"last_item"` |
| 46 | + } `json:"_meta"` |
| 47 | + Data []TokenData |
| 48 | +} |
| 49 | + |
| 50 | +type TokenResponse struct { |
| 51 | + Benchmark float64 `json:"_benchmark"` |
| 52 | + Message string `json:"message"` |
| 53 | + Success bool `json:"success"` |
| 54 | + Type string `json:"type"` |
| 55 | + Data TokenData `json:"data"` |
| 56 | +} |
| 57 | + |
| 58 | +func (c *Client) GetTokens() (responseJSON *TokenResult, err error) { |
| 59 | + resp, err := c.Request("GET", "/token?limit=100") |
| 60 | + if err != nil { |
| 61 | + return nil, err |
| 62 | + } |
| 63 | + |
| 64 | + defer resp.Body.Close() |
| 65 | + _ = json.NewDecoder(resp.Body).Decode(&responseJSON) |
| 66 | + return responseJSON, nil |
| 67 | +} |
| 68 | + |
| 69 | +func (c *Client) CreateToken(label string) (responseJSON *TokenResponse, err error) { |
| 70 | + resp, err := c.Form("label", label).Form("icon", "i-vc-icon").Request("POST", "/token") |
| 71 | + if err != nil { |
| 72 | + return nil, err |
| 73 | + } |
| 74 | + |
| 75 | + defer resp.Body.Close() |
| 76 | + _ = json.NewDecoder(resp.Body).Decode(&responseJSON) |
| 77 | + return responseJSON, nil |
| 78 | +} |
| 79 | + |
| 80 | +func (c *Client) DeleteToken(ID string) (responseJSON *TokenResponse, err error) { |
| 81 | + resp, err := c.Request("DELETE", "/token/"+ID) |
| 82 | + if err != nil { |
| 83 | + return nil, err |
| 84 | + } |
| 85 | + defer resp.Body.Close() |
| 86 | + _ = json.NewDecoder(resp.Body).Decode(&responseJSON) |
| 87 | + return responseJSON, nil |
| 88 | +} |
| 89 | + |
| 90 | +// GetData - Returns the data from the response |
| 91 | +func (r TokenResult) GetData() []TokenData { |
| 92 | + return r.Data |
| 93 | +} |
| 94 | + |
| 95 | +// GetHumanUpdatedAt - convert 2024-09-03T23:09:14.574Z to "8 hours ago" |
| 96 | +func (t TokenData) GetHumanUpdatedAt() string { |
| 97 | + updatedAt, err := time.Parse(time.RFC3339, t.UpdatedAt) |
| 98 | + if err != nil { |
| 99 | + return "Unknown" |
| 100 | + } |
| 101 | + return humanize.Time(updatedAt) |
| 102 | +} |
| 103 | + |
| 104 | +// GetLocationString - Return either Unknown or Austin, TX US |
| 105 | +func (t TokenData) GetLocationString() string { |
| 106 | + if t.Location.City == "" { |
| 107 | + return "Unknown" |
| 108 | + } |
| 109 | + return t.Location.City + ", " + t.Location.Region + " " + t.Location.Country |
| 110 | +} |
| 111 | + |
| 112 | +func (t TokenData) GetSourceLabel() string { |
| 113 | + if t.Label != "" { |
| 114 | + return fmt.Sprintf("%s (%s)", t.Source, t.Label) |
| 115 | + } |
| 116 | + return t.Source |
| 117 | +} |
0 commit comments