Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions go/apps.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package phala

import (
"context"
"fmt"
"net/url"
)

// GetAppList returns the list of applications.
func (c *Client) GetAppList(ctx context.Context) (*GetAppListResponse, error) {
var result GetAppListResponse
if err := c.doJSON(ctx, "GET", "/apps", nil, &result); err != nil {
return nil, err
}
return &result, nil
}

// GetAppInfo returns information about a specific application.
func (c *Client) GetAppInfo(ctx context.Context, appID string) (*AppInfo, error) {
var result AppInfo
if err := c.doJSON(ctx, "GET", "/apps/"+appID, nil, &result); err != nil {
return nil, err
}
return &result, nil
}

// GetAppCVMs returns CVMs associated with an application.
func (c *Client) GetAppCVMs(ctx context.Context, appID string) ([]GenericObject, error) {
var result []GenericObject
if err := c.doJSON(ctx, "GET", "/apps/"+appID+"/cvms", nil, &result); err != nil {
return nil, err
}
return result, nil
}

// GetAppRevisions returns revisions for an application.
func (c *Client) GetAppRevisions(ctx context.Context, appID string, opts *PaginationOptions) (*AppRevisionsResponse, error) {
path := "/apps/" + appID + "/revisions"
if opts != nil {
q := url.Values{}
if opts.Page != nil {
q.Set("page", fmt.Sprintf("%d", *opts.Page))
}
if opts.PageSize != nil {
q.Set("page_size", fmt.Sprintf("%d", *opts.PageSize))
}
if encoded := q.Encode(); encoded != "" {
path += "?" + encoded
}
}
var result AppRevisionsResponse
if err := c.doJSON(ctx, "GET", path, nil, &result); err != nil {
return nil, err
}
return &result, nil
}

// GetAppRevisionDetail returns detailed information about a specific revision.
func (c *Client) GetAppRevisionDetail(ctx context.Context, appID, revisionID string) (*AppRevisionDetail, error) {
var result AppRevisionDetail
path := fmt.Sprintf("/apps/%s/revisions/%s", appID, revisionID)
if err := c.doJSON(ctx, "GET", path, nil, &result); err != nil {
return nil, err
}
return &result, nil
}

// GetAppAttestation returns attestation data for an application.
func (c *Client) GetAppAttestation(ctx context.Context, appID string) (*AppAttestationResponse, error) {
var result AppAttestationResponse
if err := c.doJSON(ctx, "GET", "/apps/"+appID+"/attestations", nil, &result); err != nil {
return nil, err
}
return &result, nil
}

// GetAppDeviceAllowlist returns the device allowlist for an application.
func (c *Client) GetAppDeviceAllowlist(ctx context.Context, appID string) (*DeviceAllowlistResponse, error) {
var result DeviceAllowlistResponse
if err := c.doJSON(ctx, "GET", "/apps/"+appID+"/device-allowlist", nil, &result); err != nil {
return nil, err
}
return &result, nil
}

// GetAppFilterOptions returns filter options for application listings.
func (c *Client) GetAppFilterOptions(ctx context.Context) (*AppFilterOptions, error) {
var result AppFilterOptions
if err := c.doJSON(ctx, "GET", "/apps/filter-options", nil, &result); err != nil {
return nil, err
}
return &result, nil
}
12 changes: 12 additions & 0 deletions go/auth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package phala

import "context"

// GetCurrentUser returns the currently authenticated user.
func (c *Client) GetCurrentUser(ctx context.Context) (*CurrentUser, error) {
var result CurrentUser
if err := c.doJSON(ctx, "GET", "/auth/me", nil, &result); err != nil {
return nil, err
}
return &result, nil
}
56 changes: 56 additions & 0 deletions go/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package phala

import (
"fmt"
"net/http"
"os"
"strings"
)

// Client is the Phala Cloud API client.
type Client struct {
baseURL string
apiKey string
apiVersion string
httpClient *http.Client
userAgent string
headers map[string]string
maxRetries int
}

// NewClient creates a new Phala Cloud API client with the given options.
// API key is resolved from options or the PHALA_CLOUD_API_KEY environment variable.
// Base URL is resolved from options or the PHALA_CLOUD_API_PREFIX environment variable.
func NewClient(opts ...Option) (*Client, error) {
c := &Client{
baseURL: DefaultBaseURL,
apiVersion: DefaultAPIVersion,
httpClient: &http.Client{},
userAgent: "phala-cloud-sdk-go/" + sdkVersion,
headers: make(map[string]string),
maxRetries: 30,
}

for _, opt := range opts {
opt(c)
}

// Environment variable fallbacks.
if c.apiKey == "" {
c.apiKey = os.Getenv("PHALA_CLOUD_API_KEY")
}
if envURL := os.Getenv("PHALA_CLOUD_API_PREFIX"); envURL != "" {
// Only use env URL if no explicit option was set (check if still default).
if c.baseURL == DefaultBaseURL {
c.baseURL = envURL
}
}

if c.apiKey == "" {
return nil, fmt.Errorf("phala: API key is required (set via WithAPIKey or PHALA_CLOUD_API_KEY)")
}

c.baseURL = strings.TrimRight(c.baseURL, "/")

return c, nil
}
49 changes: 49 additions & 0 deletions go/client_options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package phala

import (
"net/http"
"time"
)

// Option configures the Client.
type Option func(*Client)

// WithAPIKey sets the API key for authentication.
func WithAPIKey(key string) Option {
return func(c *Client) { c.apiKey = key }
}

// WithBaseURL sets the base URL for the API.
func WithBaseURL(url string) Option {
return func(c *Client) { c.baseURL = url }
}

// WithAPIVersion sets the API version header value.
func WithAPIVersion(v string) Option {
return func(c *Client) { c.apiVersion = v }
}

// WithHTTPClient sets a custom HTTP client.
func WithHTTPClient(hc *http.Client) Option {
return func(c *Client) { c.httpClient = hc }
}

// WithTimeout sets the HTTP client timeout.
func WithTimeout(d time.Duration) Option {
return func(c *Client) { c.httpClient.Timeout = d }
}

// WithUserAgent sets a custom User-Agent header.
func WithUserAgent(ua string) Option {
return func(c *Client) { c.userAgent = ua }
}

// WithHeader adds a custom header to all requests.
func WithHeader(key, value string) Option {
return func(c *Client) { c.headers[key] = value }
}

// WithMaxRetries sets the maximum number of retries for retryable errors.
func WithMaxRetries(n int) Option {
return func(c *Client) { c.maxRetries = n }
}
Loading
Loading