From 5e62f946a75c5be13a8b2b65bcb66096f8f72634 Mon Sep 17 00:00:00 2001 From: Ludovico Russo Date: Thu, 28 Apr 2022 16:09:30 +0200 Subject: [PATCH] feat: Deprecate URL func and add NewURL and MustURL --- client.go | 6 +++--- feed.go | 2 +- filescan.go | 4 ++-- monitor.go | 4 ++-- urlscan.go | 2 +- vt.go | 30 +++++++++++++++++++++++++----- vt_test.go | 16 ++++++++-------- 7 files changed, 42 insertions(+), 22 deletions(-) diff --git a/client.go b/client.go index b571cf8..a513b54 100644 --- a/client.go +++ b/client.go @@ -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 @@ -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() @@ -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 diff --git a/feed.go b/feed.go index e2df5cd..c1e059e 100644 --- a/feed.go +++ b/feed.go @@ -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 { diff --git a/filescan.go b/filescan.go index 06be176..15cf693 100644 --- a/filescan.go +++ b/filescan.go @@ -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{ diff --git a/monitor.go b/monitor.go index dae8518..3060501 100644 --- a/monitor.go +++ b/monitor.go @@ -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{ diff --git a/urlscan.go b/urlscan.go index 0e0930b..f8bd741 100644 --- a/urlscan.go +++ b/urlscan.go @@ -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 } diff --git a/vt.go b/vt.go index 5ec69a7..1ec04ce 100644 --- a/vt.go +++ b/vt.go @@ -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 diff --git a/vt_test.go b/vt_test.go index 52416e8..d614392 100644 --- a/vt_test.go +++ b/vt_test.go @@ -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 @@ -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()) @@ -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()) @@ -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()) @@ -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()) @@ -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())