github.com/hyperledger/aries-framework-go@v0.3.2/pkg/ld/service.go (about) 1 /* 2 Copyright SecureKey Technologies Inc. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package ld 8 9 import ( 10 "fmt" 11 12 "github.com/hyperledger/aries-framework-go/pkg/doc/ldcontext" 13 "github.com/hyperledger/aries-framework-go/pkg/doc/ldcontext/remote" 14 "github.com/hyperledger/aries-framework-go/pkg/store/ld" 15 ) 16 17 // provider contains dependencies for the JSON-LD service. 18 type provider interface { 19 JSONLDContextStore() ld.ContextStore 20 JSONLDRemoteProviderStore() ld.RemoteProviderStore 21 } 22 23 // Service is a service that supports JSON-LD operations. 24 type Service interface { 25 AddContexts(documents []ldcontext.Document) error 26 AddRemoteProvider(providerEndpoint string, opts ...remote.ProviderOpt) (string, error) 27 RefreshRemoteProvider(providerID string, opts ...remote.ProviderOpt) error 28 DeleteRemoteProvider(providerID string, opts ...remote.ProviderOpt) error 29 GetAllRemoteProviders() ([]ld.RemoteProviderRecord, error) 30 RefreshAllRemoteProviders(opts ...remote.ProviderOpt) error 31 } 32 33 // DefaultService is a default implementation of Service. 34 type DefaultService struct { 35 contextStore ld.ContextStore 36 remoteProviderStore ld.RemoteProviderStore 37 } 38 39 // New returns a new default JSON-LD service instance. 40 func New(ctx provider) *DefaultService { 41 return &DefaultService{ 42 contextStore: ctx.JSONLDContextStore(), 43 remoteProviderStore: ctx.JSONLDRemoteProviderStore(), 44 } 45 } 46 47 // AddContexts adds JSON-LD contexts to the underlying storage. 48 func (s *DefaultService) AddContexts(documents []ldcontext.Document) error { 49 if err := s.contextStore.Import(documents); err != nil { 50 return fmt.Errorf("add contexts: %w", err) 51 } 52 53 return nil 54 } 55 56 // AddRemoteProvider adds remote provider and JSON-LD contexts from that provider. 57 func (s *DefaultService) AddRemoteProvider(providerEndpoint string, opts ...remote.ProviderOpt) (string, error) { 58 p := remote.NewProvider(providerEndpoint, opts...) 59 60 contexts, err := p.Contexts() 61 if err != nil { 62 return "", fmt.Errorf("get contexts from remote provider: %w", err) 63 } 64 65 record, err := s.remoteProviderStore.Save(providerEndpoint) 66 if err != nil { 67 return "", fmt.Errorf("save remote provider: %w", err) 68 } 69 70 if err := s.contextStore.Import(contexts); err != nil { 71 return "", fmt.Errorf("import contexts: %w", err) 72 } 73 74 return record.ID, nil 75 } 76 77 // RefreshRemoteProvider updates contexts from the remote provider. 78 func (s *DefaultService) RefreshRemoteProvider(providerID string, opts ...remote.ProviderOpt) error { 79 record, err := s.remoteProviderStore.Get(providerID) 80 if err != nil { 81 return fmt.Errorf("get remote provider from store: %w", err) 82 } 83 84 p := remote.NewProvider(record.Endpoint, opts...) 85 86 contexts, err := p.Contexts() 87 if err != nil { 88 return fmt.Errorf("get contexts from remote provider: %w", err) 89 } 90 91 if err := s.contextStore.Import(contexts); err != nil { 92 return fmt.Errorf("import contexts: %w", err) 93 } 94 95 return nil 96 } 97 98 // DeleteRemoteProvider deletes remote provider and contexts from that provider. 99 func (s *DefaultService) DeleteRemoteProvider(providerID string, opts ...remote.ProviderOpt) error { 100 record, err := s.remoteProviderStore.Get(providerID) 101 if err != nil { 102 return fmt.Errorf("get remote provider from store: %w", err) 103 } 104 105 p := remote.NewProvider(record.Endpoint, opts...) 106 107 contexts, err := p.Contexts() 108 if err != nil { 109 return fmt.Errorf("get contexts from remote provider: %w", err) 110 } 111 112 if err := s.contextStore.Delete(contexts); err != nil { 113 return fmt.Errorf("delete contexts: %w", err) 114 } 115 116 if err := s.remoteProviderStore.Delete(record.ID); err != nil { 117 return fmt.Errorf("delete remote provider record: %w", err) 118 } 119 120 return nil 121 } 122 123 // GetAllRemoteProviders gets all remote providers. 124 func (s *DefaultService) GetAllRemoteProviders() ([]ld.RemoteProviderRecord, error) { 125 records, err := s.remoteProviderStore.GetAll() 126 if err != nil { 127 return nil, fmt.Errorf("get remote provider records: %w", err) 128 } 129 130 return records, nil 131 } 132 133 // RefreshAllRemoteProviders updates contexts from all remote providers. 134 func (s *DefaultService) RefreshAllRemoteProviders(opts ...remote.ProviderOpt) error { 135 records, err := s.remoteProviderStore.GetAll() 136 if err != nil { 137 return fmt.Errorf("get remote provider records: %w", err) 138 } 139 140 for _, record := range records { 141 p := remote.NewProvider(record.Endpoint, opts...) 142 143 contexts, err := p.Contexts() 144 if err != nil { 145 return fmt.Errorf("get contexts from remote provider: %w", err) 146 } 147 148 if err := s.contextStore.Import(contexts); err != nil { 149 return fmt.Errorf("import contexts: %w", err) 150 } 151 } 152 153 return nil 154 }