Files
telecom-platform/apps/api-server/internal/services/es2_queries.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

87 lines
2.5 KiB
Go

package services
import (
"context"
"encoding/json"
"fmt"
"net/http"
)
// GetProfileStatus retrieves the status of an eSIM profile
func (e *ES2Service) GetProfileStatus(ctx context.Context, euiccID, profileID string) (*ProfileInfo, error) {
url := fmt.Sprintf("%s/es2/eid/%s/profile/%s/status", e.config.BaseURL, euiccID, profileID)
httpReq, err := http.NewRequestWithContext(ctx, "GET", url, nil)
if err != nil {
return nil, fmt.Errorf("failed to create HTTP request: %w", err)
}
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 status 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 ProfileStatusResponse
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
return nil, fmt.Errorf("failed to decode status response: %w", err)
}
profileInfo := &ProfileInfo{
ICCID: response.ICCID,
ProfileID: response.ProfileID,
ProfileName: response.ProfileName,
State: response.State,
Operator: response.Operator,
}
return profileInfo, nil
}
// ListProfiles lists all profiles on an eUICC
func (e *ES2Service) ListProfiles(ctx context.Context, euiccID string) ([]*ProfileInfo, error) {
url := fmt.Sprintf("%s/es2/eid/%s/profiles", e.config.BaseURL, euiccID)
httpReq, err := http.NewRequestWithContext(ctx, "GET", url, nil)
if err != nil {
return nil, fmt.Errorf("failed to create HTTP request: %w", err)
}
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 list 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 ListProfilesResponse
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
return nil, fmt.Errorf("failed to decode list response: %w", err)
}
var profiles []*ProfileInfo
for _, profile := range response.Profiles {
profileInfo := &ProfileInfo{
ICCID: profile.ICCID,
ProfileID: profile.ProfileID,
ProfileName: profile.ProfileName,
State: profile.State,
Operator: profile.Operator,
}
profiles = append(profiles, profileInfo)
}
return profiles, nil
}