Go API Documentation

github.com/TykTechnologies/tyk/internal/portal

No package summary is available.

Package

Files: 2. Third party imports: 3. Imports from organisation: 0. Tests: 0. Benchmarks: 0.

Constants

const applicationJSON = "application/json"

Types

App

App represents the structure of an application from the developer portal

Field name Field type Comment
ID

int

No comment on field.
Name

string

No comment on field.
Description

string

No comment on field.
UserID

int

No comment on field.
type App struct {
	ID		int	`json:"ID"`
	Name		string	`json:"Name"`
	Description	string	`json:"Description"`
	UserID		int	`json:"UserID"`
	// Assuming other fields based on the provided example
}

AppDetail

AppDetail includes detailed information about an application, including webhooks

Field name Field type Comment
ID

int

No comment on field.
AccessRequests

[]struct { WebhookEventTypes string `json:"WebhookEventTypes"` WebhookSecret string `json:"WebhookSecret"` WebhookURL string `json:"WebhookURL"` }

No comment on field.
type AppDetail struct {
	ID		int	`json:"ID"`
	AccessRequests	[]struct {
		WebhookEventTypes	string	`json:"WebhookEventTypes"`
		WebhookSecret		string	`json:"WebhookSecret"`
		WebhookURL		string	`json:"WebhookURL"`
	}	`json:"AccessRequests"`
	// Assuming other fields based on the provided example
}

Client

Client is a client application for the portal API

Field name Field type Comment
Secret

string

No comment on field.
BaseURL

string

No comment on field.
type Client struct {
	Secret	string
	BaseURL	string
}

WebhookCredential

WebhookCredential contains the necessary fields to describe a webhook

Field name Field type Comment
AppID

int

No comment on field.
AppName

string

No comment on field.
WebhookEventTypes

string

No comment on field.
WebhookSecret

string

No comment on field.
WebhookURL

string

No comment on field.
type WebhookCredential struct {
	AppID			int
	AppName			string
	WebhookEventTypes	string
	WebhookSecret		string
	WebhookURL		string
}

portalOutput

This type doesn't have documentation.

Field name Field type Comment
conf

*portalOutputConfig

No comment on field.
portalClient

*Client

No comment on field.
type portalOutput struct {
	conf		*portalOutputConfig
	portalClient	*Client
}

portalOutputConfig

This type doesn't have documentation.

Field name Field type Comment
PortalURL

string

No comment on field.
Secret

string

No comment on field.
EventType

string

No comment on field.
Headers

map[string]string

No comment on field.
type portalOutputConfig struct {
	PortalURL	string	`json:"portal_url"`
	Secret		string	`json:"secret"`
	EventType	string	`json:"event_type"`
	Headers		map[string]string
}

Functions

func NewClient

NewClient creates a new Client for interacting with the portal API

func NewClient(baseURL, secret string) *Client {
	if !strings.HasSuffix(baseURL, "/") {
		baseURL += "/"
	}
	if !strings.HasSuffix(baseURL, "portal-api/") {
		baseURL += "portal-api/"
	}
	baseURL = strings.TrimSuffix(baseURL, "/")

	return &Client{Secret: secret, BaseURL: baseURL}
}

Cognitive complexity: 5, Cyclomatic complexity: 3

Uses: strings.HasSuffix, strings.TrimSuffix.

func (*Client) ListWebhookCredentials

ListWebhookCredentials retrieves a list of apps and filters out their webhook credentials

func (client *Client) ListWebhookCredentials() ([]WebhookCredential, error) {
	var allApps []App
	for page := 1; ; page++ {
		apps, err := client.fetchApps(page)
		if err != nil {
			return nil, err
		}

		allApps = append(allApps, apps...)

		if len(apps) < 10 {
			break
		}
	}

	var webhookCredentials []WebhookCredential
	for _, app := range allApps {
		detail, err := client.fetchAppDetail(app.ID)
		if err != nil {
			return nil, err
		}

		for _, ar := range detail.AccessRequests {
			if ar.WebhookURL != "" {
				webhookCredentials = append(webhookCredentials, WebhookCredential{
					AppID:			app.ID,
					AppName:		app.Name,
					WebhookEventTypes:	ar.WebhookEventTypes,
					WebhookSecret:		ar.WebhookSecret,
					WebhookURL:		ar.WebhookURL,
				})
			}
		}
	}

	return webhookCredentials, nil
}

Cognitive complexity: 17, Cyclomatic complexity: 8

func (*portalOutput) Close

func (p *portalOutput) Close(_ context.Context) error {
	log.Println("Closing Portal Output")
	// Cleanup resources here if necessary
	return nil
}

Cognitive complexity: 0, Cyclomatic complexity: 1

Uses: log.Println.

func (*portalOutput) Connect

func (p *portalOutput) Connect(_ context.Context) error {
	log.Println("Connecting to Portal API")
	// Connection logic here if necessary, e.g., test connection or fetch token.
	// For simplicity, consider the portal client already prepared for requests.
	return nil
}

Cognitive complexity: 0, Cyclomatic complexity: 1

Uses: log.Println.

func (*portalOutput) Write

func (p *portalOutput) Write(_ context.Context, msg *service.Message) error {
	webhooks, err := p.portalClient.ListWebhookCredentials()
	if err != nil {
		return fmt.Errorf("failed to list webhook credentials: %w", err)
	}

	content, err := msg.AsBytes()
	if err != nil {
		return err
	}

	for _, webhook := range webhooks {
		if strings.Contains(webhook.WebhookEventTypes, p.conf.EventType) {
			go func(webhookURL string, content []byte) {
				if err := p.sendToWebhook(webhookURL, content); err != nil {
					log.Printf("failed to send event to webhook URL %s: %v", webhookURL, err)
				}
			}(webhook.WebhookURL, content)
		}
	}

	return nil
}

Cognitive complexity: 12, Cyclomatic complexity: 6

Uses: fmt.Errorf, log.Printf, strings.Contains.

Private functions

func init

init ()
References: service.Output, service.ParsedConfig, service.RegisterOutput, service.Resources.

func newPortalOutput

newPortalOutput (conf *portalOutputConfig, _ *service.Resources) *portalOutput

func newPortalOutputConfig

newPortalOutputConfig () *portalOutputConfig

func portalOutputConfigSpec

portalOutputConfigSpec () *service.ConfigSpec
References: service.NewConfigSpec, service.NewStringField, service.NewStringMapField.

func fetchAppDetail

fetchAppDetail (appID int) (*AppDetail, error)
References: context.Background, fmt.Errorf, fmt.Sprintf, http.DefaultClient, http.MethodGet, http.NewRequestWithContext, http.StatusOK, json.NewDecoder, log.Error.

func fetchApps

fetchApps (page int) ([]App, error)
References: context.Background, fmt.Errorf, fmt.Sprintf, http.DefaultClient, http.MethodGet, http.NewRequestWithContext, http.StatusOK, json.NewDecoder, log.Error.

func sendToWebhook

sendToWebhook (url string, data []byte) error
References: bytes.NewBuffer, context.Background, fmt.Errorf, http.Client, http.NewRequestWithContext, io.ReadAll, log.Printf.


Tests

Files: 2. Third party imports: 1. Imports from organisation: 0. Tests: 2. Benchmarks: 0.

Test functions

TestListWebhookCredentialsMultipleApps

References: httptest.NewServer.

TestPortalWebhookOutput

References: context.Background, http.HandlerFunc, http.Request, http.ResponseWriter, http.StatusNotFound, http.StatusOK, httptest.NewServer, service.NewStreamBuilder, sync.WaitGroup, time.After, time.Second.