Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(SPV-802): refactor pike output method #95

Merged
merged 13 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
64 changes: 64 additions & 0 deletions capabilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ type CapabilitiesResponse struct {
type CapabilitiesPayload struct {
BsvAlias string `json:"bsvalias"` // Version of the bsvalias
Capabilities map[string]interface{} `json:"capabilities"` // Raw list of the capabilities
Pike *PikeCapability `json:"pike,omitempty"`
}

// PikeCapability represents the structure of the PIKE capability
type PikeCapability struct {
Invite string `json:"invite,omitempty"`
Outputs string `json:"outputs,omitempty"`
}

// PikeOutputs represents the structure of the PIKE outputs
type PikeOutputs struct {
URL string `json:"url"`
}

// Has will check if a BRFC ID (or alternate) is found in the list of capabilities
Expand Down Expand Up @@ -137,7 +149,59 @@ func (c *Client) GetCapabilities(target string, port int) (response *Capabilitie
// Invalid version detected
if len(response.BsvAlias) == 0 {
err = fmt.Errorf("missing %s version", DefaultServiceName)
return
}

// Parse PIKE capability
if err = parsePikeCapability(response); err != nil {
return
}

return
}

// ExtractPikeOutputsURL extracts the outputs URL from the PIKE capability
func (c *CapabilitiesPayload) ExtractPikeOutputsURL() string {
if c.Pike != nil {
return c.Pike.Outputs
}
return ""
}

// ExtractPikeInviteURL extracts the invite URL from the PIKE capability
func (c *CapabilitiesPayload) ExtractPikeInviteURL() string {
if c.Pike != nil {
return c.Pike.Invite
}
return ""
}

// parsePikeCapability parses the PIKE capability from the capabilities response
func parsePikeCapability(response *CapabilitiesResponse) error {
if pike, ok := response.Capabilities[BRFCPike].(map[string]interface{}); ok {
var (
invite, outputs string
errMsgs []string
)

if inviteStr, ok := pike["invite"].(string); ok {
invite = inviteStr
}

if outputsStr, ok := pike["outputs"].(string); ok {
outputs = outputsStr
} else {
errMsgs = append(errMsgs, "missing outputs URL in PIKE capability")
}

if len(errMsgs) > 0 {
return fmt.Errorf(strings.Join(errMsgs, "; "))
}

response.Pike = &PikeCapability{
Invite: invite,
Outputs: outputs,
}
}
return nil
}
Loading
Loading