2024-06-20 12:14:46 +05:30
|
|
|
package prime_api
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"fmt"
|
|
|
|
|
"io"
|
|
|
|
|
"net/http"
|
|
|
|
|
"net/url"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type IPrimeMonitorApi interface {
|
|
|
|
|
PostServiceStatus(StatusPayload) ErrorCode
|
2025-02-12 17:26:27 +05:30
|
|
|
GetFeatureFlags(licenseKey string) (*FlagDataResponse, *APIError)
|
|
|
|
|
ActivateInstance() *APIError
|
|
|
|
|
DeactivateInstance() *APIError
|
|
|
|
|
RetrievePlans(string) (*[]Product, *APIError)
|
|
|
|
|
RetrievePaymentLink(RetrievePaymentLinkPayload) (*RetrievePaymentLinkResponse, *APIError)
|
|
|
|
|
UpdateSubcription(SeatUpdatePayload) (*SeatUpdateResponse, *APIError)
|
|
|
|
|
SyncWorkspace(WorkspaceSyncPayload) (*WorkspaceActivationResponse, *APIError)
|
|
|
|
|
DeactivateLicense(LicenseDeactivatePayload) (*WorkspaceActivationResponse, *APIError)
|
|
|
|
|
ActivateWorkspace(WorkspaceActivationPayload) (*WorkspaceActivationResponse, *APIError)
|
|
|
|
|
ActivateFreeWorkspace(WorkspaceActivationPayload) (*WorkspaceActivationResponse, *APIError)
|
|
|
|
|
InitializeInstance(CredentialsPayload) (SetupResponse, *APIError)
|
|
|
|
|
GetSubscriptionDetails(WorkspaceSubscriptionPayload) (*WorkspaceSubscriptionResponse, *APIError)
|
|
|
|
|
GetProrationPreview(ProrationPreviewPayload) (*ProrationPreviewResponse, *APIError)
|
2024-06-20 12:14:46 +05:30
|
|
|
}
|
|
|
|
|
|
2024-10-03 17:28:18 +05:30
|
|
|
type LicenseDeactivatePayload struct {
|
|
|
|
|
WorkspaceSlug string `json:"workspace_slug"`
|
|
|
|
|
WorkspaceID string `json:"workspace_id"`
|
|
|
|
|
LicenseKey string `json:"license_key"`
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-20 12:14:46 +05:30
|
|
|
type PrimeMonitorApi struct {
|
|
|
|
|
host string
|
2024-08-22 17:39:19 +05:30
|
|
|
instanceId string
|
|
|
|
|
appVersion string
|
2024-06-20 12:14:46 +05:30
|
|
|
client string
|
|
|
|
|
version string
|
|
|
|
|
machineSignature string
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-02 15:28:43 +05:30
|
|
|
type CredentialsPayload struct {
|
2025-02-12 17:26:27 +05:30
|
|
|
ServerId string
|
|
|
|
|
Domain string
|
2024-12-05 15:16:48 +05:30
|
|
|
AppVersion string
|
2024-09-02 15:28:43 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type WorkspaceSubscriptionPayload struct {
|
|
|
|
|
WorkspaceId string `json:"workspace_id"`
|
|
|
|
|
LicenseKey string `json:"license_key"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type WorkspaceSubscriptionResponse struct {
|
|
|
|
|
Product string `json:"product"`
|
|
|
|
|
CurrentPeriodEnd string `json:"current_period_end_date"`
|
|
|
|
|
SubscriptionExists bool `json:"subscription_exists"`
|
|
|
|
|
Url string `json:"url"`
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-12 17:26:27 +05:30
|
|
|
type ProrationPreviewPayload struct {
|
|
|
|
|
WorkspaceId string `json:"workspace_id"`
|
|
|
|
|
Quantity int `json:"quantity"`
|
|
|
|
|
WorkspaceSlug string `json:"workspace_slug"`
|
|
|
|
|
LicenseKey string `json:"license_key"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type ProrationPreviewResponse struct {
|
|
|
|
|
QuantityDifference int `json:"quantity_difference"`
|
|
|
|
|
PerSeatProrationAmount float64 `json:"per_seat_prorated_amount"`
|
|
|
|
|
NewQuantity int `json:"new_quantity"`
|
|
|
|
|
TotalProratedAmount float64 `json:"total_prorated_amount"`
|
|
|
|
|
CurrentQuantity int `json:"current_quantity"`
|
|
|
|
|
CurrentPriceAmount float64 `json:"current_price_amount"`
|
|
|
|
|
CurrentPriceInterval string `json:"current_price_interval"`
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-02 15:28:43 +05:30
|
|
|
type SetupResponse struct {
|
|
|
|
|
Domain string `json:"domain"`
|
|
|
|
|
InstanceId string `json:"instance_id"`
|
|
|
|
|
Host string `json:"prime_host"`
|
|
|
|
|
Version string `json:"version"`
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-22 17:39:19 +05:30
|
|
|
type FlagsPayload struct {
|
|
|
|
|
EncryptedData string `json:"encrypted_data"`
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-12 17:26:27 +05:30
|
|
|
// Add this struct to handle the error response from the server
|
|
|
|
|
type ErrorResponse struct {
|
|
|
|
|
Message string `json:"message"`
|
|
|
|
|
Error string `json:"error"`
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-22 17:39:19 +05:30
|
|
|
func NewMonitorApi(host, machineSignature, instanceId, appVersion string) IPrimeMonitorApi {
|
2024-06-20 12:14:46 +05:30
|
|
|
return &PrimeMonitorApi{
|
|
|
|
|
host: host,
|
2024-08-22 17:39:19 +05:30
|
|
|
instanceId: instanceId,
|
|
|
|
|
appVersion: appVersion,
|
2024-06-20 12:14:46 +05:30
|
|
|
client: "Prime-Monitor",
|
2024-08-22 17:39:19 +05:30
|
|
|
version: appVersion,
|
2024-06-20 12:14:46 +05:30
|
|
|
machineSignature: machineSignature,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (api *PrimeMonitorApi) SetClient(client string) {
|
|
|
|
|
api.client = client
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var (
|
2024-10-03 17:28:18 +05:30
|
|
|
API_PREFIX = "/api/v2"
|
|
|
|
|
MONITOR_ENDPOINT = API_PREFIX + "/monitor/"
|
|
|
|
|
FEATURE_FLAGS = API_PREFIX + "/flags/"
|
|
|
|
|
INSTANCE_PREFIX = API_PREFIX + "/instances"
|
|
|
|
|
ACTIVATE_INSTANCE = INSTANCE_PREFIX + "/activate/"
|
|
|
|
|
DEACTIVATE_INSTANCE = INSTANCE_PREFIX + "/deactivate/"
|
|
|
|
|
UPGRADE_INSTANCE = INSTANCE_PREFIX + "/upgrade/"
|
|
|
|
|
FREE_WORKSPACE_ACTIVATE = API_PREFIX + "/licenses/initialize/"
|
|
|
|
|
WORKSPACE_ACTIVATE = API_PREFIX + "/licenses/activate/"
|
|
|
|
|
SYNC_WORKSPACE = API_PREFIX + "/licenses/sync/"
|
|
|
|
|
UPDATE_SUBSCRIPTION = API_PREFIX + "/modify-subscriptions/"
|
|
|
|
|
SETUP_ENDPOINT = API_PREFIX + "/instances/initialize/"
|
|
|
|
|
SUBSCRIPTION_PORTAL = API_PREFIX + "/subscription-portal/"
|
|
|
|
|
RETRIEVE_PLANS = API_PREFIX + "/instances/products/"
|
|
|
|
|
RETRIEVE_PAYMENT_LINK = API_PREFIX + "/instances/payment-link/"
|
|
|
|
|
DEACTIVATE_LICENSE_ENDPOINT = API_PREFIX + "/licenses/deactivate/"
|
2025-02-12 17:26:27 +05:30
|
|
|
PRORATION_PREVIEW = API_PREFIX + "/subscriptions/proration-preview/"
|
2024-06-20 12:14:46 +05:30
|
|
|
)
|
|
|
|
|
|
2024-08-22 17:39:19 +05:30
|
|
|
/* ----------------------- Controller Methods ------------------------------ */
|
2024-06-20 12:14:46 +05:30
|
|
|
// Posts the status of the services given, to the prime server, returns error if
|
|
|
|
|
// hinderer, else doesn't return anything
|
|
|
|
|
func (api *PrimeMonitorApi) PostServiceStatus(payload StatusPayload) ErrorCode {
|
|
|
|
|
_, err := api.post(api.host+MONITOR_ENDPOINT, payload)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return UNABLE_TO_POST_SERVICE_STATUS
|
|
|
|
|
}
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-12 17:26:27 +05:30
|
|
|
func (api *PrimeMonitorApi) DeactivateLicense(payload LicenseDeactivatePayload) (*WorkspaceActivationResponse, *APIError) {
|
|
|
|
|
resp, apiError := api.post(api.host+DEACTIVATE_LICENSE_ENDPOINT, payload)
|
|
|
|
|
if apiError != nil {
|
|
|
|
|
return nil, apiError
|
2024-10-03 17:28:18 +05:30
|
|
|
}
|
|
|
|
|
data := WorkspaceActivationResponse{}
|
2025-02-12 17:26:27 +05:30
|
|
|
|
|
|
|
|
if err := json.Unmarshal(resp, &data); err != nil {
|
|
|
|
|
return nil, &APIError{
|
|
|
|
|
Error: fmt.Sprintf("Error unmarshaling response: %v", err),
|
|
|
|
|
Success: false,
|
|
|
|
|
}
|
2024-10-03 17:28:18 +05:30
|
|
|
}
|
2025-02-12 17:26:27 +05:30
|
|
|
|
|
|
|
|
return &data, nil
|
2024-10-03 17:28:18 +05:30
|
|
|
}
|
|
|
|
|
|
2025-02-12 17:26:27 +05:30
|
|
|
func (api *PrimeMonitorApi) ActivateFreeWorkspace(payload WorkspaceActivationPayload) (*WorkspaceActivationResponse, *APIError) {
|
|
|
|
|
resp, apiError := api.post(api.host+FREE_WORKSPACE_ACTIVATE, payload)
|
|
|
|
|
if apiError != nil {
|
|
|
|
|
return nil, apiError
|
2024-08-22 17:39:19 +05:30
|
|
|
}
|
|
|
|
|
data := WorkspaceActivationResponse{}
|
2025-02-12 17:26:27 +05:30
|
|
|
if err := json.Unmarshal(resp, &data); err != nil {
|
|
|
|
|
return nil, &APIError{
|
|
|
|
|
Error: fmt.Sprintf("Error unmarshaling response: %v", err),
|
|
|
|
|
Success: false,
|
|
|
|
|
}
|
2024-08-22 17:39:19 +05:30
|
|
|
}
|
2025-02-12 17:26:27 +05:30
|
|
|
return &data, nil
|
2024-08-22 17:39:19 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Activating a paid licensed workspace
|
2025-02-12 17:26:27 +05:30
|
|
|
func (api *PrimeMonitorApi) ActivateWorkspace(payload WorkspaceActivationPayload) (*WorkspaceActivationResponse, *APIError) {
|
|
|
|
|
resp, apiError := api.post(api.host+WORKSPACE_ACTIVATE, payload)
|
|
|
|
|
if apiError != nil {
|
|
|
|
|
return nil, apiError
|
2024-08-22 17:39:19 +05:30
|
|
|
}
|
|
|
|
|
data := WorkspaceActivationResponse{}
|
2025-02-12 17:26:27 +05:30
|
|
|
if err := json.Unmarshal(resp, &data); err != nil {
|
|
|
|
|
return nil, &APIError{
|
|
|
|
|
Error: fmt.Sprintf("Error unmarshaling response: %v", err),
|
|
|
|
|
Success: false,
|
|
|
|
|
}
|
2024-08-22 17:39:19 +05:30
|
|
|
}
|
2025-02-12 17:26:27 +05:30
|
|
|
return &data, nil
|
2024-08-22 17:39:19 +05:30
|
|
|
}
|
|
|
|
|
|
2025-02-12 17:26:27 +05:30
|
|
|
func (api *PrimeMonitorApi) GetFeatureFlags(licenseKey string) (*FlagDataResponse, *APIError) {
|
|
|
|
|
flagData, apiError := api.post(api.host+FEATURE_FLAGS, map[string]string{
|
2024-08-22 17:39:19 +05:30
|
|
|
"license_key": licenseKey,
|
|
|
|
|
"version": api.version,
|
|
|
|
|
})
|
2025-02-12 17:26:27 +05:30
|
|
|
if apiError != nil {
|
|
|
|
|
return nil, apiError
|
2024-08-22 17:39:19 +05:30
|
|
|
}
|
|
|
|
|
data := FlagDataResponse{}
|
2025-02-12 17:26:27 +05:30
|
|
|
if err := json.Unmarshal(flagData, &data); err != nil {
|
|
|
|
|
return nil, &APIError{
|
|
|
|
|
Error: fmt.Sprintf("Error unmarshaling response: %v", err),
|
|
|
|
|
Success: false,
|
|
|
|
|
}
|
2024-08-22 17:39:19 +05:30
|
|
|
}
|
2025-02-12 17:26:27 +05:30
|
|
|
return &data, nil
|
2024-08-22 17:39:19 +05:30
|
|
|
}
|
|
|
|
|
|
2025-02-12 17:26:27 +05:30
|
|
|
func (api *PrimeMonitorApi) RetrievePlans(quantity string) (*[]Product, *APIError) {
|
|
|
|
|
resp, apiError := api.get(api.host+RETRIEVE_PLANS, map[string]string{
|
2024-09-17 13:03:54 +05:30
|
|
|
"quantity": quantity,
|
|
|
|
|
})
|
2025-02-12 17:26:27 +05:30
|
|
|
if apiError != nil {
|
|
|
|
|
return nil, apiError
|
2024-09-17 13:03:54 +05:30
|
|
|
}
|
|
|
|
|
data := []Product{}
|
2025-02-12 17:26:27 +05:30
|
|
|
if err := json.Unmarshal(resp, &data); err != nil {
|
|
|
|
|
return nil, &APIError{
|
|
|
|
|
Error: fmt.Sprintf("Error unmarshaling response: %v", err),
|
|
|
|
|
Success: false,
|
|
|
|
|
}
|
2024-09-17 13:03:54 +05:30
|
|
|
}
|
2025-02-12 17:26:27 +05:30
|
|
|
return &data, nil
|
2024-09-17 13:03:54 +05:30
|
|
|
}
|
|
|
|
|
|
2025-02-12 17:26:27 +05:30
|
|
|
func (api *PrimeMonitorApi) RetrievePaymentLink(payload RetrievePaymentLinkPayload) (*RetrievePaymentLinkResponse, *APIError) {
|
|
|
|
|
resp, apiError := api.post(api.host+RETRIEVE_PAYMENT_LINK, payload)
|
|
|
|
|
if apiError != nil {
|
|
|
|
|
return nil, apiError
|
2024-09-17 13:03:54 +05:30
|
|
|
}
|
|
|
|
|
data := RetrievePaymentLinkResponse{}
|
2025-02-12 17:26:27 +05:30
|
|
|
if err := json.Unmarshal(resp, &data); err != nil {
|
|
|
|
|
return nil, &APIError{
|
|
|
|
|
Error: fmt.Sprintf("Error unmarshaling response: %v", err),
|
|
|
|
|
Success: false,
|
|
|
|
|
}
|
2024-09-17 13:03:54 +05:30
|
|
|
}
|
2025-02-12 17:26:27 +05:30
|
|
|
return &data, nil
|
2024-09-17 13:03:54 +05:30
|
|
|
}
|
|
|
|
|
|
2025-02-12 17:26:27 +05:30
|
|
|
func (api *PrimeMonitorApi) SyncWorkspace(payload WorkspaceSyncPayload) (*WorkspaceActivationResponse, *APIError) {
|
|
|
|
|
resp, apiError := api.post(api.host+SYNC_WORKSPACE, payload)
|
|
|
|
|
if apiError != nil {
|
|
|
|
|
return nil, apiError
|
2024-08-22 17:39:19 +05:30
|
|
|
}
|
|
|
|
|
data := WorkspaceActivationResponse{}
|
2025-02-12 17:26:27 +05:30
|
|
|
if err := json.Unmarshal(resp, &data); err != nil {
|
|
|
|
|
return nil, &APIError{
|
|
|
|
|
Error: fmt.Sprintf("Error unmarshaling response: %v", err),
|
|
|
|
|
Success: false,
|
|
|
|
|
}
|
2024-08-22 17:39:19 +05:30
|
|
|
}
|
2025-02-12 17:26:27 +05:30
|
|
|
return &data, nil
|
2024-08-22 17:39:19 +05:30
|
|
|
}
|
|
|
|
|
|
2025-02-12 17:26:27 +05:30
|
|
|
func (api *PrimeMonitorApi) ActivateInstance() *APIError {
|
|
|
|
|
_, apiError := api.post(api.host+ACTIVATE_INSTANCE, nil)
|
|
|
|
|
if apiError != nil {
|
|
|
|
|
return apiError
|
2024-08-22 17:39:19 +05:30
|
|
|
}
|
2025-02-12 17:26:27 +05:30
|
|
|
return nil
|
2024-08-22 17:39:19 +05:30
|
|
|
}
|
|
|
|
|
|
2025-02-12 17:26:27 +05:30
|
|
|
func (api *PrimeMonitorApi) DeactivateInstance() *APIError {
|
|
|
|
|
_, apiError := api.post(api.host+DEACTIVATE_INSTANCE, nil)
|
|
|
|
|
if apiError != nil {
|
|
|
|
|
return apiError
|
2024-08-22 17:39:19 +05:30
|
|
|
}
|
2025-02-12 17:26:27 +05:30
|
|
|
return nil
|
2024-08-22 17:39:19 +05:30
|
|
|
}
|
|
|
|
|
|
2025-02-12 17:26:27 +05:30
|
|
|
func (api *PrimeMonitorApi) UpgradeInstance() *APIError {
|
|
|
|
|
_, apiError := api.post(api.host+UPGRADE_INSTANCE, nil)
|
|
|
|
|
if apiError != nil {
|
|
|
|
|
return apiError
|
2024-08-22 17:39:19 +05:30
|
|
|
}
|
2025-02-12 17:26:27 +05:30
|
|
|
return nil
|
2024-08-22 17:39:19 +05:30
|
|
|
}
|
|
|
|
|
|
2025-02-12 17:26:27 +05:30
|
|
|
func (api *PrimeMonitorApi) UpdateSubcription(payload SeatUpdatePayload) (*SeatUpdateResponse, *APIError) {
|
|
|
|
|
resp, apiError := api.post(api.host+UPDATE_SUBSCRIPTION, payload)
|
|
|
|
|
if apiError != nil {
|
|
|
|
|
return nil, apiError
|
2024-09-02 15:28:43 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data := SeatUpdateResponse{}
|
2025-02-12 17:26:27 +05:30
|
|
|
if err := json.Unmarshal(resp, &data); err != nil {
|
|
|
|
|
return nil, &APIError{
|
|
|
|
|
Error: fmt.Sprintf("Error unmarshaling response: %v", err),
|
|
|
|
|
Success: false,
|
|
|
|
|
}
|
2024-09-02 15:28:43 +05:30
|
|
|
}
|
2025-02-12 17:26:27 +05:30
|
|
|
return &data, nil
|
2024-09-02 15:28:43 +05:30
|
|
|
}
|
|
|
|
|
|
2025-02-12 17:26:27 +05:30
|
|
|
func (api *PrimeMonitorApi) InitializeInstance(payload CredentialsPayload) (SetupResponse, *APIError) {
|
|
|
|
|
resp, apiError := api.post(api.host+SETUP_ENDPOINT, map[string]string{
|
2024-09-02 15:28:43 +05:30
|
|
|
"machine_signature": payload.ServerId,
|
|
|
|
|
"domain": payload.Domain,
|
2024-12-05 15:16:48 +05:30
|
|
|
"app_version": payload.AppVersion,
|
2024-09-06 16:09:44 +05:30
|
|
|
"deploy_platform": "KUBERNETES",
|
2024-09-02 15:28:43 +05:30
|
|
|
})
|
|
|
|
|
|
2025-02-12 17:26:27 +05:30
|
|
|
if apiError != nil {
|
|
|
|
|
return SetupResponse{}, apiError
|
2024-09-02 15:28:43 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setupResponse := SetupResponse{}
|
2025-02-12 17:26:27 +05:30
|
|
|
if err := json.Unmarshal(resp, &setupResponse); err != nil {
|
|
|
|
|
return SetupResponse{}, &APIError{
|
|
|
|
|
Error: fmt.Sprintf("Error unmarshaling response: %v", err),
|
|
|
|
|
Success: false,
|
|
|
|
|
}
|
2024-09-02 15:28:43 +05:30
|
|
|
}
|
2025-02-12 17:26:27 +05:30
|
|
|
return setupResponse, nil
|
2024-09-02 15:28:43 +05:30
|
|
|
}
|
|
|
|
|
|
2025-02-12 17:26:27 +05:30
|
|
|
func (api *PrimeMonitorApi) GetSubscriptionDetails(payload WorkspaceSubscriptionPayload) (*WorkspaceSubscriptionResponse, *APIError) {
|
|
|
|
|
resp, apiError := api.post(api.host+SUBSCRIPTION_PORTAL, payload)
|
|
|
|
|
if apiError != nil {
|
|
|
|
|
return nil, apiError
|
2024-09-02 15:28:43 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data := WorkspaceSubscriptionResponse{}
|
2025-02-12 17:26:27 +05:30
|
|
|
if err := json.Unmarshal(resp, &data); err != nil {
|
|
|
|
|
return nil, &APIError{
|
|
|
|
|
Error: fmt.Sprintf("Error unmarshaling response: %v", err),
|
|
|
|
|
Success: false,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return &data, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (api *PrimeMonitorApi) GetProrationPreview(payload ProrationPreviewPayload) (*ProrationPreviewResponse, *APIError) {
|
|
|
|
|
// Make the request
|
|
|
|
|
resp, apiError := api.post(api.host+PRORATION_PREVIEW, payload)
|
|
|
|
|
if apiError != nil {
|
|
|
|
|
return nil, apiError
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Unmarshal the response
|
|
|
|
|
data := ProrationPreviewResponse{}
|
|
|
|
|
if err := json.Unmarshal(resp, &data); err != nil {
|
|
|
|
|
return nil, &APIError{
|
|
|
|
|
Error: fmt.Sprintf("Error unmarshaling response: %v", err),
|
|
|
|
|
Success: false,
|
|
|
|
|
}
|
2024-09-02 15:28:43 +05:30
|
|
|
}
|
2025-02-12 17:26:27 +05:30
|
|
|
// Return the response
|
|
|
|
|
return &data, nil
|
2024-09-02 15:28:43 +05:30
|
|
|
}
|
|
|
|
|
|
2024-06-20 12:14:46 +05:30
|
|
|
// ------------------------ Helper Methods ----------------------------------
|
|
|
|
|
/*
|
|
|
|
|
prepareRequest prepares an HTTP request with the necessary headers and parameters.
|
|
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
|
- method: string specifying the HTTP method (e.g., "GET", "POST").
|
|
|
|
|
- urlStr: string specifying the URL for the request.
|
|
|
|
|
- body: io.Reader containing the request body.
|
|
|
|
|
- params: map[string]string containing the query parameters.
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
- *http.Request: The prepared HTTP request.
|
|
|
|
|
- error: An error if any occurs during the preparation.
|
|
|
|
|
*/
|
|
|
|
|
func (api *PrimeMonitorApi) prepareRequest(method, urlStr string, body io.Reader, params map[string]string) (*http.Request, error) {
|
|
|
|
|
if method == "GET" && params != nil {
|
|
|
|
|
parsedURL, err := url.Parse(urlStr)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("error parsing URL: %v", err)
|
|
|
|
|
}
|
|
|
|
|
query := parsedURL.Query()
|
|
|
|
|
for key, value := range params {
|
|
|
|
|
query.Set(key, value)
|
|
|
|
|
}
|
|
|
|
|
parsedURL.RawQuery = query.Encode()
|
|
|
|
|
urlStr = parsedURL.String()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
req, err := http.NewRequest(method, urlStr, body)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("error creating request: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
headers := map[string]string{
|
2024-08-22 17:39:19 +05:30
|
|
|
"X-Instance-Id": api.instanceId,
|
2024-06-20 12:14:46 +05:30
|
|
|
"X-Machine-Signature": api.machineSignature,
|
|
|
|
|
"X-Client": api.client,
|
|
|
|
|
"X-License-Version": api.version,
|
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for key, value := range headers {
|
|
|
|
|
req.Header.Add(key, value)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return req, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
doRequest executes the HTTP request and handles common response scenarios.
|
|
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
|
- req: *http.Request specifying the HTTP request to execute.
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
- []byte: The response body.
|
|
|
|
|
- error: An error if any occurs during the request execution.
|
|
|
|
|
*/
|
2025-02-12 17:26:27 +05:30
|
|
|
func (api *PrimeMonitorApi) doRequest(req *http.Request) ([]byte, *APIError) {
|
2024-06-20 12:14:46 +05:30
|
|
|
client := &http.Client{}
|
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
|
if err != nil {
|
2025-02-12 17:26:27 +05:30
|
|
|
return nil, &APIError{
|
|
|
|
|
Error: fmt.Sprintf("error making request: %v", err),
|
|
|
|
|
Success: false,
|
|
|
|
|
}
|
2024-06-20 12:14:46 +05:30
|
|
|
}
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
|
|
switch {
|
|
|
|
|
case resp.StatusCode >= 200 && resp.StatusCode <= 227:
|
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
|
|
|
if err != nil {
|
2025-02-12 17:26:27 +05:30
|
|
|
return nil, &APIError{
|
|
|
|
|
Error: fmt.Sprintf("error reading response body: %v", err),
|
|
|
|
|
Success: false,
|
|
|
|
|
}
|
2024-06-20 12:14:46 +05:30
|
|
|
}
|
|
|
|
|
return body, nil
|
|
|
|
|
case resp.StatusCode >= 300 && resp.StatusCode <= 308:
|
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
|
|
|
if err != nil {
|
2025-02-12 17:26:27 +05:30
|
|
|
return nil, &APIError{
|
|
|
|
|
Error: fmt.Sprintf("error reading response body: %v", err),
|
|
|
|
|
Success: false,
|
|
|
|
|
}
|
2024-06-20 12:14:46 +05:30
|
|
|
}
|
|
|
|
|
return body, nil
|
|
|
|
|
case resp.StatusCode >= 400 && resp.StatusCode <= 451:
|
2025-02-12 17:26:27 +05:30
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, &APIError{
|
|
|
|
|
Error: fmt.Sprintf("error reading response body: %v", err),
|
|
|
|
|
Success: false,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Try to parse the error response
|
|
|
|
|
var errorResp ErrorResponse
|
|
|
|
|
if err := json.Unmarshal(body, &errorResp); err != nil {
|
|
|
|
|
// If we can't parse the error, return the status code
|
|
|
|
|
return nil, &APIError{
|
|
|
|
|
Error: fmt.Sprintf("unexpected status code %d", resp.StatusCode),
|
|
|
|
|
Success: false,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil, &APIError{
|
|
|
|
|
Error: errorResp.Error,
|
|
|
|
|
Success: false,
|
|
|
|
|
}
|
2024-06-20 12:14:46 +05:30
|
|
|
default:
|
2025-02-12 17:26:27 +05:30
|
|
|
return nil, &APIError{
|
|
|
|
|
Error: fmt.Sprintf("unexpected status code: %v", resp.StatusCode),
|
|
|
|
|
Success: false,
|
|
|
|
|
}
|
2024-06-20 12:14:46 +05:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
get performs a GET request.
|
|
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
|
- baseURL: string specifying the base URL for the request.
|
|
|
|
|
- params: map[string]string containing the query parameters.
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
- []byte: The response body.
|
|
|
|
|
- error: An error if any occurs during the request.
|
|
|
|
|
*/
|
2025-02-12 17:26:27 +05:30
|
|
|
func (api *PrimeMonitorApi) get(baseURL string, params map[string]string) ([]byte, *APIError) {
|
2024-06-20 12:14:46 +05:30
|
|
|
req, err := api.prepareRequest("GET", baseURL, nil, params)
|
|
|
|
|
if err != nil {
|
2025-02-12 17:26:27 +05:30
|
|
|
return nil, &APIError{
|
|
|
|
|
Error: fmt.Sprintf("error preparing request: %v", err),
|
|
|
|
|
Success: false,
|
|
|
|
|
}
|
2024-06-20 12:14:46 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return api.doRequest(req)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
post performs a POST request with JSON body.
|
|
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
|
- baseURL: string specifying the base URL for the request.
|
|
|
|
|
- data: interface{} containing the data to be sent in the request body.
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
- []byte: The response body.
|
|
|
|
|
- error: An error if any occurs during the request.
|
|
|
|
|
*/
|
2025-02-12 17:26:27 +05:30
|
|
|
func (api *PrimeMonitorApi) post(baseURL string, data interface{}) ([]byte, *APIError) {
|
2024-06-20 12:14:46 +05:30
|
|
|
jsonData, err := json.Marshal(data)
|
|
|
|
|
if err != nil {
|
2025-02-12 17:26:27 +05:30
|
|
|
return nil, &APIError{
|
|
|
|
|
Error: fmt.Sprintf("error marshaling data: %v", err),
|
|
|
|
|
Success: false,
|
|
|
|
|
}
|
2024-06-20 12:14:46 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
req, err := api.prepareRequest("POST", baseURL, bytes.NewBuffer(jsonData), nil)
|
|
|
|
|
if err != nil {
|
2025-02-12 17:26:27 +05:30
|
|
|
return nil, &APIError{
|
|
|
|
|
Error: fmt.Sprintf("error preparing request: %v", err),
|
|
|
|
|
Success: false,
|
|
|
|
|
}
|
2024-06-20 12:14:46 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return api.doRequest(req)
|
|
|
|
|
}
|