Skip to content

Commit

Permalink
Add Post helper
Browse files Browse the repository at this point in the history
  • Loading branch information
alaingilbert committed Aug 26, 2018
1 parent ed5ec79 commit c591525
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions ogame.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package ogame

import (
"compress/gzip"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"math"
Expand Down Expand Up @@ -30,6 +32,7 @@ type Wrapper interface {
SetUserAgent(newUserAgent string)
ServerURL() string
GetPageContent(url.Values) string
PostPageContent(url.Values, url.Values) string
Login() error
Logout()
GetUsername() string
Expand Down Expand Up @@ -697,6 +700,42 @@ func isPartialPage(vals url.Values) bool {
return false
}

func (b *OGame) postPageContent(vals, payload url.Values) string {
finalURL := b.serverURL + "/game/index.php?" + vals.Encode()
req, err := http.NewRequest("POST", finalURL, strings.NewReader(payload.Encode()))
if err != nil {
b.error(err)
return ""
}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.Header.Add("X-Requested-With", "XMLHttpRequest")
req.Header.Add("Accept-Encoding", "gzip, deflate, br")

resp, err := b.client.Do(req)
if err != nil {
b.error(err)
return ""
}
defer resp.Body.Close()

var reader io.ReadCloser
switch resp.Header.Get("Content-Encoding") {
case "gzip":
reader, err = gzip.NewReader(resp.Body)
defer reader.Close()
default:
reader = resp.Body
}

body, err := ioutil.ReadAll(reader)
if err != nil {
b.error(err)
return ""
}

return string(body)
}

func (b *OGame) getPageContent(vals url.Values) string {
if b.serverURL == "" {
logrus.Error("serverURL is empty")
Expand Down Expand Up @@ -2537,6 +2576,13 @@ func (b *OGame) GetPageContent(vals url.Values) string {
return b.getPageContent(vals)
}

// PostPageContent ...
func (b *OGame) PostPageContent(vals, payload url.Values) string {
b.Lock()
defer b.Unlock()
return b.postPageContent(vals, payload)
}

// IsUnderAttack returns true if the user is under attack, false otherwise
func (b *OGame) IsUnderAttack() bool {
b.Lock()
Expand Down

0 comments on commit c591525

Please sign in to comment.