Skip to content

Commit 53b91b7

Browse files
committed
feat(enmeshed): support nmshd::Connector API version 3.1.0
Main changes: - Relationship: Adapt parsing of the userdata to new schema Values to the requested attributes are now passed in `creationContent/response/items` (former: `changes/request/response/items`). - Connector: Adapt accepting/rejecting a Relationship to new endpoints `/api/v2/Relationships/#{relationship_id}/Changes/#{change_id}/#{action}` was dropped in favor of `/api/v2/Relationships/#{relationship_id}/Accept` (and `Reject`) Part of XI-6523
1 parent ef9dd87 commit 53b91b7

17 files changed

+1131
-1062
lines changed

lib/enmeshed/api_schema.yml

Lines changed: 887 additions & 358 deletions
Large diffs are not rendered by default.

lib/enmeshed/attribute.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,17 @@ def to_h
3636
default_attributes.deep_merge(additional_attributes)
3737
end
3838

39+
def to_json(*)
40+
{
41+
content: {
42+
value: {
43+
'@type': @type,
44+
value: @value,
45+
},
46+
},
47+
}.to_json(*)
48+
end
49+
3950
def id
4051
@id ||= persistent_id
4152
end

lib/enmeshed/connector.rb

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def enmeshed_address
2020
# @return [String] The ID of the created attribute.
2121
def create_attribute(attribute)
2222
response = connection.post('/api/v2/Attributes') do |request|
23-
request.body = {content: attribute.to_h}.to_json
23+
request.body = attribute.to_json
2424
end
2525
parse_result(response, Attribute).id
2626
end
@@ -67,14 +67,21 @@ def pending_relationships
6767
parse_result(response, Relationship)
6868
end
6969

70-
# @return [Boolean] Whether the relationship change was changed (accepted or rejected) successfully.
71-
def respond_to_rel_change(relationship_id, change_id, action = 'Accept')
72-
response =
73-
connection.put("/api/v2/Relationships/#{relationship_id}/Changes/#{change_id}/#{action}") do |request|
74-
request.body = {content: {}}.to_json
75-
end
70+
# @return [Boolean] Whether the relationship was accepted successfully.
71+
def accept_relationship(relationship_id)
72+
response = connection.put("/api/v2/Relationships/#{relationship_id}/Accept")
7673
Rails.logger.debug do
77-
"Enmeshed::ConnectorApi responded to RelationshipChange with: #{action}; " \
74+
"Enmeshed::ConnectorApi accepted the relationship; connector response status is #{response.status}"
75+
end
76+
77+
response.status == 200
78+
end
79+
80+
# @return [Boolean] Whether the relationship was rejected successfully.
81+
def reject_relationship(relationship_id)
82+
response = connection.put("/api/v2/Relationships/#{relationship_id}/Reject")
83+
Rails.logger.debug do
84+
'Enmeshed::ConnectorApi rejected the relationship; ' \
7885
"connector response status is #{response.status}"
7986
end
8087

lib/enmeshed/object.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,14 @@ def schema
1818
end
1919

2020
def validate!(instance)
21-
raise ConnectorError.new("Invalid #{klass} schema") unless schema.valid?(instance)
21+
unless schema.valid?(instance)
22+
error = schema.validate(instance).first.fetch('error')
23+
Rails.logger.debug(error)
24+
raise ConnectorError.new("Invalid #{klass} schema: #{error}") unless KNOWN_API_ISSUES.include? error
25+
end
2226
end
27+
28+
KNOWN_API_ISSUES = ['value at `/auditLog/0/createdBy` does not match format: date-time'].freeze
2329
end
2430
end
2531
end

lib/enmeshed/relationship.rb

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ class Relationship < Object
55
STATUS_GROUP_SYNONYMS = YAML.safe_load_file(Rails.root.join('lib/enmeshed/status_group_synonyms.yml'))
66

77
delegate :expires_at, :nbp_uid, :truncated_reference, to: :@template
8-
attr_reader :relationship_changes
8+
attr_reader :response_items
99

10-
def initialize(json:, template:, changes: [])
10+
def initialize(json:, template:, response_items: [])
1111
@json = json
1212
@template = template
13-
@relationship_changes = changes
13+
@response_items = response_items
1414
end
1515

1616
def peer
@@ -37,25 +37,19 @@ def id
3737
end
3838

