mirror of
https://github.com/rodneyosodo/memfault-go.git
synced 2026-06-23 04:00:08 +00:00
Add new projects methods
Signed-off-by: 0x6f736f646f <blackd0t@protonmail.com>
This commit is contained in:
+89
-16
@@ -5,7 +5,9 @@ package memfault
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"time"
|
||||
@@ -18,30 +20,45 @@ var _ SDK = (*mfSDK)(nil)
|
||||
|
||||
// SDK contains Mainflux API.
|
||||
type SDK interface {
|
||||
// GetMe
|
||||
GetMe() (string, error)
|
||||
// GetMe Return information about the logged in User
|
||||
GetMe() (UserRes, error)
|
||||
|
||||
// Generate a user api key
|
||||
GenerateUserApiKey() (string, error)
|
||||
GenerateUserAPIKey() (UserAPIKeyRes, error)
|
||||
|
||||
// Get the issued user api key
|
||||
GetUserApiKey() (string, error)
|
||||
// Get a previously generated API Key for the logged in User
|
||||
GetUserAPIKey() (UserAPIKeyRes, error)
|
||||
|
||||
// Deletes the issued user api key
|
||||
DeleteUserApiKey() (string, error)
|
||||
// Invalidate the previously generated API Key for the logged in User and do not create another one
|
||||
DeleteUserAPIKey() (string, error)
|
||||
|
||||
// Gets the organization slug
|
||||
GetOrganizationSlug(multiple bool) (string, error)
|
||||
// Creates a Project under the given Organization
|
||||
CreateProject(project Project) (CreateProjectRes, error)
|
||||
|
||||
// Creates project
|
||||
CreateProject(project Project) (string, error)
|
||||
// List the Projects under a given Organization
|
||||
ListProject() (ListProjectRes, 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)
|
||||
|
||||
// Delete a Project under a given Organization
|
||||
DeleteProject(project Project) (string, error)
|
||||
|
||||
// Return the Project Client Key
|
||||
GetProjectClientKey(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"`
|
||||
APIKey string `json:"api_key"`
|
||||
OrganisationToken string `json:"organisation_token"`
|
||||
ProjectKey string `json:"project_key"`
|
||||
}
|
||||
@@ -58,7 +75,7 @@ type mfSDK struct {
|
||||
|
||||
// Config contains sdk configuration parameters.
|
||||
type Config struct {
|
||||
ApiURL string
|
||||
APIURL string
|
||||
ChunksURL string
|
||||
IngressURL string
|
||||
FilesURL string
|
||||
@@ -68,6 +85,7 @@ type Config struct {
|
||||
IdleConnTimeout time.Duration
|
||||
}
|
||||
|
||||
// Project struct
|
||||
type Project struct {
|
||||
Name string `json:"name"`
|
||||
Slug string `json:"slug"`
|
||||
@@ -78,7 +96,7 @@ type Project struct {
|
||||
// NewSDK returns new mainflux SDK instance.
|
||||
func NewSDK(conf Config) SDK {
|
||||
return &mfSDK{
|
||||
apiURL: conf.ApiURL,
|
||||
apiURL: conf.APIURL,
|
||||
chunksURL: conf.ChunksURL,
|
||||
ingressURL: conf.IngressURL,
|
||||
filesURL: conf.FilesURL,
|
||||
@@ -129,8 +147,8 @@ 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)
|
||||
} 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)
|
||||
@@ -138,3 +156,58 @@ func (sdk mfSDK) authenticate() (string, error) {
|
||||
}
|
||||
return "", errors.New("Empty credentials")
|
||||
}
|
||||
|
||||
func (sdk mfSDK) getOrganizationSlug(multiple bool) (string, error) {
|
||||
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
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
+102
-39
@@ -2,65 +2,128 @@ package memfault
|
||||
|
||||
import "time"
|
||||
|
||||
type apiKey struct {
|
||||
ApiKey string `json:"api_key,omitempty"`
|
||||
}
|
||||
type UserApiKeyRes struct {
|
||||
Data apiKey `json:"data,omitempty"`
|
||||
// APIKey provides the apikey
|
||||
type APIKey struct {
|
||||
APIKey string `json:"api_key,omitempty"`
|
||||
}
|
||||
|
||||
// UserAPIKeyRes is information about the logged in user api key
|
||||
type UserAPIKeyRes struct {
|
||||
Data APIKey `json:"data,omitempty"`
|
||||
}
|
||||
|
||||
// provides information about the organisation
|
||||
type organization struct {
|
||||
Id int `json:"id,omitempty"`
|
||||
ID int `json:"id,omitempty"`
|
||||
Name string `json:"name,omitempty"`
|
||||
Slug string `json:"slug"`
|
||||
}
|
||||
|
||||
type organizationAcl struct {
|
||||
// provides the organisation access control list
|
||||
type organizationACL struct {
|
||||
Admin bool `json:"admin,omitempty"`
|
||||
CustomerSupport bool `json:"customer_support,omitempty"`
|
||||
Organization organization `json:"organization,omitempty"`
|
||||
}
|
||||
type organizations []organization
|
||||
|
||||
// UserRes is information about the logged in User
|
||||
type UserRes struct {
|
||||
CreatedDate time.Time `json:"created_date,omitempty"`
|
||||
Email string `json:"email,omitempty"`
|
||||
Id int `json:"id,omitempty"`
|
||||
ID int `json:"id,omitempty"`
|
||||
Name string `json:"name,omitempty"`
|
||||
Organizations organizations `json:"organizations,omitempty"`
|
||||
Organizations []organization `json:"organizations,omitempty"`
|
||||
UpdatedDate time.Time `json:"updated_date,omitempty"`
|
||||
PrivacyNotice bool `json:"has_accepted_privacy_notice"`
|
||||
TermsOfService bool `json:"has_accepted_terms_of_service"`
|
||||
Impersonated string `json:"impersonator"`
|
||||
OrganizationsAcls []organizationAcl `json:"organization_acls"`
|
||||
OrganizationsAcls []organizationACL `json:"organization_acls"`
|
||||
}
|
||||
|
||||
// type integrations struct {
|
||||
// name string `json:"name,omitempty"`
|
||||
// }
|
||||
// type projectData struct {
|
||||
// CreatedDate time.Time `json:"created_date,omitempty"`
|
||||
// Slug string `json:"slug,omitempty": "smartsink"`
|
||||
// LegacyLatestApiEnabled bool `json:"legacy_latest_api_enabled"`
|
||||
// Integrations []integrations `json:"integrations"`
|
||||
// AlertsEnabled bool `json:"alerts_enabled,omitempty"`
|
||||
// UpdatedDate time.Time `json:"updated_date,omitempty"`
|
||||
// CountDeployments int `json:"count_deployments"`
|
||||
// DashboardEnabled bool `json:"dashboard_enabled"`
|
||||
// DeviceLinkStrategies [] `json:"device_link_strategies"`
|
||||
// PrimarySoftwareType string `json:"primary_software_type"`
|
||||
// BugReportEnabled bool `json:"bugreport_export_enabled"`
|
||||
// MetricChartEnabled bool `json:"metric_chart_enabled"`
|
||||
// // "chunks_debug_log_length": 0,
|
||||
// // "api_key": "3kfvYHivnNzFGtlKnNdUAAhETQU77Ldp",
|
||||
// // "multi_component_enabled": false,
|
||||
// intergrations of the project
|
||||
type integrations struct {
|
||||
Name string `json:"name,omitempty"`
|
||||
}
|
||||
|
||||
// VersioningScheme string `json:"versioning_scheme"`
|
||||
// Name string `json:"name"`
|
||||
// Os string `json:"os"`
|
||||
// Platform string `json:"platform"`
|
||||
// Id int `json:"id"`
|
||||
// }
|
||||
// type CreateProjectRes struct {
|
||||
// Data projectData `json:"data,omitempty"`
|
||||
// }
|
||||
// device link strategies
|
||||
type deviceLinkStrategies struct {
|
||||
Name string `json:"name,omitempty"`
|
||||
}
|
||||
|
||||
// setup datails for the project
|
||||
type setupDetails struct {
|
||||
OperatingSystem []string `json:"operating_system,omitempty"`
|
||||
CompilerOther string `json:"compiler_other,omitempty"`
|
||||
BuildToolchain []string `json:"build_toolchain,omitempty"`
|
||||
BuildToolchainOther string `json:"build_toolchain_other,omitempty"`
|
||||
AdditionalChips string `json:"additional_chips,omitempty"`
|
||||
ProgrammingLanguagesOther string `json:"programming_languages_other,omitempty"`
|
||||
DebuggingToolchain []string `json:"debugging_toolchain,omitempty"`
|
||||
ConnectivityOther string `json:"connectivity_other,omitempty"`
|
||||
PrimaryChip string `json:"primary_chip,omitempty"`
|
||||
Compiler []string `json:"compiler,omitempty"`
|
||||
Connectivity []string `json:"connectivity,omitempty"`
|
||||
PrimaryChipOther string `json:"primary_chip_other,omitempty"`
|
||||
DebuggingToolchainOther string `json:"debugging_toolchain_other,omitempty"`
|
||||
GmsCertification string `json:"gms_certification,omitempty"`
|
||||
OperatingSystemOther string `json:"operating_system_other,omitempty"`
|
||||
ProgrammingLanguages []string `json:"programming_languages,omitempty"`
|
||||
}
|
||||
|
||||
// Details to the project
|
||||
type projectData struct {
|
||||
CreatedDate time.Time `json:"created_date,omitempty"`
|
||||
Slug string `json:"slug,omitempty"`
|
||||
LegacyLatestAPIEnabled bool `json:"legacy_latest_api_enabled,omitempty"`
|
||||
Integrations []integrations `json:"integrations,omitempty"`
|
||||
AlertsEnabled bool `json:"alerts_enabled,omitempty,omitempty"`
|
||||
UpdatedDate time.Time `json:"updated_date,omitempty,omitempty"`
|
||||
CountDeployments int `json:"count_deployments,omitempty"`
|
||||
DashboardEnabled bool `json:"dashboard_enabled,omitempty"`
|
||||
DeviceLinkStrategies []deviceLinkStrategies `json:"device_link_strategies,omitempty"`
|
||||
PrimarySoftwareType string `json:"primary_software_type,omitempty"`
|
||||
BugReportEnabled bool `json:"bugreport_export_enabled,omitempty"`
|
||||
MetricChartEnabled bool `json:"metric_chart_enabled,omitempty"`
|
||||
ChunksDebugLogLength int `json:"chunks_debug_log_length,omitempty"`
|
||||
EventDebugLogLength int `json:"event_debug_log_length,omitempty"`
|
||||
APIKey string `json:"api_key,omitempty"`
|
||||
MultiComponentEnabled bool `json:"multi_component_enabled,omitempty"`
|
||||
SetupDetails setupDetails `json:"setup_details,omitempty"`
|
||||
VersioningScheme string `json:"versioning_scheme"`
|
||||
Name string `json:"name"`
|
||||
Os string `json:"os"`
|
||||
Platform string `json:"platform"`
|
||||
ID int `json:"id"`
|
||||
}
|
||||
|
||||
// CreateProjectRes thirs is a new project created
|
||||
type CreateProjectRes struct {
|
||||
Data projectData `json:"data,omitempty"`
|
||||
}
|
||||
|
||||
// project error message
|
||||
type createProjectError struct {
|
||||
Code int `json:"code,omitempty"`
|
||||
HTTPCode int `json:"http_code,omitempty"`
|
||||
Message string `json:"message,omitempty"`
|
||||
ErrorType string `json:"type,omitempty"`
|
||||
}
|
||||
|
||||
// CreateProjectAlreadyExistRes eror message that project already exist
|
||||
type CreateProjectAlreadyExistRes struct {
|
||||
Error createProjectError `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
type paging struct {
|
||||
PerPage int `json:"per_page,omitempty"`
|
||||
TotalCount int `json:"total_count,omitempty"`
|
||||
PageCount int `json:"page_count,omitempty"`
|
||||
Page int `json:"page,omitempty"`
|
||||
ItemCount int `json:"item_count,omitempty"`
|
||||
}
|
||||
|
||||
// ListProjectRes struct
|
||||
type ListProjectRes struct {
|
||||
Data []projectData `json:"data,omitempty"`
|
||||
Paging paging `json:"paging,omitempty"`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user