Skip to content
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
4 changes: 2 additions & 2 deletions stackongo/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type authError struct {

var auth_url string = "https://stackexchange.com/oauth/access_token"

// AuthURL returns the URL to redirect the user for authentication
// AuthURL returns the URL to redirect the user for authentication
// It accepts the following arguments
// client_id - Your App's registered ID
// redirect_uri - URI to redirect after authentication
Expand Down Expand Up @@ -58,7 +58,7 @@ func ObtainAccessToken(client_id, client_secret, code, redirect_uri string) (out

error = errors.New(collection.Error["type"] + ": " + collection.Error["message"])
} else {
// if not process the output
// if not process the output
bytes, err2 := ioutil.ReadAll(response.Body)

if err2 != nil {
Expand Down
10 changes: 8 additions & 2 deletions stackongo/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@ package stackongo

import (
"net/url"
"reflect"
"testing"
)

func TestAuthURL(t *testing.T) {
result_uri, _ := url.Parse(AuthURL("abc", "www.my_app.com", map[string]string{"state": "test", "scope": "read_inbox,no_expiry"}))

if result_uri.Query().Encode() != "state=test&scope=read_inbox%2Cno_expiry&redirect_uri=www.my_app.com&client_id=abc" {
print(result_uri.Query().Encode())
if !reflect.DeepEqual(result_uri.Query(), url.Values{
"state": []string{"test"},
"scope": []string{"read_inbox,no_expiry"},
"redirect_uri": []string{"www.my_app.com"},
"client_id": []string{"abc"},
}) {
t.Log(result_uri.Query().Encode())
t.Error("URL doesn't match")
}
}
Expand Down
38 changes: 15 additions & 23 deletions stackongo/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,30 +57,26 @@ func setupEndpoint(path string, params map[string]string) *url.URL {
}

// parse the response
func parseResponse(response *http.Response, result interface{}) (error error) {
func parseResponse(response *http.Response, result interface{}) error {
// close the body when done reading
defer response.Body.Close()

//read the response
bytes, error := ioutil.ReadAll(response.Body)

if error != nil {
return
bytes, err := ioutil.ReadAll(response.Body)
if err != nil {
return err
}

//parse JSON
error = json.Unmarshal(bytes, result)

if error != nil {
print(error.Error())
err = json.Unmarshal(bytes, result)
if err != nil {
return err
}

//check whether the response is a bad request
if response.StatusCode == 400 {
error = errors.New(fmt.Sprintf("Bad Request: %s", string(bytes)))
if response.StatusCode == http.StatusBadRequest {
return errors.New(fmt.Sprintf("Bad Request: %s", string(bytes)))
}

return
return nil
}

func (session Session) get(section string, params map[string]string, collection interface{}) (error error) {
Expand All @@ -90,17 +86,13 @@ func (session Session) get(section string, params map[string]string, collection
return get(section, params, collection)
}

func get(section string, params map[string]string, collection interface{}) (error error) {
func get(section string, params map[string]string, collection interface{}) error {
client := &http.Client{Transport: getTransport()}

response, error := client.Get(setupEndpoint(section, params).String())

if error != nil {
return
response, err := client.Get(setupEndpoint(section, params).String())
if err != nil {
return err
}

error = parseResponse(response, collection)

return

return parseResponse(response, collection)
}