This repository has been archived by the owner on Feb 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
need.go
69 lines (58 loc) · 2 KB
/
need.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
package need_api
import (
"encoding/json"
"github.com/alphagov/metadata-api/request"
)
type Organisation struct {
ID string `json:"id"`
Name string `json:"name"`
Status string `json:"govuk_status"`
Abbreviation string `json:"abbreviation"`
ParentIDs []string `json:"parent_ids"`
ChildIDs []string `json:"child_ids"`
}
type NeedStatus struct {
Description string `json:"description"`
}
type Need struct {
ID int `json:"id"`
Role string `json:"role"`
Goal string `json:"goal"`
Benefit string `json:"benefit"`
OrganisationIDs []string `json:"organisation_ids"`
Organisations []Organisation `json:"organisations"`
Justifications []string `json:"justifications"`
Impact string `json:"impact"`
MetWhen []string `json:"met_when"`
YearlyUserContacts int `json:"yearly_user_contacts"`
YearlySiteViews int `json:"yearly_site_views"`
YearlyNeedViews int `json:"yearly_need_views"`
YearlySearches int `json:"yearly_searches"`
OtherEvidence string `json:"other_evidence"`
Legislation string `json:"legislation"`
AllOrganisations bool `json:"applies_to_all_organisations"`
DuplicateOf int `json:"duplicate_of"`
Status *NeedStatus `json:"status"`
}
func ParseNeedResponse(response []byte) (*Need, error) {
need := &Need{}
if err := json.Unmarshal(response, &need); err != nil {
return nil, err
}
return need, nil
}
func FetchNeed(needAPI, bearerToken, id string) (*Need, error) {
needResponse, err := request.NewRequest(needAPI+"/needs/"+id, bearerToken)
if err != nil {
return nil, err
}
needBody, err := request.ReadResponseBody(needResponse)
if err != nil {
return nil, err
}
need, err := ParseNeedResponse([]byte(needBody))
if err != nil {
return nil, err
}
return need, nil
}