/
gln_psyreg.go
141 lines (128 loc) · 4.08 KB
/
gln_psyreg.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package business
import (
"context"
"fmt"
"strings"
"time"
"unicode/utf8"
"github.com/cloudtrust/httpclient"
"github.com/cloudtrust/keycloak-bridge/internal/keycloakb"
"github.com/cloudtrust/keycloak-client"
"github.com/pkg/errors"
"gopkg.in/h2non/gentleman.v2/plugin"
"gopkg.in/h2non/gentleman.v2/plugins/body"
"gopkg.in/h2non/gentleman.v2/plugins/headers"
gurl "gopkg.in/h2non/gentleman.v2/plugins/url"
)
type psyRegResponse struct {
Count *int `json:"personenCount"`
MaxResultExceeded *bool `json:"maxResultExceeded"`
Persons []psyRegPerson `json:"personen"`
}
type psyRegPerson struct {
ID *int `json:"personId"`
FirstName *string `json:"vorname"`
LastName *string `json:"name"`
ProfessionID *int `json:"berufsbezeichnungId"`
Languages []int `json:"sprachIds"`
Locations []psyRegLocation `json:"plzOrtCollection"`
CantonID *int `json:"kantonId"`
ApprovalStatusID *int `json:"bewilligungsstatusId"`
TooLongName interface{} `json:"isMeldungNeunzigTageDienstleisterCurrentYear"`
}
type psyRegLocation struct {
ZipCode *string `json:"plz"`
City *string `json:"ort"`
}
const (
psyRegReqParams = `{"advancedSearchActive":false,"kantonId":null,"gln":"##GLN##","currentPage":0,"ort":"Lausanne","name":"","vorname":"","plz":""}`
)
type psyReg struct {
client *httpclient.Client
origin string
formPath string
referer string
logger keycloakb.Logger
}
// NewPsyRegLookup creates a GLN lookup tool using psyreg.admin.ch
func NewPsyRegLookup(baseURL string, httpTimeout time.Duration, logger keycloakb.Logger) (GlnLookupProvider, error) {
var client, err = httpclient.New(baseURL, httpTimeout)
if err != nil {
return nil, err
}
var origin = strings.ReplaceAll(baseURL, "ws.", "www.")
origin = strings.ReplaceAll(origin, ".bag.", ".")
return &psyReg{
client: client,
formPath: "/api/personen/search",
origin: origin,
referer: origin + "/",
logger: logger,
}, nil
}
func (l *psyReg) Lookup(gln string) GlnSearchResult {
var details, err = l.request(gln)
if err != nil {
return GlnSearchResult{Error: err}
}
return l.jsonToDetails(gln, details)
}
func (l *psyReg) request(gln string) (psyRegResponse, error) {
var bodyValue = strings.ReplaceAll(psyRegReqParams, "##GLN##", gln)
var plugins []plugin.Plugin
plugins = append(plugins,
gurl.Path(l.formPath),
headers.Set("Accept", "application/json, text/plain, */*"),
headers.Set("Content-Length", fmt.Sprintf("%d", utf8.RuneCountInString(bodyValue))),
headers.Set("Content-Type", "application/json;charset=UTF-8"),
headers.Set("Origin", l.origin),
headers.Set("Referer", l.referer),
body.String(bodyValue),
)
var response psyRegResponse
var _, err = l.client.Post(&response, plugins...)
if err != nil {
l.logger.Warn(context.Background(), "msg", "Can't get response from psyReg", "err", err.Error(), "gln", gln)
return psyRegResponse{}, errors.Wrap(err, keycloak.MsgErrCannotObtain+"."+keycloak.Response)
}
return response, nil
}
func (l *psyReg) jsonToDetails(gln string, response psyRegResponse) GlnSearchResult {
if *response.Count == 0 {
return GlnSearchResult{Error: ErrGLNNotFound}
}
var result []GlnPerson
for _, person := range response.Persons {
var (
active = true
zipCode *string
city *string
canton *string
profession *string
)
if person.CantonID != nil {
var value = fmt.Sprintf("%d", *person.CantonID)
canton = &value
}
if person.ProfessionID != nil {
var value = fmt.Sprintf("%d", *person.ProfessionID)
profession = &value
}
if len(person.Locations) > 0 {
zipCode = person.Locations[0].ZipCode
city = person.Locations[0].City
}
result = append(result, GlnPerson{
Active: &active,
Number: &gln,
FirstName: person.FirstName,
LastName: person.LastName,
Canton: canton,
ZipCode: zipCode,
City: city,
Country: nil,
Profession: profession,
})
}
return GlnSearchResult{Persons: result, Error: nil}
}