forked from paulmach/osm
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathchangeset.go
63 lines (50 loc) · 2.25 KB
/
changeset.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
package osmapi
import (
"context"
"fmt"
"github.com/onXmaps/osm"
)
// Changeset returns a given changeset from the osm rest api.
// Delegates to the DefaultDatasource and uses its http.Client to make the request.
func Changeset(ctx context.Context, id osm.ChangesetID) (*osm.Changeset, error) {
return DefaultDatasource.Changeset(ctx, id)
}
// Changeset returns a given changeset from the osm rest api.
func (ds *Datasource) Changeset(ctx context.Context, id osm.ChangesetID) (*osm.Changeset, error) {
url := fmt.Sprintf("%s/changeset/%d", ds.baseURL(), id)
return ds.getChangeset(ctx, url)
}
// ChangesetWithDiscussion returns a changeset and its discussion from the osm rest api.
// Delegates to the DefaultDatasource and uses its http.Client to make the request.
func ChangesetWithDiscussion(ctx context.Context, id osm.ChangesetID) (*osm.Changeset, error) {
return DefaultDatasource.ChangesetWithDiscussion(ctx, id)
}
// ChangesetWithDiscussion returns a changeset and its discussion from the osm rest api.
func (ds *Datasource) ChangesetWithDiscussion(ctx context.Context, id osm.ChangesetID) (*osm.Changeset, error) {
url := fmt.Sprintf("%s/changeset/%d?include_discussion=true", ds.baseURL(), id)
return ds.getChangeset(ctx, url)
}
func (ds *Datasource) getChangeset(ctx context.Context, url string) (*osm.Changeset, error) {
css := &osm.OSM{}
if err := ds.getFromAPI(ctx, url, &css); err != nil {
return nil, err
}
if l := len(css.Changesets); l != 1 {
return nil, fmt.Errorf("wrong number of changesets, expected 1, got %v", l)
}
return css.Changesets[0], nil
}
// ChangesetDownload returns the full osmchange for the changeset using the osm rest api.
// Delegates to the DefaultDatasource and uses its http.Client to make the request.
func ChangesetDownload(ctx context.Context, id osm.ChangesetID) (*osm.Change, error) {
return DefaultDatasource.ChangesetDownload(ctx, id)
}
// ChangesetDownload returns the full osmchange for the changeset using the osm rest api.
func (ds *Datasource) ChangesetDownload(ctx context.Context, id osm.ChangesetID) (*osm.Change, error) {
url := fmt.Sprintf("%s/changeset/%d/download", ds.baseURL(), id)
change := &osm.Change{}
if err := ds.getFromAPI(ctx, url, &change); err != nil {
return nil, err
}
return change, nil
}