forked from rapidpro/mailroom
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
114 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package models | ||
|
||
import ( | ||
"database/sql/driver" | ||
"net/http" | ||
|
||
"github.com/nyaruka/gocommon/httpx" | ||
"github.com/nyaruka/goflow/assets" | ||
"github.com/nyaruka/goflow/flows" | ||
"github.com/nyaruka/goflow/flows/engine" | ||
"github.com/nyaruka/mailroom/core/goflow" | ||
"github.com/nyaruka/mailroom/runtime" | ||
"github.com/nyaruka/null" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
type OrgContextID null.Int | ||
|
||
func (i OrgContextID) MarshalJSON() ([]byte, error) { | ||
return null.Int(i).MarshalJSON() | ||
} | ||
|
||
func (i *OrgContextID) UnmarshalJSON(b []byte) error { | ||
return null.UnmarshalInt(b, (*null.Int)(i)) | ||
} | ||
|
||
func (i OrgContextID) Value() (driver.Value, error) { | ||
return null.Int(i).Value() | ||
} | ||
|
||
func (i *OrgContextID) Scan(value interface{}) error { | ||
return null.ScanInt(value, (*null.Int)(i)) | ||
} | ||
|
||
func init() { | ||
goflow.RegisterOrgContextServiceFactory(orgContextServiceFactory) | ||
} | ||
|
||
func orgContextServiceFactory(c *runtime.Config) engine.OrgContextServiceFactory { | ||
return func(session flows.Session, orgContext *flows.OrgContext) (flows.OrgContextService, error) { | ||
return orgContext.Asset().(*OrgContext).AsService(c, orgContext) | ||
} | ||
} | ||
|
||
type OrgContext struct { | ||
c struct { | ||
OrgContext string `json:"context"` | ||
ChannelUUID assets.ChannelUUID `json:"channel_uuid"` | ||
} | ||
} | ||
|
||
func (c *OrgContext) Context() string { return c.c.OrgContext } | ||
func (c *OrgContext) ChannelUUID() assets.ChannelUUID { return c.c.ChannelUUID } | ||
|
||
type OrgContextService interface { | ||
flows.OrgContextService | ||
} | ||
|
||
func (c *OrgContext) AsService(cfg *runtime.Config, context *flows.OrgContext) (OrgContextService, error) { | ||
httpClient, httpRetries, _ := goflow.HTTP(cfg) | ||
|
||
initFunc := orgContextServices["org_context"] | ||
if initFunc != nil { | ||
return initFunc(cfg, httpClient, httpRetries, context, nil) | ||
} | ||
|
||
return nil, errors.Errorf("unrecognized context %s", c.Context()) | ||
} | ||
|
||
type OrgContextServiceFunc func(*runtime.Config, *http.Client, *httpx.RetryConfig, *flows.OrgContext, map[string]string) (OrgContextService, error) | ||
|
||
var orgContextServices = map[string]OrgContextServiceFunc{} | ||
|
||
func RegisterContextService(name string, initFunc OrgContextServiceFunc) { | ||
orgContextServices[name] = initFunc | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters