mirror of
https://github.com/rodneyosodo/memfault-go.git
synced 2026-08-07 07:14:46 +00:00
Format code with gofmt and gofumpt
This commit fixes the style issues introduced in 18bf45e according to the output
from gofmt and gofumpt.
Details: https://deepsource.io/gh/0x6f736f646f/memfault-go/transform/a839d37e-6490-41eb-868f-c29e114412f5/
This commit is contained in:
committed by
GitHub
parent
cf9893fae0
commit
ef91e0e804
@@ -10,8 +10,6 @@ import (
|
|||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// rootCmd represents the base command when called without any subcommands
|
// rootCmd represents the base command when called without any subcommands
|
||||||
var rootCmd = &cobra.Command{
|
var rootCmd = &cobra.Command{
|
||||||
Use: "cli",
|
Use: "cli",
|
||||||
@@ -47,5 +45,3 @@ func init() {
|
|||||||
// when this action is called directly.
|
// when this action is called directly.
|
||||||
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,78 +1,78 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
mem "github.com/0x6f736f646f/memfault-go/pkg/memfault"
|
mem "github.com/0x6f736f646f/memfault-go/pkg/memfault"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
username = os.Getenv("MEMFAULT_USERNAME")
|
username = os.Getenv("MEMFAULT_USERNAME")
|
||||||
password = os.Getenv("MEMFAULT_PASSWORD")
|
password = os.Getenv("MEMFAULT_PASSWORD")
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
creds := mem.Credentials{
|
creds := mem.Credentials{
|
||||||
Email: username,
|
Email: username,
|
||||||
Password: password,
|
Password: password,
|
||||||
}
|
}
|
||||||
d, _ := time.ParseDuration("30s")
|
d, _ := time.ParseDuration("30s")
|
||||||
conf := mem.Config{
|
conf := mem.Config{
|
||||||
APIURL: "https://api.memfault.com",
|
APIURL: "https://api.memfault.com",
|
||||||
Credentials: creds,
|
Credentials: creds,
|
||||||
MaxIdleConns: 10,
|
MaxIdleConns: 10,
|
||||||
IdleConnTimeout: d,
|
IdleConnTimeout: d,
|
||||||
}
|
}
|
||||||
memfault := mem.NewSDK(conf)
|
memfault := mem.NewSDK(conf)
|
||||||
|
|
||||||
payload := mem.Project{
|
payload := mem.Project{
|
||||||
Name: "SmartSdinkamakeabc",
|
Name: "SmartSdinkamakeabc",
|
||||||
Slug: "smartsdinkmskeabc",
|
Slug: "smartsdinkmskeabc",
|
||||||
Os: "FreeRTOS",
|
Os: "FreeRTOS",
|
||||||
Platform: "nRF52",
|
Platform: "nRF52",
|
||||||
}
|
}
|
||||||
response1, err := memfault.CreateProject(payload)
|
response1, err := memfault.CreateProject(payload)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
fmt.Println(response1.Data.APIKey)
|
fmt.Println(response1.Data.APIKey)
|
||||||
|
|
||||||
response2, err := memfault.ListProject()
|
response2, err := memfault.ListProject()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
fmt.Println(response2.Data[0].Slug)
|
fmt.Println(response2.Data[0].Slug)
|
||||||
|
|
||||||
response3, err := memfault.RetrieveProject(response2.Data[0].Slug)
|
response3, err := memfault.RetrieveProject(response2.Data[0].Slug)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
fmt.Println(response3.Data.Slug)
|
fmt.Println(response3.Data.Slug)
|
||||||
|
|
||||||
response4, err := memfault.UpdateProject(payload)
|
response4, err := memfault.UpdateProject(payload)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
fmt.Println(response4.Data.APIKey)
|
fmt.Println(response4.Data.APIKey)
|
||||||
|
|
||||||
response6, err := memfault.GetProjectClientKey(response2.Data[0].Slug)
|
response6, err := memfault.GetProjectClientKey(response2.Data[0].Slug)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
fmt.Println(response6)
|
fmt.Println(response6)
|
||||||
|
|
||||||
response7, err := memfault.RefreshProjectClientKey(response2.Data[0].Slug)
|
response7, err := memfault.RefreshProjectClientKey(response2.Data[0].Slug)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println(response7)
|
fmt.Println(response7)
|
||||||
|
|
||||||
response5, err := memfault.DeleteProject(payload)
|
response5, err := memfault.DeleteProject(payload)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
fmt.Println(response5)
|
fmt.Println(response5)
|
||||||
}
|
}
|
||||||
|
|||||||
+52
-52
@@ -1,68 +1,68 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
mem "github.com/0x6f736f646f/memfault-go/pkg/memfault"
|
mem "github.com/0x6f736f646f/memfault-go/pkg/memfault"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
username = os.Getenv("MEMFAULT_USERNAME")
|
username = os.Getenv("MEMFAULT_USERNAME")
|
||||||
password = os.Getenv("MEMFAULT_PASSWORD")
|
password = os.Getenv("MEMFAULT_PASSWORD")
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
creds := mem.Credentials{
|
creds := mem.Credentials{
|
||||||
Email: username,
|
Email: username,
|
||||||
Password: password,
|
Password: password,
|
||||||
}
|
}
|
||||||
d, _ := time.ParseDuration("30s")
|
d, _ := time.ParseDuration("30s")
|
||||||
conf := mem.Config{
|
conf := mem.Config{
|
||||||
APIURL: "https://api.memfault.com",
|
APIURL: "https://api.memfault.com",
|
||||||
Credentials: creds,
|
Credentials: creds,
|
||||||
MaxIdleConns: 10,
|
MaxIdleConns: 10,
|
||||||
IdleConnTimeout: d,
|
IdleConnTimeout: d,
|
||||||
}
|
}
|
||||||
memfault := mem.NewSDK(conf)
|
memfault := mem.NewSDK(conf)
|
||||||
response1, err := memfault.GetMe()
|
response1, err := memfault.GetMe()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
// Prints out the user email address
|
// Prints out the user email address
|
||||||
fmt.Println(response1.Email)
|
fmt.Println(response1.Email)
|
||||||
|
|
||||||
response2, err := memfault.GenerateUserAPIKey()
|
response2, err := memfault.GenerateUserAPIKey()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
// Prints out the apikey for the logged in user
|
// Prints out the apikey for the logged in user
|
||||||
fmt.Println(response2.Data.APIKey)
|
fmt.Println(response2.Data.APIKey)
|
||||||
|
|
||||||
response3, err := memfault.GetUserAPIKey()
|
response3, err := memfault.GetUserAPIKey()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
// Prints out the apikey for the logged in user
|
// Prints out the apikey for the logged in user
|
||||||
fmt.Println(response3.Data.APIKey)
|
fmt.Println(response3.Data.APIKey)
|
||||||
|
|
||||||
response4, err := memfault.DeleteUserAPIKey()
|
response4, err := memfault.DeleteUserAPIKey()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
// print out the response `DELETED`
|
// print out the response `DELETED`
|
||||||
fmt.Println(response4)
|
fmt.Println(response4)
|
||||||
|
|
||||||
// response5, err := memfault.getOrganizationSlug(false)
|
// response5, err := memfault.getOrganizationSlug(false)
|
||||||
// if err != nil {
|
// if err != nil {
|
||||||
// fmt.Println(err)
|
// fmt.Println(err)
|
||||||
// }
|
// }
|
||||||
// fmt.Println(fmt.Sprint(response5))
|
// fmt.Println(fmt.Sprint(response5))
|
||||||
|
|
||||||
// response5, err := memfault.getOrganizationSlug(true)
|
// response5, err := memfault.getOrganizationSlug(true)
|
||||||
// if err != nil {
|
// if err != nil {
|
||||||
// fmt.Println(err)
|
// fmt.Println(err)
|
||||||
// }
|
// }
|
||||||
// fmt.Println(fmt.Sprint(response5))
|
// fmt.Println(fmt.Sprint(response5))
|
||||||
}
|
}
|
||||||
|
|||||||
+62
-63
@@ -1,90 +1,89 @@
|
|||||||
package memfault
|
package memfault
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// GetMe Return information about the logged in User
|
// GetMe Return information about the logged in User
|
||||||
func (sdk mfSDK) GetMe() (UserRes, error) {
|
func (sdk mfSDK) GetMe() (UserRes, error) {
|
||||||
var ur UserRes
|
var ur UserRes
|
||||||
endpoint := "auth/me"
|
endpoint := "auth/me"
|
||||||
url := fmt.Sprintf("%s/%s", sdk.apiURL, endpoint)
|
url := fmt.Sprintf("%s/%s", sdk.apiURL, endpoint)
|
||||||
|
|
||||||
req, err := http.NewRequest(http.MethodGet, url, nil)
|
|
||||||
if err != nil {
|
|
||||||
return ur, err
|
|
||||||
}
|
|
||||||
resp, err := sdk.makeRequest(req)
|
|
||||||
if err != nil {
|
|
||||||
return ur, err
|
|
||||||
}
|
|
||||||
if err := json.Unmarshal(resp, &ur); err != nil {
|
|
||||||
return ur, err
|
|
||||||
}
|
|
||||||
return ur, err
|
|
||||||
|
|
||||||
|
req, err := http.NewRequest(http.MethodGet, url, nil)
|
||||||
|
if err != nil {
|
||||||
|
return ur, err
|
||||||
|
}
|
||||||
|
resp, err := sdk.makeRequest(req)
|
||||||
|
if err != nil {
|
||||||
|
return ur, err
|
||||||
|
}
|
||||||
|
if err := json.Unmarshal(resp, &ur); err != nil {
|
||||||
|
return ur, err
|
||||||
|
}
|
||||||
|
return ur, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// GenerateUserAPIKey generates (or re-generate) an API Key for logged in User.
|
// GenerateUserAPIKey generates (or re-generate) an API Key for logged in User.
|
||||||
// To generate this without logging into Memfault, you may use HTTP Basic Auth to call this API.
|
// To generate this without logging into Memfault, you may use HTTP Basic Auth to call this API.
|
||||||
func (sdk mfSDK) GenerateUserAPIKey() (UserAPIKeyRes, error) {
|
func (sdk mfSDK) GenerateUserAPIKey() (UserAPIKeyRes, error) {
|
||||||
var akr UserAPIKeyRes
|
var akr UserAPIKeyRes
|
||||||
endpoint := "auth/api_key"
|
endpoint := "auth/api_key"
|
||||||
url := fmt.Sprintf("%s/%s", sdk.apiURL, endpoint)
|
url := fmt.Sprintf("%s/%s", sdk.apiURL, endpoint)
|
||||||
payload := strings.NewReader(``)
|
payload := strings.NewReader(``)
|
||||||
|
|
||||||
req, err := http.NewRequest(http.MethodPost, url, payload)
|
req, err := http.NewRequest(http.MethodPost, url, payload)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return akr, err
|
return akr, err
|
||||||
}
|
}
|
||||||
resp, err := sdk.makeRequest(req)
|
resp, err := sdk.makeRequest(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return akr, err
|
return akr, err
|
||||||
}
|
}
|
||||||
if err := json.Unmarshal(resp, &akr); err != nil {
|
if err := json.Unmarshal(resp, &akr); err != nil {
|
||||||
return akr, err
|
return akr, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return akr, nil
|
return akr, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetUserAPIKey Get a previously generated API Key for the logged in User
|
// GetUserAPIKey Get a previously generated API Key for the logged in User
|
||||||
func (sdk mfSDK) GetUserAPIKey() (UserAPIKeyRes, error) {
|
func (sdk mfSDK) GetUserAPIKey() (UserAPIKeyRes, error) {
|
||||||
var akr UserAPIKeyRes
|
var akr UserAPIKeyRes
|
||||||
endpoint := "auth/api_key"
|
endpoint := "auth/api_key"
|
||||||
url := fmt.Sprintf("%s/%s", sdk.apiURL, endpoint)
|
url := fmt.Sprintf("%s/%s", sdk.apiURL, endpoint)
|
||||||
|
|
||||||
req, err := http.NewRequest(http.MethodGet, url, nil)
|
req, err := http.NewRequest(http.MethodGet, url, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return akr, err
|
return akr, err
|
||||||
}
|
}
|
||||||
resp, err := sdk.makeRequest(req)
|
resp, err := sdk.makeRequest(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return akr, err
|
return akr, err
|
||||||
}
|
}
|
||||||
if err := json.Unmarshal(resp, &akr); err != nil {
|
if err := json.Unmarshal(resp, &akr); err != nil {
|
||||||
return akr, err
|
return akr, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return akr, nil
|
return akr, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteUserAPIKey Invalidate the previously generated API Key for the logged in User and do not create another one
|
// DeleteUserAPIKey Invalidate the previously generated API Key for the logged in User and do not create another one
|
||||||
func (sdk mfSDK) DeleteUserAPIKey() (string, error) {
|
func (sdk mfSDK) DeleteUserAPIKey() (string, error) {
|
||||||
endpoint := "auth/api_key"
|
endpoint := "auth/api_key"
|
||||||
url := fmt.Sprintf("%s/%s", sdk.apiURL, endpoint)
|
url := fmt.Sprintf("%s/%s", sdk.apiURL, endpoint)
|
||||||
payload := strings.NewReader(``)
|
payload := strings.NewReader(``)
|
||||||
|
|
||||||
req, err := http.NewRequest(http.MethodDelete, url, payload)
|
req, err := http.NewRequest(http.MethodDelete, url, payload)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
_, err = sdk.makeRequest(req)
|
_, err = sdk.makeRequest(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
return "DELETED", nil
|
return "DELETED", nil
|
||||||
}
|
}
|
||||||
|
|||||||
+144
-144
@@ -4,13 +4,13 @@
|
|||||||
package memfault
|
package memfault
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ContentType represents all possible content types.
|
// ContentType represents all possible content types.
|
||||||
@@ -20,194 +20,194 @@ var _ SDK = (*mfSDK)(nil)
|
|||||||
|
|
||||||
// SDK contains Mainflux API.
|
// SDK contains Mainflux API.
|
||||||
type SDK interface {
|
type SDK interface {
|
||||||
// GetMe Return information about the logged in User
|
// GetMe Return information about the logged in User
|
||||||
GetMe() (UserRes, error)
|
GetMe() (UserRes, error)
|
||||||
|
|
||||||
// Generate a user api key
|
// Generate a user api key
|
||||||
GenerateUserAPIKey() (UserAPIKeyRes, error)
|
GenerateUserAPIKey() (UserAPIKeyRes, error)
|
||||||
|
|
||||||
// Get a previously generated API Key for the logged in User
|
// Get a previously generated API Key for the logged in User
|
||||||
GetUserAPIKey() (UserAPIKeyRes, error)
|
GetUserAPIKey() (UserAPIKeyRes, error)
|
||||||
|
|
||||||
// Invalidate the previously generated API Key for the logged in User and do not create another one
|
// Invalidate the previously generated API Key for the logged in User and do not create another one
|
||||||
DeleteUserAPIKey() (string, error)
|
DeleteUserAPIKey() (string, error)
|
||||||
|
|
||||||
// Creates a Project under the given Organization
|
// Creates a Project under the given Organization
|
||||||
CreateProject(project Project) (CreateProjectRes, error)
|
CreateProject(project Project) (CreateProjectRes, error)
|
||||||
|
|
||||||
// List the Projects under a given Organization
|
// List the Projects under a given Organization
|
||||||
ListProject() (ListProjectRes, error)
|
ListProject() (ListProjectRes, error)
|
||||||
|
|
||||||
// Retrieves a Project under a given Organization
|
// Retrieves a Project under a given Organization
|
||||||
RetrieveProject(projectSlug string) (CreateProjectRes, error)
|
RetrieveProject(projectSlug string) (CreateProjectRes, error)
|
||||||
|
|
||||||
// Update a Project under a given Organization
|
// Update a Project under a given Organization
|
||||||
UpdateProject(project Project) (CreateProjectRes, error)
|
UpdateProject(project Project) (CreateProjectRes, error)
|
||||||
|
|
||||||
// Delete a Project under a given Organization
|
// Delete a Project under a given Organization
|
||||||
DeleteProject(project Project) (string, error)
|
DeleteProject(project Project) (string, error)
|
||||||
|
|
||||||
// Return the Project Client Key
|
// Return the Project Client Key
|
||||||
GetProjectClientKey(projectSlug string) (UserAPIKeyRes, error)
|
GetProjectClientKey(projectSlug string) (UserAPIKeyRes, error)
|
||||||
|
|
||||||
// Regenerate the Project Client Key
|
// Regenerate the Project Client Key
|
||||||
RefreshProjectClientKey(projectSlug string) (UserAPIKeyRes, error)
|
RefreshProjectClientKey(projectSlug string) (UserAPIKeyRes, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Credentials contains the credentials
|
// Credentials contains the credentials
|
||||||
type Credentials struct {
|
type Credentials struct {
|
||||||
Email string `json:"email"`
|
Email string `json:"email"`
|
||||||
Password string `json:"password"`
|
Password string `json:"password"`
|
||||||
APIKey string `json:"api_key"`
|
APIKey string `json:"api_key"`
|
||||||
OrganisationToken string `json:"organisation_token"`
|
OrganisationToken string `json:"organisation_token"`
|
||||||
ProjectKey string `json:"project_key"`
|
ProjectKey string `json:"project_key"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type mfSDK struct {
|
type mfSDK struct {
|
||||||
apiURL string
|
apiURL string
|
||||||
chunksURL string
|
chunksURL string
|
||||||
ingressURL string
|
ingressURL string
|
||||||
filesURL string
|
filesURL string
|
||||||
credentials Credentials
|
credentials Credentials
|
||||||
msgContentType ContentType
|
msgContentType ContentType
|
||||||
client *http.Client
|
client *http.Client
|
||||||
}
|
}
|
||||||
|
|
||||||
// Config contains sdk configuration parameters.
|
// Config contains sdk configuration parameters.
|
||||||
type Config struct {
|
type Config struct {
|
||||||
APIURL string
|
APIURL string
|
||||||
ChunksURL string
|
ChunksURL string
|
||||||
IngressURL string
|
IngressURL string
|
||||||
FilesURL string
|
FilesURL string
|
||||||
Credentials Credentials
|
Credentials Credentials
|
||||||
|
|
||||||
MaxIdleConns int
|
MaxIdleConns int
|
||||||
IdleConnTimeout time.Duration
|
IdleConnTimeout time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
// Project struct
|
// Project struct
|
||||||
type Project struct {
|
type Project struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Slug string `json:"slug"`
|
Slug string `json:"slug"`
|
||||||
Os string `json:"os"`
|
Os string `json:"os"`
|
||||||
Platform string `json:"platform"`
|
Platform string `json:"platform"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewSDK returns new mainflux SDK instance.
|
// NewSDK returns new mainflux SDK instance.
|
||||||
func NewSDK(conf Config) SDK {
|
func NewSDK(conf Config) SDK {
|
||||||
return &mfSDK{
|
return &mfSDK{
|
||||||
apiURL: conf.APIURL,
|
apiURL: conf.APIURL,
|
||||||
chunksURL: conf.ChunksURL,
|
chunksURL: conf.ChunksURL,
|
||||||
ingressURL: conf.IngressURL,
|
ingressURL: conf.IngressURL,
|
||||||
filesURL: conf.FilesURL,
|
filesURL: conf.FilesURL,
|
||||||
credentials: conf.Credentials,
|
credentials: conf.Credentials,
|
||||||
|
|
||||||
client: &http.Client{
|
client: &http.Client{
|
||||||
Transport: &http.Transport{
|
Transport: &http.Transport{
|
||||||
MaxIdleConns: conf.MaxIdleConns,
|
MaxIdleConns: conf.MaxIdleConns,
|
||||||
IdleConnTimeout: conf.IdleConnTimeout,
|
IdleConnTimeout: conf.IdleConnTimeout,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sdk mfSDK) makeRequest(req *http.Request) ([]byte, error) {
|
func (sdk mfSDK) makeRequest(req *http.Request) ([]byte, error) {
|
||||||
token, err := sdk.authenticate()
|
token, err := sdk.authenticate()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
resp, err := sdk.sendRequest(req, token)
|
resp, err := sdk.sendRequest(req, token)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
body, err := ioutil.ReadAll(resp.Body)
|
body, err := ioutil.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
return body, nil
|
return body, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sdk mfSDK) sendRequest(req *http.Request, token string) (*http.Response, error) {
|
func (sdk mfSDK) sendRequest(req *http.Request, token string) (*http.Response, error) {
|
||||||
if token != "" {
|
if token != "" {
|
||||||
req.Header.Add("Authorization", "Basic "+token)
|
req.Header.Add("Authorization", "Basic "+token)
|
||||||
}
|
}
|
||||||
|
|
||||||
req.Header.Add("Content-Type", "application/json")
|
req.Header.Add("Content-Type", "application/json")
|
||||||
|
|
||||||
res, err := sdk.client.Do(req)
|
res, err := sdk.client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sdk mfSDK) authenticate() (string, error) {
|
func (sdk mfSDK) authenticate() (string, error) {
|
||||||
if sdk.credentials.Email != "" && sdk.credentials.Password != "" {
|
if sdk.credentials.Email != "" && sdk.credentials.Password != "" {
|
||||||
auth := []byte(sdk.credentials.Email + ":" + sdk.credentials.Password)
|
auth := []byte(sdk.credentials.Email + ":" + sdk.credentials.Password)
|
||||||
return base64.StdEncoding.EncodeToString(auth), nil
|
return base64.StdEncoding.EncodeToString(auth), nil
|
||||||
} else if sdk.credentials.APIKey != "" && sdk.credentials.Email != "" {
|
} else if sdk.credentials.APIKey != "" && sdk.credentials.Email != "" {
|
||||||
auth := []byte(sdk.credentials.Email + ":" + sdk.credentials.APIKey)
|
auth := []byte(sdk.credentials.Email + ":" + sdk.credentials.APIKey)
|
||||||
return base64.StdEncoding.EncodeToString(auth), nil
|
return base64.StdEncoding.EncodeToString(auth), nil
|
||||||
} else if sdk.credentials.OrganisationToken != "" {
|
} else if sdk.credentials.OrganisationToken != "" {
|
||||||
auth := []byte(":" + sdk.credentials.OrganisationToken)
|
auth := []byte(":" + sdk.credentials.OrganisationToken)
|
||||||
return base64.StdEncoding.EncodeToString(auth), nil
|
return base64.StdEncoding.EncodeToString(auth), nil
|
||||||
}
|
}
|
||||||
return "", errors.New("Empty credentials")
|
return "", errors.New("Empty credentials")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sdk mfSDK) getOrganizationSlug(multiple bool) (string, error) {
|
func (sdk mfSDK) getOrganizationSlug(multiple bool) (string, error) {
|
||||||
endpoint := "auth/me"
|
endpoint := "auth/me"
|
||||||
url := fmt.Sprintf("%s/%s", sdk.apiURL, endpoint)
|
url := fmt.Sprintf("%s/%s", sdk.apiURL, endpoint)
|
||||||
|
|
||||||
req, err := http.NewRequest(http.MethodGet, url, nil)
|
req, err := http.NewRequest(http.MethodGet, url, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
resp, err := sdk.makeRequest(req)
|
resp, err := sdk.makeRequest(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
var ur UserRes
|
var ur UserRes
|
||||||
if err := json.Unmarshal(resp, &ur); err != nil {
|
if err := json.Unmarshal(resp, &ur); err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
if multiple {
|
if multiple {
|
||||||
var list []string
|
var list []string
|
||||||
for _, organization := range ur.Organizations {
|
for _, organization := range ur.Organizations {
|
||||||
list = append(list, organization.Slug)
|
list = append(list, organization.Slug)
|
||||||
}
|
}
|
||||||
organizationslug := fmt.Sprint(list)
|
organizationslug := fmt.Sprint(list)
|
||||||
return organizationslug, nil
|
return organizationslug, nil
|
||||||
}
|
}
|
||||||
return ur.Organizations[0].Slug, nil
|
return ur.Organizations[0].Slug, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sdk mfSDK) getProjectSlugByName(name string) (string, error) {
|
func (sdk mfSDK) getProjectSlugByName(name string) (string, error) {
|
||||||
projects, err := sdk.ListProject()
|
projects, err := sdk.ListProject()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", nil
|
return "", nil
|
||||||
}
|
}
|
||||||
for _, project := range projects.Data {
|
for _, project := range projects.Data {
|
||||||
if name == project.Name {
|
if name == project.Name {
|
||||||
return project.Slug, nil
|
return project.Slug, nil
|
||||||
}
|
}
|
||||||
return "", nil
|
return "", nil
|
||||||
}
|
}
|
||||||
return "", nil
|
return "", nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sdk mfSDK) getProjectSlugByID(id int) (string, error) {
|
func (sdk mfSDK) getProjectSlugByID(id int) (string, error) {
|
||||||
projects, err := sdk.ListProject()
|
projects, err := sdk.ListProject()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", nil
|
return "", nil
|
||||||
}
|
}
|
||||||
for _, project := range projects.Data {
|
for _, project := range projects.Data {
|
||||||
if id == project.ID {
|
if id == project.ID {
|
||||||
return project.Slug, nil
|
return project.Slug, nil
|
||||||
}
|
}
|
||||||
return "", nil
|
return "", nil
|
||||||
}
|
}
|
||||||
return "", nil
|
return "", nil
|
||||||
}
|
}
|
||||||
|
|||||||
+169
-169
@@ -1,214 +1,214 @@
|
|||||||
package memfault
|
package memfault
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// CreateProject creates a Project under the given Organization
|
// CreateProject creates a Project under the given Organization
|
||||||
func (sdk mfSDK) CreateProject(project Project) (CreateProjectRes, error) {
|
func (sdk mfSDK) CreateProject(project Project) (CreateProjectRes, error) {
|
||||||
var exists bool
|
var exists bool
|
||||||
var cpr CreateProjectRes
|
var cpr CreateProjectRes
|
||||||
var cpaer CreateProjectAlreadyExistRes
|
var cpaer CreateProjectAlreadyExistRes
|
||||||
|
|
||||||
slug, err := sdk.getProjectSlugByName(project.Name)
|
slug, err := sdk.getProjectSlugByName(project.Name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return cpr, nil
|
return cpr, nil
|
||||||
}
|
}
|
||||||
if slug == project.Slug {
|
if slug == project.Slug {
|
||||||
exists = true
|
exists = true
|
||||||
}
|
}
|
||||||
data, err := json.Marshal(project)
|
data, err := json.Marshal(project)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return cpr, err
|
return cpr, err
|
||||||
}
|
}
|
||||||
payload := strings.NewReader(string(data))
|
payload := strings.NewReader(string(data))
|
||||||
organizationslug, err := sdk.getOrganizationSlug(false)
|
organizationslug, err := sdk.getOrganizationSlug(false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return cpr, err
|
return cpr, err
|
||||||
}
|
}
|
||||||
endpoint := "api/v0/organizations/" + string(organizationslug) + "/projects"
|
endpoint := "api/v0/organizations/" + string(organizationslug) + "/projects"
|
||||||
url := fmt.Sprintf("%s/%s", sdk.apiURL, endpoint)
|
url := fmt.Sprintf("%s/%s", sdk.apiURL, endpoint)
|
||||||
|
|
||||||
req, err := http.NewRequest(http.MethodPost, url, payload)
|
req, err := http.NewRequest(http.MethodPost, url, payload)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return cpr, err
|
return cpr, err
|
||||||
}
|
}
|
||||||
resp, err := sdk.makeRequest(req)
|
resp, err := sdk.makeRequest(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return cpr, err
|
return cpr, err
|
||||||
}
|
}
|
||||||
if exists {
|
if exists {
|
||||||
if err := json.Unmarshal(resp, &cpaer); err != nil {
|
if err := json.Unmarshal(resp, &cpaer); err != nil {
|
||||||
return cpr, err
|
return cpr, err
|
||||||
}
|
}
|
||||||
return cpr, errors.New(string(cpaer.Error.Message))
|
return cpr, errors.New(string(cpaer.Error.Message))
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := json.Unmarshal(resp, &cpr); err != nil {
|
if err := json.Unmarshal(resp, &cpr); err != nil {
|
||||||
return cpr, err
|
return cpr, err
|
||||||
}
|
}
|
||||||
return cpr, nil
|
return cpr, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ListProject list the Projects under a given Organization
|
// ListProject list the Projects under a given Organization
|
||||||
func (sdk mfSDK) ListProject() (ListProjectRes, error) {
|
func (sdk mfSDK) ListProject() (ListProjectRes, error) {
|
||||||
var lpr ListProjectRes
|
var lpr ListProjectRes
|
||||||
organizationslug, err := sdk.getOrganizationSlug(false)
|
organizationslug, err := sdk.getOrganizationSlug(false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return lpr, err
|
return lpr, err
|
||||||
}
|
}
|
||||||
endpoint := "api/v0/organizations/" + string(organizationslug) + "/projects"
|
endpoint := "api/v0/organizations/" + string(organizationslug) + "/projects"
|
||||||
url := fmt.Sprintf("%s/%s", sdk.apiURL, endpoint)
|
url := fmt.Sprintf("%s/%s", sdk.apiURL, endpoint)
|
||||||
|
|
||||||
req, err := http.NewRequest(http.MethodGet, url, nil)
|
req, err := http.NewRequest(http.MethodGet, url, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return lpr, err
|
return lpr, err
|
||||||
}
|
}
|
||||||
resp, err := sdk.makeRequest(req)
|
resp, err := sdk.makeRequest(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return lpr, err
|
return lpr, err
|
||||||
}
|
}
|
||||||
if err := json.Unmarshal(resp, &lpr); err != nil {
|
if err := json.Unmarshal(resp, &lpr); err != nil {
|
||||||
return lpr, err
|
return lpr, err
|
||||||
}
|
}
|
||||||
return lpr, nil
|
return lpr, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// RetrieveProject retrieve a Project under a given Organization
|
// RetrieveProject retrieve a Project under a given Organization
|
||||||
func (sdk mfSDK) RetrieveProject(projectSlug string) (CreateProjectRes, error) {
|
func (sdk mfSDK) RetrieveProject(projectSlug string) (CreateProjectRes, error) {
|
||||||
var cpr CreateProjectRes
|
var cpr CreateProjectRes
|
||||||
organizationslug, err := sdk.getOrganizationSlug(false)
|
organizationslug, err := sdk.getOrganizationSlug(false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return cpr, err
|
return cpr, err
|
||||||
}
|
}
|
||||||
endpoint := "api/v0/organizations/" + string(organizationslug) + "/projects/" + projectSlug
|
endpoint := "api/v0/organizations/" + string(organizationslug) + "/projects/" + projectSlug
|
||||||
url := fmt.Sprintf("%s/%s", sdk.apiURL, endpoint)
|
url := fmt.Sprintf("%s/%s", sdk.apiURL, endpoint)
|
||||||
|
|
||||||
req, err := http.NewRequest(http.MethodGet, url, nil)
|
req, err := http.NewRequest(http.MethodGet, url, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return cpr, err
|
return cpr, err
|
||||||
}
|
}
|
||||||
resp, err := sdk.makeRequest(req)
|
resp, err := sdk.makeRequest(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return cpr, err
|
return cpr, err
|
||||||
}
|
}
|
||||||
if err := json.Unmarshal(resp, &cpr); err != nil {
|
if err := json.Unmarshal(resp, &cpr); err != nil {
|
||||||
return cpr, err
|
return cpr, err
|
||||||
}
|
}
|
||||||
return cpr, nil
|
return cpr, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdateProject updates a Project under a given Organization
|
// UpdateProject updates a Project under a given Organization
|
||||||
func (sdk mfSDK) UpdateProject(project Project) (CreateProjectRes, error) {
|
func (sdk mfSDK) UpdateProject(project Project) (CreateProjectRes, error) {
|
||||||
var cpr CreateProjectRes
|
var cpr CreateProjectRes
|
||||||
organizationslug, err := sdk.getOrganizationSlug(false)
|
organizationslug, err := sdk.getOrganizationSlug(false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return cpr, err
|
return cpr, err
|
||||||
}
|
}
|
||||||
projectSlug, err := sdk.getProjectSlugByName(project.Name)
|
projectSlug, err := sdk.getProjectSlugByName(project.Name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return cpr, nil
|
return cpr, nil
|
||||||
}
|
}
|
||||||
data, err := json.Marshal(project)
|
data, err := json.Marshal(project)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return cpr, err
|
return cpr, err
|
||||||
}
|
}
|
||||||
payload := strings.NewReader(string(data))
|
payload := strings.NewReader(string(data))
|
||||||
|
|
||||||
endpoint := "api/v0/organizations/" + string(organizationslug) + "/projects/" + projectSlug
|
endpoint := "api/v0/organizations/" + string(organizationslug) + "/projects/" + projectSlug
|
||||||
url := fmt.Sprintf("%s/%s", sdk.apiURL, endpoint)
|
url := fmt.Sprintf("%s/%s", sdk.apiURL, endpoint)
|
||||||
|
|
||||||
req, err := http.NewRequest(http.MethodPatch, url, payload)
|
req, err := http.NewRequest(http.MethodPatch, url, payload)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return cpr, err
|
return cpr, err
|
||||||
}
|
}
|
||||||
resp, err := sdk.makeRequest(req)
|
resp, err := sdk.makeRequest(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return cpr, err
|
return cpr, err
|
||||||
}
|
}
|
||||||
if err := json.Unmarshal(resp, &cpr); err != nil {
|
if err := json.Unmarshal(resp, &cpr); err != nil {
|
||||||
return cpr, err
|
return cpr, err
|
||||||
}
|
}
|
||||||
return cpr, nil
|
return cpr, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteProject Delete a Project under a given Organization
|
// DeleteProject Delete a Project under a given Organization
|
||||||
func (sdk mfSDK) DeleteProject(project Project) (string, error) {
|
func (sdk mfSDK) DeleteProject(project Project) (string, error) {
|
||||||
organizationslug, err := sdk.getOrganizationSlug(false)
|
organizationslug, err := sdk.getOrganizationSlug(false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
projectSlug, err := sdk.getProjectSlugByName(project.Name)
|
projectSlug, err := sdk.getProjectSlugByName(project.Name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", nil
|
return "", nil
|
||||||
}
|
}
|
||||||
|
|
||||||
endpoint := "api/v0/organizations/" + string(organizationslug) + "/projects/" + projectSlug
|
endpoint := "api/v0/organizations/" + string(organizationslug) + "/projects/" + projectSlug
|
||||||
url := fmt.Sprintf("%s/%s", sdk.apiURL, endpoint)
|
url := fmt.Sprintf("%s/%s", sdk.apiURL, endpoint)
|
||||||
|
|
||||||
payload := strings.NewReader(``)
|
payload := strings.NewReader(``)
|
||||||
|
|
||||||
req, err := http.NewRequest(http.MethodDelete, url, payload)
|
req, err := http.NewRequest(http.MethodDelete, url, payload)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
_, err = sdk.makeRequest(req)
|
_, err = sdk.makeRequest(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
return "DELETED", nil
|
return "DELETED", nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetProjectClientKey Return the Project Client Key
|
// GetProjectClientKey Return the Project Client Key
|
||||||
func (sdk mfSDK) GetProjectClientKey(projectSlug string) (UserAPIKeyRes, error) {
|
func (sdk mfSDK) GetProjectClientKey(projectSlug string) (UserAPIKeyRes, error) {
|
||||||
var uakr UserAPIKeyRes
|
var uakr UserAPIKeyRes
|
||||||
organizationslug, err := sdk.getOrganizationSlug(false)
|
organizationslug, err := sdk.getOrganizationSlug(false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return uakr, err
|
return uakr, err
|
||||||
}
|
}
|
||||||
endpoint := "api/v0/organizations/" + string(organizationslug) + "/projects/" + projectSlug + "/api_key"
|
endpoint := "api/v0/organizations/" + string(organizationslug) + "/projects/" + projectSlug + "/api_key"
|
||||||
url := fmt.Sprintf("%s/%s", sdk.apiURL, endpoint)
|
url := fmt.Sprintf("%s/%s", sdk.apiURL, endpoint)
|
||||||
|
|
||||||
req, err := http.NewRequest(http.MethodGet, url, nil)
|
req, err := http.NewRequest(http.MethodGet, url, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return uakr, err
|
return uakr, err
|
||||||
}
|
}
|
||||||
resp, err := sdk.makeRequest(req)
|
resp, err := sdk.makeRequest(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return uakr, err
|
return uakr, err
|
||||||
}
|
}
|
||||||
if err := json.Unmarshal(resp, &uakr); err != nil {
|
if err := json.Unmarshal(resp, &uakr); err != nil {
|
||||||
return uakr, err
|
return uakr, err
|
||||||
}
|
}
|
||||||
return uakr, nil
|
return uakr, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// RefreshProjectClientKey Regenerate the Project Client Key
|
// RefreshProjectClientKey Regenerate the Project Client Key
|
||||||
func (sdk mfSDK) RefreshProjectClientKey(projectSlug string) (UserAPIKeyRes, error) {
|
func (sdk mfSDK) RefreshProjectClientKey(projectSlug string) (UserAPIKeyRes, error) {
|
||||||
var uakr UserAPIKeyRes
|
var uakr UserAPIKeyRes
|
||||||
organizationslug, err := sdk.getOrganizationSlug(false)
|
organizationslug, err := sdk.getOrganizationSlug(false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return uakr, err
|
return uakr, err
|
||||||
}
|
}
|
||||||
endpoint := "api/v0/organizations/" + string(organizationslug) + "/projects/" + projectSlug + "/api_key"
|
endpoint := "api/v0/organizations/" + string(organizationslug) + "/projects/" + projectSlug + "/api_key"
|
||||||
url := fmt.Sprintf("%s/%s", sdk.apiURL, endpoint)
|
url := fmt.Sprintf("%s/%s", sdk.apiURL, endpoint)
|
||||||
|
|
||||||
payload := strings.NewReader(``)
|
payload := strings.NewReader(``)
|
||||||
|
|
||||||
req, err := http.NewRequest(http.MethodPost, url, payload)
|
req, err := http.NewRequest(http.MethodPost, url, payload)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return uakr, err
|
return uakr, err
|
||||||
}
|
}
|
||||||
resp, err := sdk.makeRequest(req)
|
resp, err := sdk.makeRequest(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return uakr, err
|
return uakr, err
|
||||||
}
|
}
|
||||||
if err := json.Unmarshal(resp, &uakr); err != nil {
|
if err := json.Unmarshal(resp, &uakr); err != nil {
|
||||||
return uakr, err
|
return uakr, err
|
||||||
}
|
}
|
||||||
return uakr, nil
|
return uakr, nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user