From 6cab60421c64d530724aedd2ff7731197a9888d1 Mon Sep 17 00:00:00 2001 From: Denys Smirnov Date: Mon, 4 Nov 2024 15:23:22 +0200 Subject: [PATCH] Common interface for update requests. --- livekit/sip.go | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) diff --git a/livekit/sip.go b/livekit/sip.go index f5445ce1..a2b77661 100644 --- a/livekit/sip.go +++ b/livekit/sip.go @@ -5,6 +5,8 @@ import ( "fmt" "slices" "strings" + + "google.golang.org/protobuf/proto" ) // ToProto implements DataPacket in Go SDK. @@ -287,6 +289,36 @@ func (p *SIPInboundTrunkUpdate) Apply(info *SIPInboundTrunkInfo) error { return info.Validate() } +type UpdateSIPOutboundTrunkRequestAction interface { + isUpdateSIPOutboundTrunkRequest_Action + Apply(info *SIPOutboundTrunkInfo) error +} + +var ( + _ UpdateSIPOutboundTrunkRequestAction = (*UpdateSIPOutboundTrunkRequest_Replace)(nil) + _ UpdateSIPOutboundTrunkRequestAction = (*UpdateSIPOutboundTrunkRequest_Update)(nil) +) + +func (p *UpdateSIPOutboundTrunkRequest_Replace) Apply(info *SIPOutboundTrunkInfo) error { + val := p.Replace + if val == nil { + return errors.New("missing trunk") + } + if info.SipTrunkId != "" { + val.SipTrunkId = info.SipTrunkId + } + proto.Merge(info, val) + return val.Validate() +} + +func (p *UpdateSIPOutboundTrunkRequest_Update) Apply(info *SIPOutboundTrunkInfo) error { + diff := p.Update + if diff == nil { + return errors.New("missing trunk update") + } + return diff.Apply(info) +} + func (p *SIPOutboundTrunkInfo) Validate() error { if len(p.Numbers) == 0 { return errors.New("no trunk numbers specified") @@ -326,6 +358,36 @@ func (p *SIPOutboundTrunkUpdate) Apply(info *SIPOutboundTrunkInfo) error { return info.Validate() } +type UpdateSIPInboundTrunkRequestAction interface { + isUpdateSIPInboundTrunkRequest_Action + Apply(info *SIPInboundTrunkInfo) error +} + +var ( + _ UpdateSIPInboundTrunkRequestAction = (*UpdateSIPInboundTrunkRequest_Replace)(nil) + _ UpdateSIPInboundTrunkRequestAction = (*UpdateSIPInboundTrunkRequest_Update)(nil) +) + +func (p *UpdateSIPInboundTrunkRequest_Replace) Apply(info *SIPInboundTrunkInfo) error { + val := p.Replace + if val == nil { + return errors.New("missing trunk") + } + if info.SipTrunkId != "" { + val.SipTrunkId = info.SipTrunkId + } + proto.Merge(info, val) + return val.Validate() +} + +func (p *UpdateSIPInboundTrunkRequest_Update) Apply(info *SIPInboundTrunkInfo) error { + diff := p.Update + if diff == nil { + return errors.New("missing trunk update") + } + return diff.Apply(info) +} + func (p *CreateSIPDispatchRuleRequest) DispatchRuleInfo() *SIPDispatchRuleInfo { if p == nil { return nil @@ -409,6 +471,36 @@ func (p *SIPDispatchRuleUpdate) Apply(info *SIPDispatchRuleInfo) error { return info.Validate() } +type UpdateSIPDispatchRuleRequestAction interface { + isUpdateSIPDispatchRuleRequest_Action + Apply(info *SIPDispatchRuleInfo) error +} + +var ( + _ UpdateSIPDispatchRuleRequestAction = (*UpdateSIPDispatchRuleRequest_Replace)(nil) + _ UpdateSIPDispatchRuleRequestAction = (*UpdateSIPDispatchRuleRequest_Update)(nil) +) + +func (p *UpdateSIPDispatchRuleRequest_Replace) Apply(info *SIPDispatchRuleInfo) error { + val := p.Replace + if val == nil { + return errors.New("missing dispatch rule") + } + if info.SipDispatchRuleId != "" { + val.SipDispatchRuleId = info.SipDispatchRuleId + } + proto.Merge(info, val) + return val.Validate() +} + +func (p *UpdateSIPDispatchRuleRequest_Update) Apply(info *SIPDispatchRuleInfo) error { + diff := p.Update + if diff == nil { + return errors.New("missing dispatch rule update") + } + return diff.Apply(info) +} + func (p *CreateSIPParticipantRequest) Validate() error { if p.SipTrunkId == "" { return errors.New("missing sip trunk id")