Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Toktar committed Jul 13, 2022
1 parent 359e3e5 commit 6ee5f66
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
24 changes: 16 additions & 8 deletions services/ledger_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ func (ls LedgerService) QueryDIDDoc(did string) (cheqd.Did, cheqd.Metadata, bool
}

conn, err := ls.openGRPCConnection(serverAddr)
defer func(conn *grpc.ClientConn) {
if conn == nil { return }
err := conn.Close()
if err != nil {
log.Panic().Err(err).Msg("QueryDIDDoc: failed to close connection")
panic(err)
}
}(conn)
if err != nil {
log.Error().Err(err).Msg("QueryDIDDoc: failed connection")
return cheqd.Did{}, cheqd.Metadata{}, false, err
Expand All @@ -69,6 +77,14 @@ func (ls LedgerService) QueryResource(collectionDid string, resourceId string) (
}

conn, err := ls.openGRPCConnection(serverAddr)
defer func(conn *grpc.ClientConn) {
if conn == nil { return }
err := conn.Close()
if err != nil {
log.Panic().Err(err).Msg("QueryResource: failed to close connection")
panic(err)
}
}(conn)
if err != nil {
log.Error().Err(err).Msg("QueryResource: failed connection")
return resource.Resource{}, false, err
Expand Down Expand Up @@ -122,14 +138,6 @@ func (ls LedgerService) openGRPCConnection(addr string) (conn *grpc.ClientConn,
return nil, err
}

defer func(conn *grpc.ClientConn) {
err := conn.Close()
if err != nil {
log.Panic().Err(err).Msg("QueryDIDDoc: failed to close connection")
panic(err)
}
}(conn)

log.Info().Msg("openGRPCConnection: opened")
return conn, nil
}
Expand Down
2 changes: 0 additions & 2 deletions services/request_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
// jsonpb Marshaller is deprecated, but is needed because there's only one way to proto
// marshal in combination with our proto generator version
"encoding/json"
"fmt"

"github.com/rs/zerolog/log"

Expand Down Expand Up @@ -101,7 +100,6 @@ func (rs RequestService) Resolve(did string, resolutionOptions types.ResolutionO
didResolutionMetadata := types.NewResolutionMetadata(did, resolutionOptions.Accept, "")

if didMethod, _, _, _ := cheqdUtils.TrySplitDID(did); didMethod != rs.didMethod {
fmt.Print("resolution: " + did)
didResolutionMetadata.ResolutionError = types.ResolutionMethodNotSupported
return types.DidResolution{ResolutionMetadata: didResolutionMetadata}, nil
}
Expand Down
5 changes: 2 additions & 3 deletions utils/did_url.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ import (
var ResourcePath, _ = regexp.Compile(`resource\/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}`)

func GetResourceId(didUrlPath string) (id string) {
if !ResourcePath.Match([]byte(didUrlPath)) {
match := ResourcePath.FindStringSubmatch(didUrlPath)
if len(match) != 1 {
return ""
}

match := ResourcePath.FindStringSubmatch(didUrlPath)
return match[0]
}

0 comments on commit 6ee5f66

Please sign in to comment.