3939
def accept!
40-
if relationship_changes.size != 1
41-
raise ConnectorError.new('Relationship should have exactly one RelationshipChange')
42-
end
43-
4440
Rails.logger.debug do
4541
"Enmeshed::ConnectorApi accepting Relationship for template #{truncated_reference}"
4642
end
4743

48-
Connector.respond_to_rel_change(id, relationship_changes.first[:id], 'Accept')
44+
Connector.accept_relationship(id)
4945
end
5046

5147
def reject!
5248
Rails.logger.debug do
5349
"Enmeshed::ConnectorApi rejecting Relationship for template #{truncated_reference}"
5450
end
5551

56-
@json[:changes].each do |change|
57-
Connector.respond_to_rel_change(id, change[:id], 'Reject')
58-
end
52+
Connector.reject_relationship(id)
5953
end
6054

6155
class << self
@@ -64,7 +58,7 @@ def parse(content)
6458
attributes = {
6559
json: content,
6660
template: RelationshipTemplate.parse(content[:template]),
67-
changes: content[:changes],
61+
response_items: content[:creationContent][:response][:items],
6862
}
6963
new(**attributes)
7064
end
@@ -80,17 +74,10 @@ def pending_for(nbp_uid)
8074
private
8175

8276
def parse_userdata # rubocop:disable Metrics/AbcSize
83-
# Since the RelationshipTemplate has a `maxNumberOfAllocations` attribute set to 1,
84-
# you cannot request multiple Relationships with the same template.
85-
# Further, RelationshipChanges should not be possible before accepting the Relationship.
86-
if relationship_changes.size != 1
87-
raise ConnectorError.new('Relationship should have exactly one RelationshipChange')
77+
user_provided_attributes = response_items.select do |item|
78+
item[:@type] == 'ReadAttributeAcceptResponseItem'
8879
end
8980

90-
change_response_items = relationship_changes.first.dig(:request, :content, :response, :items)
91-
92-
user_provided_attributes = change_response_items.select {|item| item[:@type] == 'ReadAttributeAcceptResponseItem' }
93-
9481
enmeshed_user_attributes = {}
9582

9683
user_provided_attributes.each do |item|
@@ -109,7 +96,7 @@ def parse_userdata # rubocop:disable Metrics/AbcSize
10996
status_group: parse_status_group(enmeshed_user_attributes['AffiliationRole'].downcase),
11097
}
11198
rescue NoMethodError
112-
raise ConnectorError.new("Could not parse userdata in relationship change: #{relationship_changes.first}")
99+
raise ConnectorError.new("Could not parse userdata in the response items: #{response_items}")
113100
end
114101

115102
def check_for_required_attributes!(enmeshed_user_attributes)

spec/fixtures/files/enmeshed/display_name_created.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"id": "ATT_id_of_a_new_name",
44
"content": {
55
"@type": "IdentityAttribute",
6-
"owner": "id1JttmYYvjT6wojkJ9rpaFkMUKHAWKr9RDf",
6+
"owner": "did:e:example.com:dids:checksum______________",
77
"value": {
88
"@type": "DisplayName",
99
"value": "CodeHarbor"

spec/fixtures/files/enmeshed/existing_display_name.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"id": "ATT_id_of_other_name",
55
"content": {
66
"@type": "IdentityAttribute",
7-
"owner": "id1GyHw8CefvC62VGQ5oTZjT3PvbveyDsrW2",
7+
"owner": "did:e:example.com:dids:checksum______________",
88
"value": {
99
"@type": "DisplayName",
1010
"value": "Some other display name"
@@ -16,7 +16,7 @@
1616
"id": "ATT_id_of_exist_name",
1717
"content": {
1818
"@type": "IdentityAttribute",
19-
"owner": "id1GyHw8CefvC62VGQ5oTZjT3PvbveyDsrW2",
19+
"owner": "did:e:example.com:dids:checksum______________",
2020
"value": {
2121
"@type": "DisplayName",
2222
"value": "CodeHarbor"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"result": {
3-
"address": "id_of_an_example_enmeshed_address_AB",
3+
"address": "did:e:example.com:dids:checksum______________",
44
"publicKey": "eyJwdWIiOiJZV2xVNDV1aEJsZmREa05iQ3RMSnhZaW1zckRGTlBXVHFfcjdLZUlYemN3IiwiYWxnIjozfQ"
55
}
66
}

spec/fixtures/files/enmeshed/invalid_role_relationship.json

Lines changed: 0 additions & 156 deletions
This file was deleted.

0 commit comments

Comments
 (0)