File: diff.go

package info (click to toggle)
golang-github-sethvargo-go-fastly 1.2.1%2Bgit20190805.5c6c8bd-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 2,584 kB
  • sloc: makefile: 71
file content (57 lines) | stat: -rw-r--r-- 1,397 bytes parent folder | download | duplicates (2)
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
package fastly

import "fmt"

// Diff represents a diff of two versions as a response from the Fastly API.
type Diff struct {
	Format string `mapstructure:"format"`
	From   int    `mapstructure:"from"`
	To     int    `mapstructure:"to"`
	Diff   string `mapstructure:"diff"`
}

// GetDiffInput is used as input to the GetDiff function.
type GetDiffInput struct {
	// Service is the ID of the service (required).
	Service string

	// From is the version to diff from. This can either be a string indicating a
	// positive number (e.g. "1") or a negative number from "-1" down ("-1" is the
	// latest version).
	From int

	// To is the version to diff up to. The same rules for From apply.
	To int

	// Format is an optional field to specify the format with which the diff will
	// be returned. Acceptable values are "text" (default), "html", or
	// "html_simple".
	Format string
}

// GetDiff returns the diff of the given versions.
func (c *Client) GetDiff(i *GetDiffInput) (*Diff, error) {
	if i.Service == "" {
		return nil, ErrMissingService
	}

	if i.From == 0 {
		return nil, ErrMissingFrom
	}

	if i.To == 0 {
		return nil, ErrMissingTo
	}

	path := fmt.Sprintf("service/%s/diff/from/%d/to/%d", i.Service, i.From, i.To)
	resp, err := c.Get(path, nil)
	if err != nil {
		return nil, err
	}

	var d *Diff
	if err := decodeJSON(&d, resp.Body); err != nil {
		return nil, err
	}
	return d, nil
}