/
save_contacts.go
102 lines (85 loc) · 3.32 KB
/
save_contacts.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package contacts
import (
"context"
"github.com/uber/zanzibar/examples/example-gateway/build/endpoints/contacts/module"
"github.com/uber/zanzibar/examples/example-gateway/build/endpoints/contacts/workflow"
contactsClientStructs "github.com/uber/zanzibar/examples/example-gateway/build/gen-code/clients/contacts/contacts"
endpointContacts "github.com/uber/zanzibar/examples/example-gateway/build/gen-code/endpoints/contacts/contacts"
zanzibar "github.com/uber/zanzibar/runtime"
"go.uber.org/zap"
)
// NewContactsSaveContactsWorkflow ...
func NewContactsSaveContactsWorkflow(
deps *module.Dependencies,
) workflow.ContactsSaveContactsWorkflow {
return &SaveContactsEndpoint{
Clients: deps.Client,
Logger: deps.Default.Logger,
}
}
// SaveContactsEndpoint ...
type SaveContactsEndpoint struct {
Clients *module.ClientDependencies
Logger *zap.Logger
Request *zanzibar.ServerHTTPRequest
}
// Handle "/contacts/:userUUID/contacts"
func (w SaveContactsEndpoint) Handle(
ctx context.Context,
headers zanzibar.Header,
r *endpointContacts.SaveContactsRequest,
) (*endpointContacts.SaveContactsResponse, zanzibar.Header, error) {
// TODO AuthenticatedRequest()
// TODO MatchedIdRequest({paramName: 'userUUID'})
clientBody := convertToClient(r)
cres, _, err := w.Clients.Contacts.SaveContacts(ctx, nil, clientBody)
if err != nil {
w.Logger.Error("Could not make client request", zap.Error(err))
return nil, nil, err
}
// TODO: verify IsOKResponse() on client response status code
response := convertToResponse(cres)
return response, nil, nil
}
func convertToResponse(
body *contactsClientStructs.SaveContactsResponse,
) *endpointContacts.SaveContactsResponse {
return &endpointContacts.SaveContactsResponse{}
}
func convertToClient(
body *endpointContacts.SaveContactsRequest,
) *contactsClientStructs.SaveContactsRequest {
clientBody := &contactsClientStructs.SaveContactsRequest{}
clientBody.UserUUID = body.UserUUID
for _, contact := range body.Contacts {
clientContact := &contactsClientStructs.Contact{}
clientAttributes := &contactsClientStructs.ContactAttributes{}
attributes := contact.Attributes
clientAttributes.FirstName = attributes.FirstName
clientAttributes.LastName = attributes.LastName
clientAttributes.Nickname = attributes.Nickname
clientAttributes.HasPhoto = attributes.HasPhoto
clientAttributes.NumFields = attributes.NumFields
clientAttributes.TimesContacted = attributes.TimesContacted
clientAttributes.LastTimeContacted = attributes.LastTimeContacted
clientAttributes.IsStarred = attributes.IsStarred
clientAttributes.HasCustomRingtone = attributes.HasCustomRingtone
clientAttributes.IsSendToVoicemail = attributes.IsSendToVoicemail
clientAttributes.HasThumbnail = attributes.HasThumbnail
clientAttributes.NamePrefix = attributes.NamePrefix
clientAttributes.NameSuffix = attributes.NameSuffix
for _, fragment := range contact.Fragments {
clientFragment := &contactsClientStructs.ContactFragment{}
clientFragment.Text = fragment.Text
clientFragmentType := contactsClientStructs.
ContactFragmentType(*fragment.Type)
clientFragment.Type = &clientFragmentType
clientContact.Fragments = append(
clientContact.Fragments, clientFragment,
)
}
clientContact.Attributes = clientAttributes
clientBody.Contacts = append(clientBody.Contacts, clientContact)
}
return clientBody
}