Files
telecom-platform/apps/api-server/internal/services/es2_operations.go
T
nutcas3 3a3b4d93a0 refactor: Split ES2 client into separate files for better code organization
- Move ES2 request/response types to es2_types.go
- Move profile operations (download, activate, deactivate, delete, status, list) to es2_operations.go
- Move helper methods (send requests, validation, generateRequestID) to es2_helpers.go
- Keep core ES2Service struct and ProvisionProfile in es2_client.go
- Revert sendDeactivationRequest parameter type from any back to interface{}
2026-04-14 16:22:07 +03:00

75 lines
2.0 KiB
Go

package services
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
)
// downloadProfile is an internal method to download a profile
func (e *ES2Service) downloadProfile(ctx context.Context, req DownloadOrderRequest) (*DownloadOrderResponse, error) {
jsonData, err := json.Marshal(req)
if err != nil {
return nil, fmt.Errorf("failed to marshal download request: %w", err)
}
url := fmt.Sprintf("%s/es2/download", e.config.BaseURL)
httpReq, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewBuffer(jsonData))
if err != nil {
return nil, fmt.Errorf("failed to create HTTP request: %w", err)
}
httpReq.Header.Set("Content-Type", "application/json")
httpReq.Header.Set("X-Request-ID", generateRequestID())
httpReq.Header.Set("Authorization", "Bearer "+e.config.APIKey)
resp, err := e.httpClient.Do(httpReq)
if err != nil {
return nil, fmt.Errorf("failed to send download request: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("ES2 server returned status %d", resp.StatusCode)
}
var response DownloadOrderResponse
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
return nil, fmt.Errorf("failed to decode download response: %w", err)
}
return &response, nil
}
// ActivateProfile activates an eSIM profile
func (e *ES2Service) ActivateProfile(ctx context.Context, euiccID, profileID string) error {
req := ActivationRequest{
EID: euiccID,
ProfileID: profileID,
}
return e.sendActivationRequest(ctx, req, "activate")
}
// DeactivateProfile deactivates an eSIM profile
func (e *ES2Service) DeactivateProfile(ctx context.Context, euiccID, profileID string) error {
req := DeactivationRequest{
EID: euiccID,
ProfileID: profileID,
}
return e.sendDeactivationRequest(ctx, req)
}
// DeleteProfile deletes an eSIM profile
func (e *ES2Service) DeleteProfile(ctx context.Context, euiccID, profileID string) error {
req := DeletionRequest{
EID: euiccID,
ProfileID: profileID,
}
return e.sendDeletionRequest(ctx, req)
}