Skip to content
This repository was archived by the owner on Jan 29, 2026. It is now read-only.

Commit 521a647

Browse files
committed
Add additional helpers
1 parent a85b2b6 commit 521a647

File tree

3 files changed

+91
-10
lines changed

3 files changed

+91
-10
lines changed

audit/client.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ func (c *Client) NewAuditRequest(method, path string, bodyBytes []byte, options
168168

169169
req.Header.Set("Accept", "*/*")
170170
req.Header.Set("API-Version", APIVersion)
171+
req.Header.Set("Content-Type", "application/json")
171172

172173
if c.UserAgent != "" {
173174
req.Header.Set("User-Agent", c.UserAgent)

audit/helper/fhir/dstu2/audit_event.go

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
package dstu2
22

33
import (
4+
"time"
5+
46
dstu2dt "github.com/google/fhir/go/proto/google/fhir/proto/dstu2/datatypes_go_proto"
57
dstu2pb "github.com/google/fhir/go/proto/google/fhir/proto/dstu2/resources_go_proto"
68
)
79

8-
type WithFunc func(sub *dstu2pb.AuditEvent) error
10+
type OptionFunc func(sub *dstu2pb.AuditEvent) error
911

1012
// NewAuditEvent creates a new audit event. It takes
1113
// productKey and tenant as arguments as these are required
1214
// for publishing to to Host Auditing service
13-
func NewAuditEvent(productKey, tenant string, options ...WithFunc) (*dstu2pb.AuditEvent, error) {
15+
func NewAuditEvent(productKey, tenant string, options ...OptionFunc) (*dstu2pb.AuditEvent, error) {
1416
event := &dstu2pb.AuditEvent{}
1517

16-
if err := WithSourceExtensionUriValue("productKey", productKey)(event); err != nil {
18+
if err := AddSourceExtensionUriValue("productKey", productKey)(event); err != nil {
1719
return nil, err
1820
}
19-
if err := WithSourceExtensionUriValue("tenant", tenant)(event); err != nil {
21+
if err := AddSourceExtensionUriValue("tenant", tenant)(event); err != nil {
2022
return nil, err
2123
}
2224
for _, w := range options {
@@ -27,8 +29,35 @@ func NewAuditEvent(productKey, tenant string, options ...WithFunc) (*dstu2pb.Aud
2729
return event, nil
2830
}
2931

30-
// WithObject adds the passed object to the AuditEvent
31-
func WithObject(object *dstu2pb.AuditEvent_Object) WithFunc {
32+
// WithEvent sets the event
33+
func WithEvent(e *dstu2pb.AuditEvent_Event) OptionFunc {
34+
return func(event *dstu2pb.AuditEvent) error {
35+
event.Event = e
36+
return nil
37+
}
38+
}
39+
40+
// DateTime returns DateTime
41+
func DateTime(at time.Time) *dstu2dt.Instant {
42+
return &dstu2dt.Instant{
43+
Precision: dstu2dt.Instant_MICROSECOND,
44+
ValueUs: at.UnixNano() / 1000,
45+
}
46+
}
47+
48+
// AddParticipant adds the participant
49+
func AddParticipant(participant *dstu2pb.AuditEvent_Participant) OptionFunc {
50+
return func(event *dstu2pb.AuditEvent) error {
51+
if event.Participant == nil {
52+
event.Participant = []*dstu2pb.AuditEvent_Participant{}
53+
}
54+
event.Participant = append(event.Participant, participant)
55+
return nil
56+
}
57+
}
58+
59+
// AddObject adds the passed object to the AuditEvent
60+
func AddObject(object *dstu2pb.AuditEvent_Object) OptionFunc {
3261
return func(event *dstu2pb.AuditEvent) error {
3362
if event.Object == nil {
3463
event.Object = []*dstu2pb.AuditEvent_Object{}
@@ -38,9 +67,20 @@ func WithObject(object *dstu2pb.AuditEvent_Object) WithFunc {
3867
}
3968
}
4069

41-
// WithExtensionUriValue sets extension Uri/Value tuples, some of which are mandatory
70+
// WithSourceIdentifier sets the source identifier
71+
func WithSourceIdentifier(identifier *dstu2dt.Identifier) OptionFunc {
72+
return func(event *dstu2pb.AuditEvent) error {
73+
if event.Source == nil {
74+
event.Source = &dstu2pb.AuditEvent_Source{}
75+
}
76+
event.Source.Identifier = identifier
77+
return nil
78+
}
79+
}
80+
81+
// AddSourceExtensionUriValue sets extension Uri/Value tuples, some of which are mandatory
4282
// for successfully posting to HSDP Audit
43-
func WithSourceExtensionUriValue(extensionUri, extensionValue string) WithFunc {
83+
func AddSourceExtensionUriValue(extensionUri, extensionValue string) OptionFunc {
4484
return func(event *dstu2pb.AuditEvent) error {
4585
if event.Source == nil {
4686
event.Source = &dstu2pb.AuditEvent_Source{}
@@ -58,7 +98,7 @@ func WithSourceExtensionUriValue(extensionUri, extensionValue string) WithFunc {
5898
}
5999
if ext == nil {
60100
ext = &dstu2dt.Extension{
61-
Url: &dstu2dt.Uri{},
101+
Url: &dstu2dt.Uri{Value: "/fhir/device"},
62102
}
63103
event.Source.Extension = append(event.Source.Extension, ext)
64104
}

audit/helper/fhir/dstu2/audit_event_test.go

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,53 @@ package dstu2_test
22

33
import (
44
"testing"
5+
"time"
6+
7+
dstu2pb "github.com/google/fhir/go/proto/google/fhir/proto/dstu2/resources_go_proto"
8+
9+
dstu2ct "github.com/google/fhir/go/proto/google/fhir/proto/dstu2/codes_go_proto"
10+
dstu2dt "github.com/google/fhir/go/proto/google/fhir/proto/dstu2/datatypes_go_proto"
511

612
"github.com/philips-software/go-hsdp-api/audit/helper/fhir/dstu2"
713
"github.com/stretchr/testify/assert"
814
)
915

1016
func TestNewAuditEvent(t *testing.T) {
11-
event, err := dstu2.NewAuditEvent("key", "tenant")
17+
event, err := dstu2.NewAuditEvent("key", "tenant",
18+
dstu2.WithSourceIdentifier(&dstu2dt.Identifier{
19+
Value: &dstu2dt.String{Value: "smokeuser@philips.com"},
20+
Type: &dstu2dt.CodeableConcept{
21+
Coding: []*dstu2dt.Coding{
22+
{
23+
System: &dstu2dt.Uri{Value: "http://hl7.org/fhir/ValueSet/identifier-type"},
24+
Code: &dstu2dt.Code{Value: "4"},
25+
Display: &dstu2dt.String{Value: "application server"},
26+
},
27+
},
28+
},
29+
}),
30+
dstu2.WithEvent(&dstu2pb.AuditEvent_Event{
31+
Action: &dstu2ct.AuditEventActionCode{
32+
Value: dstu2ct.AuditEventActionCode_E,
33+
},
34+
DateTime: dstu2.DateTime(time.Now()),
35+
Type: &dstu2dt.Coding{
36+
System: &dstu2dt.Uri{Value: "http://hl7.org/fhir/ValueSet/audit-event-type"},
37+
Version: &dstu2dt.String{Value: "1"},
38+
Code: &dstu2dt.Code{Value: "11011"},
39+
Display: &dstu2dt.String{Value: "Testing"},
40+
},
41+
Outcome: &dstu2ct.AuditEventOutcomeCode{
42+
Value: dstu2ct.AuditEventOutcomeCode_INVALID_UNINITIALIZED,
43+
},
44+
OutcomeDesc: &dstu2dt.String{Value: "Success"},
45+
}),
46+
dstu2.AddParticipant(&dstu2pb.AuditEvent_Participant{
47+
UserId: &dstu2dt.Identifier{
48+
Value: &dstu2dt.String{Value: "smokeuser@philips.com"},
49+
},
50+
Requestor: &dstu2dt.Boolean{Value: true},
51+
}))
1252

1353
if !assert.Nil(t, err) {
1454
return

0 commit comments

Comments
 (0)