-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: use a common function to send a GET request
- Loading branch information
Showing
13 changed files
with
45 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,14 @@ | ||
package paperswithcode_go | ||
|
||
import ( | ||
"encoding/json" | ||
"github.com/codingpot/paperswithcode-go/v2/models" | ||
) | ||
|
||
// MethodGet returns a method in a paper. | ||
// See https://paperswithcode-client.readthedocs.io/en/latest/api/client.html#paperswithcode.client.PapersWithCodeClient.method_list | ||
func (c *Client) MethodGet(methodID string) (*models.Method, error) { | ||
url := c.baseURL + "/methods/" + methodID | ||
|
||
response, err := c.httpClient.Get(url) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var result models.Method | ||
|
||
err = json.NewDecoder(response.Body).Decode(&result) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &result, nil | ||
} | ||
err := c.sendGetRequest(url, &result) | ||
return &result, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,4 +40,4 @@ func TestClient_MethodGet(t *testing.T) { | |
assert.Equal(t, tt.want, got) | ||
}) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,14 @@ | ||
package paperswithcode_go | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"github.com/codingpot/paperswithcode-go/v2/models" | ||
"net/url" | ||
) | ||
|
||
// PaperGet returns a single paper. Note that paperID is hyphen cased (e.g., generative-adversarial-networks). | ||
func (c *Client) PaperGet(paperID string) (*models.Paper, error) { | ||
paperGetURL := fmt.Sprintf("%s/papers/%s/", c.baseURL, url.QueryEscape(paperID)) | ||
response, err := c.httpClient.Get(paperGetURL) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
paperGetURL := fmt.Sprintf("%s/papers/%s/", c.baseURL, paperID) | ||
var paperGetResult models.Paper | ||
err = json.NewDecoder(response.Body).Decode(&paperGetResult) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &paperGetResult, nil | ||
err := c.sendGetRequest(paperGetURL, &paperGetResult) | ||
return &paperGetResult, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,15 @@ | ||
package paperswithcode_go | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"github.com/codingpot/paperswithcode-go/v2/models" | ||
"net/http" | ||
"net/url" | ||
) | ||
|
||
// PaperMethodList returns the methods used in the given paper. | ||
func (c *Client) PaperMethodList(paperID string) (*models.MethodList, error) { | ||
pURL := fmt.Sprintf("%s/papers/%s/methods", c.baseURL, url.QueryEscape(paperID)) | ||
response, err := http.Get(pURL) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var methodList models.MethodList | ||
err = json.NewDecoder(response.Body).Decode(&methodList) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
err := c.sendGetRequest(pURL, &methodList) | ||
return &methodList, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package paperswithcode_go | ||
|
||
import "encoding/json" | ||
|
||
func (c *Client) sendGetRequest(url string, result interface{}) error { | ||
response, err := c.httpClient.Get(url) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return json.NewDecoder(response.Body).Decode(result) | ||
} |