summary refs log tree commit diff stats
path: root/apod/json.go
blob: eae5fb2130efab7999052fb2cbfa300ad929d89d (plain) (blame)
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
package apod

import (
	"encoding/json"
	"fmt"
	"regexp"

	"tildegit.org/andinus/cetus/request"
)

// APOD holds the response from the api. Not every field is returned
// in every request. Code & Msg should be filled only if the api
// returns an error, this behaviour was observed and shouldn't be
// trusted.
type APOD struct {
	Copyright      string `json:"copyright"`
	Date           string `json:"date"`
	Explanation    string `json:"explanation"`
	HDURL          string `json:"hdurl"`
	MediaType      string `json:"media_type"`
	ServiceVersion string `json:"service_version"`
	Title          string `json:"title"`
	URL            string `json:"url"`

	Code int    `json:"code"`
	Msg  string `json:"msg"`
}

// UnmarshalJson will take body as input & unmarshal it to res.
func UnmarshalJson(res *APOD, body string) error {
	err := json.Unmarshal([]byte(body), res)
	if err != nil {
		err = fmt.Errorf("json.go: unmarshalling json failed\n%s",
			err.Error())
	}
	return err
}

// GetJson takes reqInfo as input and returns the body and an error.
func GetJson(reqInfo map[string]string) (string, error) {
	var body string
	var err error

	// This regexp is not perfect and does not guarantee that the
	// request will not fail because of wrong date, this will
	// eliminate many wrong dates though.
	re := regexp.MustCompile("((19|20)\\d\\d)-(0?[1-9]|1[012])-(0?[1-9]|[12][0-9]|3[01])")
	if !re.MatchString(reqInfo["date"]) {
		err = fmt.Errorf("json.go: %s does not match format 'YYYY-MM-DD'",
			reqInfo["date"])
		return body, err
	}

	// reqInfo is map[string]string and params is built from it, currently
	// it takes apiKey and the date from reqInfo to build param. If any
	// new key/value is added to reqInfo then it must be addded here too,
	// it won't be sent as param directly.
	params := make(map[string]string)
	params["api_key"] = reqInfo["apiKey"]
	params["date"] = reqInfo["date"]

	body, err = request.GetRes(reqInfo["api"], params)
	return body, err
}