Issue 540 - refactor: migrate DeDi registry plugin to new wrapper API format
This commit is contained in:
@@ -21,10 +21,7 @@ func (d dediRegistryProvider) New(ctx context.Context, config map[string]string)
|
|||||||
|
|
||||||
// Create dediregistry.Config directly from map - validation is handled by dediregistry.New
|
// Create dediregistry.Config directly from map - validation is handled by dediregistry.New
|
||||||
dediConfig := &dediregistry.Config{
|
dediConfig := &dediregistry.Config{
|
||||||
BaseURL: config["baseURL"],
|
BaseURL: config["baseURL"],
|
||||||
ApiKey: config["apiKey"],
|
|
||||||
NamespaceID: config["namespaceID"],
|
|
||||||
RegistryName: config["registryName"],
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse timeout if provided
|
// Parse timeout if provided
|
||||||
|
|||||||
@@ -15,11 +15,8 @@ import (
|
|||||||
|
|
||||||
// Config holds configuration parameters for the DeDi registry client.
|
// Config holds configuration parameters for the DeDi registry client.
|
||||||
type Config struct {
|
type Config struct {
|
||||||
BaseURL string `yaml:"baseURL" json:"baseURL"`
|
BaseURL string `yaml:"baseURL" json:"baseURL"`
|
||||||
ApiKey string `yaml:"apiKey" json:"apiKey"`
|
Timeout int `yaml:"timeout" json:"timeout"`
|
||||||
NamespaceID string `yaml:"namespaceID" json:"namespaceID"`
|
|
||||||
RegistryName string `yaml:"registryName" json:"registryName"`
|
|
||||||
Timeout int `yaml:"timeout" json:"timeout"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeDiRegistryClient encapsulates the logic for calling the DeDi registry endpoints.
|
// DeDiRegistryClient encapsulates the logic for calling the DeDi registry endpoints.
|
||||||
@@ -36,16 +33,6 @@ func validate(cfg *Config) error {
|
|||||||
if cfg.BaseURL == "" {
|
if cfg.BaseURL == "" {
|
||||||
return fmt.Errorf("baseURL cannot be empty")
|
return fmt.Errorf("baseURL cannot be empty")
|
||||||
}
|
}
|
||||||
if cfg.ApiKey == "" {
|
|
||||||
return fmt.Errorf("apiKey cannot be empty")
|
|
||||||
}
|
|
||||||
if cfg.NamespaceID == "" {
|
|
||||||
return fmt.Errorf("namespaceID cannot be empty")
|
|
||||||
}
|
|
||||||
if cfg.RegistryName == "" {
|
|
||||||
return fmt.Errorf("registryName cannot be empty")
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -82,24 +69,27 @@ func New(ctx context.Context, cfg *Config) (*DeDiRegistryClient, func() error, e
|
|||||||
return client, closer, nil
|
return client, closer, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Lookup implements RegistryLookup interface - calls the DeDi lookup endpoint and returns Subscription.
|
// Lookup implements RegistryLookup interface - calls the DeDi wrapper lookup endpoint and returns Subscription.
|
||||||
func (c *DeDiRegistryClient) Lookup(ctx context.Context, req *model.Subscription) ([]model.Subscription, error) {
|
func (c *DeDiRegistryClient) Lookup(ctx context.Context, req *model.Subscription) ([]model.Subscription, error) {
|
||||||
// Extract subscriber ID from request
|
// Extract subscriber ID and key ID from request (both come from Authorization header parsing)
|
||||||
subscriberID := req.SubscriberID
|
subscriberID := req.SubscriberID
|
||||||
log.Infof(ctx, "DeDI Registry: Looking up subscriber ID: %s", subscriberID)
|
keyID := req.KeyID
|
||||||
|
log.Infof(ctx, "DeDi Registry: Looking up subscriber ID: %s, key ID: %s", subscriberID, keyID)
|
||||||
if subscriberID == "" {
|
if subscriberID == "" {
|
||||||
return nil, fmt.Errorf("subscriber_id is required for DeDi lookup")
|
return nil, fmt.Errorf("subscriber_id is required for DeDi lookup")
|
||||||
}
|
}
|
||||||
|
if keyID == "" {
|
||||||
|
return nil, fmt.Errorf("key_id is required for DeDi lookup")
|
||||||
|
}
|
||||||
|
|
||||||
lookupURL := fmt.Sprintf("%s/dedi/lookup/%s/%s/%s",
|
lookupURL := fmt.Sprintf("%s/dedi/lookup/%s/subscribers.beckn.one/%s",
|
||||||
c.config.BaseURL, c.config.NamespaceID, c.config.RegistryName, subscriberID)
|
c.config.BaseURL, subscriberID, keyID)
|
||||||
|
|
||||||
httpReq, err := retryablehttp.NewRequest("GET", lookupURL, nil)
|
httpReq, err := retryablehttp.NewRequest("GET", lookupURL, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to create request: %w", err)
|
return nil, fmt.Errorf("failed to create request: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
httpReq.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.config.ApiKey))
|
|
||||||
httpReq = httpReq.WithContext(ctx)
|
httpReq = httpReq.WithContext(ctx)
|
||||||
|
|
||||||
log.Debugf(ctx, "Making DeDi lookup request to: %s", lookupURL)
|
log.Debugf(ctx, "Making DeDi lookup request to: %s", lookupURL)
|
||||||
@@ -127,60 +117,65 @@ func (c *DeDiRegistryClient) Lookup(ctx context.Context, req *model.Subscription
|
|||||||
return nil, fmt.Errorf("failed to unmarshal response body: %w", err)
|
return nil, fmt.Errorf("failed to unmarshal response body: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Debugf(ctx, "DeDi lookup request successful")
|
log.Debugf(ctx, "DeDi lookup request successful, parsing response")
|
||||||
|
|
||||||
// Extract data field
|
// Extract data field
|
||||||
data, ok := responseData["data"].(map[string]interface{})
|
data, ok := responseData["data"].(map[string]interface{})
|
||||||
if !ok {
|
if !ok {
|
||||||
|
log.Errorf(ctx, nil, "Invalid DeDi response format: missing or invalid data field")
|
||||||
return nil, fmt.Errorf("invalid response format: missing data field")
|
return nil, fmt.Errorf("invalid response format: missing data field")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Extract details field
|
// Extract details field
|
||||||
details, ok := data["details"].(map[string]interface{})
|
details, ok := data["details"].(map[string]interface{})
|
||||||
if !ok {
|
if !ok {
|
||||||
|
log.Errorf(ctx, nil, "Invalid DeDi response format: missing or invalid details field")
|
||||||
return nil, fmt.Errorf("invalid response format: missing details field")
|
return nil, fmt.Errorf("invalid response format: missing details field")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Extract required fields from details
|
// Extract required fields from details
|
||||||
keyID, _ := details["key_id"].(string)
|
|
||||||
signingPublicKey, ok := details["signing_public_key"].(string)
|
signingPublicKey, ok := details["signing_public_key"].(string)
|
||||||
if !ok || signingPublicKey == "" {
|
if !ok || signingPublicKey == "" {
|
||||||
return nil, fmt.Errorf("invalid or missing signing_public_key in response")
|
return nil, fmt.Errorf("invalid or missing signing_public_key in response")
|
||||||
}
|
}
|
||||||
encrPublicKey, _ := details["encr_public_key"].(string)
|
|
||||||
detailsStatus, ok := details["status"].(string)
|
|
||||||
if !ok {
|
|
||||||
return nil, fmt.Errorf("missing status in response")
|
|
||||||
}
|
|
||||||
detailsCreated, _ := details["created"].(string)
|
|
||||||
detailsUpdated, _ := details["updated"].(string)
|
|
||||||
validFromStr, _ := details["valid_from"].(string)
|
|
||||||
validUntilStr, _ := details["valid_until"].(string)
|
|
||||||
|
|
||||||
// Extract record_name as subscriber ID
|
// Extract fields from details
|
||||||
recordName, _ := data["record_name"].(string)
|
detailsURL, _ := details["url"].(string)
|
||||||
if recordName == "" {
|
detailsType, _ := details["type"].(string)
|
||||||
recordName = subscriberID
|
detailsDomain, _ := details["domain"].(string)
|
||||||
|
detailsSubscriberID, _ := details["subscriber_id"].(string)
|
||||||
|
|
||||||
|
// Extract encr_public_key if available (optional field)
|
||||||
|
encrPublicKey, _ := details["encr_public_key"].(string)
|
||||||
|
|
||||||
|
// Extract fields from data level
|
||||||
|
createdAt, _ := data["created_at"].(string)
|
||||||
|
updatedAt, _ := data["updated_at"].(string)
|
||||||
|
isRevoked, _ := data["is_revoked"].(bool)
|
||||||
|
|
||||||
|
// Determine status from is_revoked flag
|
||||||
|
status := "SUBSCRIBED"
|
||||||
|
if isRevoked {
|
||||||
|
status = "UNSUBSCRIBED"
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert to Subscription format
|
// Convert to Subscription format
|
||||||
subscription := model.Subscription{
|
subscription := model.Subscription{
|
||||||
Subscriber: model.Subscriber{
|
Subscriber: model.Subscriber{
|
||||||
SubscriberID: recordName,
|
SubscriberID: detailsSubscriberID,
|
||||||
URL: req.URL,
|
URL: detailsURL,
|
||||||
Domain: req.Domain,
|
Domain: detailsDomain,
|
||||||
Type: req.Type,
|
Type: detailsType,
|
||||||
},
|
},
|
||||||
KeyID: keyID,
|
KeyID: keyID, // Use original keyID from request
|
||||||
SigningPublicKey: signingPublicKey,
|
SigningPublicKey: signingPublicKey,
|
||||||
EncrPublicKey: encrPublicKey,
|
EncrPublicKey: encrPublicKey, // May be empty if not provided
|
||||||
ValidFrom: parseTime(validFromStr),
|
Status: status,
|
||||||
ValidUntil: parseTime(validUntilStr),
|
Created: parseTime(createdAt),
|
||||||
Status: detailsStatus,
|
Updated: parseTime(updatedAt),
|
||||||
Created: parseTime(detailsCreated),
|
|
||||||
Updated: parseTime(detailsUpdated),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.Debugf(ctx, "DeDi lookup successful, found subscription for subscriber: %s", detailsSubscriberID)
|
||||||
return []model.Subscription{subscription}, nil
|
return []model.Subscription{subscription}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user