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