-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathurl.go
98 lines (91 loc) · 2.4 KB
/
url.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
// Copyright (c) 2017 Hervé Gouchet. All rights reserved.
// Use of this source code is governed by the MIT License
// that can be found in the LICENSE file.
package opencorporates
import (
"errors"
"fmt"
"net/url"
"strconv"
"strings"
)
const (
// Version is the API default version
Version = "0.4"
// URL is the base URL of the OpenCorporates service.
URL = "https://api.opencorporates.com/v%s/"
// ByNameURL is the path to search a company by name or jurisdiction.
ByNameURL = "companies/search"
// ByNumberURL is the path to search a company by identifier.
ByNumberURL = "companies/%s/%s?sparse=true"
)
// Error messages.
var (
// ErrMethod is the error for unknown method call.
ErrMethod = errors.New("unknown method call")
// ErrJurisdiction is the error returned if the jurisdiction code is missing.
ErrJurisdiction = errors.New("missing jurisdiction code")
)
func companyNamedURL(version string, param ...interface{}) (string, error) {
var (
q, jurisdiction string
page int
)
switch len(param) {
case 3:
page = param[2].(int)
fallthrough
case 2:
jurisdiction = url.QueryEscape(param[1].(string))
fallthrough
case 1:
q = url.QueryEscape(param[0].(string))
}
query := URL + ByNameURL + "?page=%d&order=score&q=%s"
if jurisdiction != "" {
return fmt.Sprintf(query+"&jurisdiction_code=%s", version, page, q, jurisdiction), nil
}
return fmt.Sprintf(query, version, page, q), nil
}
func companyNumberURL(version string, param ...interface{}) (string, error) {
var id, jurisdiction string
if len(param) == 2 {
id = url.QueryEscape(param[0].(string))
jurisdiction = url.QueryEscape(param[1].(string))
}
if _, err := strconv.Atoi(id); err != nil {
return "", err
}
if jurisdiction == "" {
return "", ErrJurisdiction
}
return fmt.Sprintf(URL+ByNumberURL, version, jurisdiction, id), nil
}
func (api *client) url(method string, param ...interface{}) (s string, err error) {
if api.Version == "" {
// Default value.
api.Version = Version
}
// By method calls
switch method {
case ByNumberURL:
s, err = companyNumberURL(api.Version, param...)
case ByNameURL:
s, err = companyNamedURL(api.Version, param...)
default:
err = ErrMethod
}
if err != nil {
return
}
if api.Token != "" {
// Adds the API key to increase the usage limits
if strings.Contains(s, "?") {
s += "&"
} else {
s += "?"
}
s += "&api_token=" + url.QueryEscape(api.Token)
}
return
}