Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deprecate panicking functions #25

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ func (cli *Client) PatchObject(url *url.URL, obj *Object, options ...RequestOpti
// DownloadFile downloads a file given its hash (SHA-256, SHA-1 or MD5). The
// file is written into the provided io.Writer.
func (cli *Client) DownloadFile(hash string, w io.Writer) (int64, error) {
u := URL("files/%s/download", hash)
u := MustURL("files/%s/download", hash)
resp, err := cli.sendRequest("GET", u, nil, nil)
if err != nil {
return 0, err
Expand Down Expand Up @@ -365,7 +365,7 @@ func (cli *Client) Iterator(url *url.URL, options ...IteratorOption) (*Iterator,
// it, err := client.Search("p:10+ size:30MB+")
//
func (cli *Client) Search(query string, options ...IteratorOption) (*Iterator, error) {
u := URL("intelligence/search")
u := MustURL("intelligence/search")
q := u.Query()
q.Add("query", query)
u.RawQuery = q.Encode()
Expand Down Expand Up @@ -399,7 +399,7 @@ type RelationshipMeta struct {
// endpoint.
func (cli *Client) GetMetadata() (*Metadata, error) {
metadata := &Metadata{}
if _, err := cli.GetData(URL("metadata"), metadata); err != nil {
if _, err := cli.GetData(MustURL("metadata"), metadata); err != nil {
return nil, err
}
return metadata, nil
Expand Down
2 changes: 1 addition & 1 deletion feed.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ var errNotFound = errors.New("not found")

func (f *Feed) getObjects(packageTime string) ([]*Object, error) {

u := URL("feeds/%s/%s", f.feedType, packageTime)
u := MustURL("feeds/%s/%s", f.feedType, packageTime)

httpResp, err := f.client.sendRequest("GET", u, nil, nil)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions filescan.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,14 @@ func (s *FileScanner) Scan(r io.Reader, filename string, progress chan<- float32
// Payload is bigger than supported by AppEngine in a POST request,
// let's ask for an upload URL.
var u string
if _, err := s.cli.GetData(URL("files/upload_url"), &u); err != nil {
if _, err := s.cli.GetData(MustURL("files/upload_url"), &u); err != nil {
return nil, err
}
if uploadURL, err = url.Parse(u); err != nil {
return nil, err
}
} else {
uploadURL = URL("files")
uploadURL = MustURL("files")
}

pr := &progressReader{
Expand Down
4 changes: 2 additions & 2 deletions monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,14 @@ func (s *MonitorUploader) upload(r io.Reader, params map[string]string, progress
// Payload is bigger than supported by AppEngine in a POST request,
// let's ask for an upload URL.
var u string
if _, err := s.cli.GetData(URL("monitor/items/upload_url"), &u); err != nil {
if _, err := s.cli.GetData(MustURL("monitor/items/upload_url"), &u); err != nil {
return nil, err
}
if uploadURL, err = url.Parse(u); err != nil {
return nil, err
}
} else {
uploadURL = URL("monitor/items")
uploadURL = MustURL("monitor/items")
}

pr := &progressReader{
Expand Down
2 changes: 1 addition & 1 deletion urlscan.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (s *URLScanner) Scan(url string) (*Object, error) {

headers := map[string]string{"Content-Type": w.FormDataContentType()}

httpResp, err := s.cli.sendRequest("POST", URL("urls"), &b, headers)
httpResp, err := s.cli.sendRequest("POST", MustURL("urls"), &b, headers)
if err != nil {
return nil, err
}
Expand Down
30 changes: 25 additions & 5 deletions vt.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,40 @@ func (e Error) Error() string {
return e.Message
}

// URL returns a full VirusTotal API URL from a relative path (i.e: a path
// NewURL returns a full VirusTotal API URL from a relative path (i.e: a path
// without the domain name and the "/api/v3/" prefix). The path can contain
// format 'verbs' as defined in the "fmt". This function is useful for creating
// URLs to be passed to any function expecting a *url.URL in this library.
func URL(pathFmt string, a ...interface{}) *url.URL {
func NewURL(pathFmt string, a ...interface{}) (*url.URL, error) {
path := fmt.Sprintf(pathFmt, a...)
url, err := url.Parse(path)
if err != nil {
msg := fmt.Sprintf(
return nil, fmt.Errorf(
"error formatting URL \"%s\": %s",
pathFmt, err)
panic(msg)
}
return baseURL.ResolveReference(url)
return baseURL.ResolveReference(url), nil
}

// MustURL returns a full VirusTotal API URL from a relative path (i.e: a path
// without the domain name and the "/api/v3/" prefix). The path can contain
// format 'verbs' as defined in the "fmt". This function is useful for creating
// URLs to be passed to any function expecting a *url.URL in this library.
func MustURL(pathFmt string, a ...interface{}) *url.URL {
url, err := NewURL(pathFmt, a...)
if err != nil {
panic(err.Error())
}
return url, nil
}

// URL returns a full VirusTotal API URL from a relative path (i.e: a path
// without the domain name and the "/api/v3/" prefix). The path can contain
// format 'verbs' as defined in the "fmt". This function is useful for creating
// URLs to be passed to any function expecting a *url.URL in this library.
// Deprecated: Use NewURL or MustURL instead.
func URL(pathFmt string, a ...interface{}) *url.URL {
return MustURL(pathFmt, a...)
}

// SetHost allows to change the host used while sending requests to the
Expand Down
16 changes: 8 additions & 8 deletions vt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import (

func ExampleURL() {
SetHost("https://www.virustotal.com")
url := URL("files/275a021bbfb6489e54d471899f7db9d1663fc695ec2fe2a2c4538aabf651fd0f")
url := MustURL("files/275a021bbfb6489e54d471899f7db9d1663fc695ec2fe2a2c4538aabf651fd0f")
fmt.Println(url)
url = URL("intelligence/retrohunt_jobs/%s", "1234567")
url = MustURL("intelligence/retrohunt_jobs/%s", "1234567")
fmt.Println(url)
// Output:
// https://www.virustotal.com/api/v3/files/275a021bbfb6489e54d471899f7db9d1663fc695ec2fe2a2c4538aabf651fd0f
Expand Down Expand Up @@ -123,7 +123,7 @@ func TestGetObject(t *testing.T) {

SetHost(ts.URL)
c := NewClient("api_key")
o, err := c.GetObject(URL("/collection/object_id"))
o, err := c.GetObject(MustURL("/collection/object_id"))

assert.NoError(t, err)
assert.Equal(t, "object_id", o.ID())
Expand Down Expand Up @@ -233,7 +233,7 @@ func TestPostObject(t *testing.T) {
SetHost(ts.URL)
c := NewClient("api_key")
o := NewObject("object_type")
err := c.PostObject(URL("/collection"), o)
err := c.PostObject(MustURL("/collection"), o)

assert.NoError(t, err)
assert.Equal(t, "object_id", o.ID())
Expand Down Expand Up @@ -274,12 +274,12 @@ func TestPatchObject(t *testing.T) {
c := NewClient("api_key")

SetHost(getServer.URL)
o, err := c.GetObject(URL("/collection/object_id"))
o, err := c.GetObject(MustURL("/collection/object_id"))
assert.NoError(t, err)

SetHost(patchServer.URL)
o.SetString("some_string", "world")
err = c.PatchObject(URL("/collection/object_id"), o)
err = c.PatchObject(MustURL("/collection/object_id"), o)

assert.NoError(t, err)
assert.Equal(t, "object_id", o.ID())
Expand Down Expand Up @@ -319,7 +319,7 @@ func TestIterator(t *testing.T) {

SetHost(ts.URL)
c := NewClient("api_key")
it, err := c.Iterator(URL("/collection"))
it, err := c.Iterator(MustURL("/collection"))

assert.NoError(t, err)
assert.NoError(t, it.Error())
Expand Down Expand Up @@ -354,7 +354,7 @@ func TestIteratorSingleObject(t *testing.T) {

SetHost(ts.URL)
c := NewClient("api_key")
it, err := c.Iterator(URL("/collection"))
it, err := c.Iterator(MustURL("/collection"))

assert.NoError(t, err)
assert.NoError(t, it.Error())
Expand Down