コード例 #1
0
ファイル: client.go プロジェクト: Slacki/go-steam
// Accept received trade offer
// It is best to confirm that offer was actually accepted
// by calling GetOffer after Accept and checking offer state
func (c *Client) Accept(offerId uint64) error {
	baseurl := fmt.Sprintf("https://steamcommunity.com/tradeoffer/%d/", offerId)
	req := netutil.NewPostForm(baseurl+"accept", netutil.ToUrlValues(map[string]string{
		"sessionid":    c.sessionId,
		"serverid":     "1",
		"tradeofferid": strconv.FormatUint(offerId, 10),
	}))
	req.Header.Add("Referer", baseurl)

	resp, err := c.client.Do(req)
	if err != nil {
		return err
	}
	defer resp.Body.Close()
	t := new(struct {
		StrError string `json:"strError"`
	})
	if err = json.NewDecoder(resp.Body).Decode(t); err != nil {
		return err
	}
	if t.StrError != "" {
		return newSteamErrorf("accept error: %v\n", t.StrError)
	}
	if resp.StatusCode != 200 {
		return fmt.Errorf("accept error: status code %d", resp.StatusCode)
	}
	return nil
}
コード例 #2
0
ファイル: client.go プロジェクト: Slacki/go-steam
func (c *Client) getPartialPartnerInventory(other steamid.SteamId, contextId uint64, appId uint32, offerId *uint64, start *uint) (*inventory.PartialInventory, error) {
	data := map[string]string{
		"sessionid": c.sessionId,
		"partner":   other.ToString(),
		"contextid": strconv.FormatUint(contextId, 10),
		"appid":     strconv.FormatUint(uint64(appId), 10),
	}
	if start != nil {
		data["start"] = strconv.FormatUint(uint64(*start), 10)
	}

	baseUrl := "https://steamcommunity.com/tradeoffer/%v/"
	if offerId != nil {
		baseUrl = fmt.Sprintf(baseUrl, *offerId)
	} else {
		baseUrl = fmt.Sprintf(baseUrl, "new")
	}

	req, err := http.NewRequest("GET", baseUrl+"partnerinventory/?"+netutil.ToUrlValues(data).Encode(), nil)
	if err != nil {
		panic(err)
	}
	req.Header.Add("Referer", baseUrl+"?partner="+strconv.FormatUint(uint64(other.GetAccountId()), 10))

	return inventory.DoInventoryRequest(c.client, req)
}
コード例 #3
0
ファイル: client.go プロジェクト: Slacki/go-steam
// Get duration of escrow in days. Call this before sending a trade offer
func (c *Client) GetPartnerEscrowDuration(other steamid.SteamId, accessToken *string) (*EscrowDuration, error) {
	data := map[string]string{
		"partner": strconv.FormatUint(uint64(other.GetAccountId()), 10),
	}
	if accessToken != nil {
		data["token"] = *accessToken
	}
	return c.getEscrowDuration("https://steamcommunity.com/tradeoffer/new/?" + netutil.ToUrlValues(data).Encode())
}
コード例 #4
0
ファイル: client.go プロジェクト: 24hours/go-steam
func (c *Client) action(method string, version uint, id TradeOfferId) error {
	resp, err := c.client.Do(netutil.NewPostForm(fmt.Sprintf(apiUrl, method, version), netutil.ToUrlValues(map[string]string{
		"key":          string(c.key),
		"tradeofferid": strconv.FormatUint(uint64(id), 10),
	})))
	if err != nil {
		return err
	}
	defer resp.Body.Close()
	if resp.StatusCode != 200 {
		return errors.New(method + " error: status code not 200")
	}
	return nil
}
コード例 #5
0
ファイル: client.go プロジェクト: 24hours/go-steam
func (c *Client) GetOffers() (*TradeOffers, error) {
	resp, err := c.client.Get(fmt.Sprintf(apiUrl, "GetTradeOffers", 1) + "?" + netutil.ToUrlValues(map[string]string{
		"key":                 string(c.key),
		"get_sent_offers":     "1",
		"get_received_offers": "1",
	}).Encode())
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()
	t := new(struct {
		Response *TradeOffers
	})
	err = json.NewDecoder(resp.Body).Decode(t)
	if err != nil {
		return nil, err
	}
	return t.Response, nil
}
コード例 #6
0
ファイル: client.go プロジェクト: Slacki/go-steam
func (c *Client) GetOffers(getSent bool, getReceived bool, getDescriptions bool, activeOnly bool, historicalOnly bool, timeHistoricalCutoff *uint32) (*TradeOffersResult, error) {
	if !getSent && !getReceived {
		return nil, fmt.Errorf("getSent and getReceived can't be both false\n")
	}

	params := map[string]string{
		"key": string(c.key),
	}
	if getSent {
		params["get_sent_offers"] = "1"
	}
	if getReceived {
		params["get_received_offers"] = "1"
	}
	if getDescriptions {
		params["get_descriptions"] = "1"
		params["language"] = "en_us"
	}
	if activeOnly {
		params["active_only"] = "1"
	}
	if historicalOnly {
		params["historical_only"] = "1"
	}
	if timeHistoricalCutoff != nil {
		params["time_historical_cutoff"] = strconv.FormatUint(uint64(*timeHistoricalCutoff), 10)
	}
	resp, err := c.client.Get(fmt.Sprintf(apiUrl, "GetTradeOffers", 1) + "?" + netutil.ToUrlValues(params).Encode())
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()
	t := new(struct {
		Response *TradeOffersResult
	})
	if err = json.NewDecoder(resp.Body).Decode(t); err != nil {
		return nil, err
	}
	if t.Response == nil {
		return nil, newSteamErrorf("steam returned empty offers result\n")
	}
	return t.Response, nil
}
コード例 #7
0
ファイル: client.go プロジェクト: Rayz0r/go-steam
func (c *Client) Accept(id TradeOfferId) error {
	baseurl := fmt.Sprintf("https://steamcommunity.com/tradeoffer/%d/", id)
	req := netutil.NewPostForm(baseurl+"accept", netutil.ToUrlValues(map[string]string{
		"sessionid":    c.sessionId,
		"serverid":     "1",
		"tradeofferid": strconv.FormatUint(uint64(id), 10),
	}))
	req.Header.Add("Referer", baseurl)

	resp, err := c.client.Do(req)
	if err != nil {
		return err
	}
	defer resp.Body.Close()
	if resp.StatusCode != 200 {
		return fmt.Errorf("accept error: status code %d", resp.StatusCode)
	}
	return nil
}
コード例 #8
0
ファイル: trade.go プロジェクト: Slacki/go-steam
// Thread-safe.
func (t *Trade) GetForeignInventory(contextId uint64, appId uint32, start *uint) (*inventory.PartialInventory, error) {
	data := map[string]string{
		"sessionid": t.sessionId,
		"steamid":   fmt.Sprintf("%d", t.other),
		"contextid": strconv.FormatUint(contextId, 10),
		"appid":     strconv.FormatUint(uint64(appId), 10),
	}
	if start != nil {
		data["start"] = strconv.FormatUint(uint64(*start), 10)
	}

	req, err := http.NewRequest("GET", t.baseUrl+"foreigninventory?"+netutil.ToUrlValues(data).Encode(), nil)
	if err != nil {
		panic(err)
	}
	req.Header.Add("Referer", t.baseUrl)

	return inventory.DoInventoryRequest(t.client, req)
}
コード例 #9
0
ファイル: client.go プロジェクト: 24hours/go-steam
func (c *Client) getPartialTheirInventory(other steamid.SteamId, contextId uint64, appId uint32, start *uint) (*inventory.PartialInventory, error) {
	data := map[string]string{
		"sessionid": c.sessionId,
		"partner":   fmt.Sprintf("%d", other),
		"contextid": strconv.FormatUint(contextId, 10),
		"appid":     strconv.FormatUint(uint64(appId), 10),
	}
	if start != nil {
		data["start"] = strconv.FormatUint(uint64(*start), 10)
	}

	const baseUrl = "http://steamcommunity.com/tradeoffer/new/"
	req, err := http.NewRequest("GET", baseUrl+"partnerinventory/?"+netutil.ToUrlValues(data).Encode(), nil)
	if err != nil {
		panic(err)
	}
	req.Header.Add("Referer", baseUrl+"?partner="+fmt.Sprintf("%d", other))

	return inventory.DoInventoryRequest(c.client, req)
}
コード例 #10
0
ファイル: trade.go プロジェクト: Slacki/go-steam
// Ajax POSTs to an API endpoint that should return a status
func (t *Trade) postWithStatus(url string, data map[string]string) (*Status, error) {
	status := new(Status)

	req := netutil.NewPostForm(url, netutil.ToUrlValues(data))
	// Tales of Madness and Pain, Episode 1: If you forget this, Steam will return an error
	// saying "missing required parameter", even though they are all there. IT WAS JUST THE HEADER, ARGH!
	req.Header.Add("Referer", t.baseUrl)

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

	err = json.NewDecoder(resp.Body).Decode(status)
	if err != nil {
		return nil, err
	}
	return status, nil
}
コード例 #11
0
ファイル: client.go プロジェクト: Slacki/go-steam
func (c *Client) GetOffer(offerId uint64) (*TradeOfferResult, error) {
	resp, err := c.client.Get(fmt.Sprintf(apiUrl, "GetTradeOffer", 1) + "?" + netutil.ToUrlValues(map[string]string{
		"key":          string(c.key),
		"tradeofferid": strconv.FormatUint(offerId, 10),
		"language":     "en_us",
	}).Encode())
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()
	t := new(struct {
		Response *TradeOfferResult
	})
	if err = json.NewDecoder(resp.Body).Decode(t); err != nil {
		return nil, err
	}
	if t.Response == nil || t.Response.Offer == nil {
		return nil, newSteamErrorf("steam returned empty offer result\n")
	}

	return t.Response, nil
}
コード例 #12
0
ファイル: client.go プロジェクト: Slacki/go-steam
// Sends a new trade offer to the given Steam user. You can optionally specify an access token if you've got one.
// In addition, `counteredOfferId` can be non-nil, indicating the trade offer this is a counter for.
// On success returns trade offer id
func (c *Client) Create(other steamid.SteamId, accessToken *string, myItems, theirItems []TradeItem, counteredOfferId *uint64, message string) (uint64, error) {
	// Create new trade offer status
	to := map[string]interface{}{
		"newversion": true,
		"version":    3,
		"me": map[string]interface{}{
			"assets":   myItems,
			"currency": make([]struct{}, 0),
			"ready":    false,
		},
		"them": map[string]interface{}{
			"assets":   theirItems,
			"currency": make([]struct{}, 0),
			"ready":    false,
		},
	}

	jto, err := json.Marshal(to)
	if err != nil {
		panic(err)
	}

	// Create url parameters for request
	data := map[string]string{
		"sessionid":         c.sessionId,
		"serverid":          "1",
		"partner":           other.ToString(),
		"tradeoffermessage": message,
		"json_tradeoffer":   string(jto),
	}

	var referer string
	if counteredOfferId != nil {
		referer = fmt.Sprintf("https://steamcommunity.com/tradeoffer/%d/", *counteredOfferId)
		data["tradeofferid_countered"] = strconv.FormatUint(*counteredOfferId, 10)
	} else {
		// Add token for non-friend offers
		if accessToken != nil {
			params := map[string]string{
				"trade_offer_access_token": *accessToken,
			}
			paramsJson, err := json.Marshal(params)
			if err != nil {
				panic(err)
			}

			data["trade_offer_create_params"] = string(paramsJson)

			referer = "https://steamcommunity.com/tradeoffer/new/?partner=" + strconv.FormatUint(uint64(other.GetAccountId()), 10) + "&token=" + *accessToken
		} else {

			referer = "https://steamcommunity.com/tradeoffer/new/?partner=" + strconv.FormatUint(uint64(other.GetAccountId()), 10)
		}
	}

	// Create request
	req := netutil.NewPostForm("https://steamcommunity.com/tradeoffer/new/send", netutil.ToUrlValues(data))
	req.Header.Add("Referer", referer)

	// Send request
	resp, err := c.client.Do(req)
	if err != nil {
		return 0, err
	}
	defer resp.Body.Close()
	t := new(struct {
		StrError     string `json:"strError"`
		TradeOfferId uint64 `json:"tradeofferid,string"`
	})
	if err = json.NewDecoder(resp.Body).Decode(t); err != nil {
		return 0, err
	}
	// strError code descriptions:
	// 15	invalide trade access token
	// 16	timeout
	// 20	wrong contextid
	// 25	can't send more offers until some is accepted/cancelled...
	// 26	object is not in our inventory
	// error code names are in internal/steamlang/enums.go EResult_name
	if t.StrError != "" {
		return 0, newSteamErrorf("create error: %v\n", t.StrError)
	}
	if resp.StatusCode != 200 {
		return 0, fmt.Errorf("create error: status code %d", resp.StatusCode)
	}
	if t.TradeOfferId == 0 {
		return 0, newSteamErrorf("create error: steam returned 0 for trade offer id")
	}
	return t.TradeOfferId, nil
}
コード例 #13
0
ファイル: client.go プロジェクト: Rayz0r/go-steam
// Sends a new trade offer to the given Steam user. You can optionally specify an access token if you've got one.
// In addition, `countered` can be non-nil, indicating the trade offer this is a counter for.
func (c *Client) Create(other steamid.SteamId, accessToken *string, myItems, theirItems []TradeItem, countered *TradeOfferId, message string) (*TradeCreateResult, error) {
	to := map[string]interface{}{
		"newversion": true,
		"version":    "3",
		"me": map[string]interface{}{
			"assets":   myItems,
			"currency": make([]struct{}, 0),
			"ready":    false,
		},
		"them": map[string]interface{}{
			"assets":   theirItems,
			"currency": make([]struct{}, 0),
			"ready":    false,
		},
	}

	jto, err := json.Marshal(to)
	if err != nil {
		panic(err)
	}

	params := make(map[string]string)
	if accessToken != nil {
		params["trade_offer_access_token"] = *accessToken
	}

	paramsJson, err := json.Marshal(params)
	if err != nil {
		panic(err)
	}

	data := map[string]string{
		"sessionid":                 c.sessionId,
		"serverid":                  "1",
		"partner":                   fmt.Sprintf("%d", other),
		"tradeoffermessage":         message,
		"json_tradeoffer":           string(jto),
		"captcha":                   "",
		"trade_offer_create_params": string(paramsJson),
	}

	var referer string
	if countered != nil {
		referer = fmt.Sprintf("https://steamcommunity.com/tradeoffer/%d/", *countered)
		data["tradeofferid_countered"] = fmt.Sprintf("%d", *countered)
	} else {
		referer = fmt.Sprintf("https://steamcommunity.com/tradeoffer/new?partner=%d", other)
	}

	req := netutil.NewPostForm("https://steamcommunity.com/tradeoffer/new/send", netutil.ToUrlValues(data))
	req.Header.Add("Referer", referer)

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

	// If we failed, error out
	if resp.StatusCode != 200 {
		return nil, errors.New("create error: status code not 200")
	}

	// Load the JSON result into TradeCreateResult
	result := new(TradeCreateResult)
	decoder := json.NewDecoder(resp.Body)
	decoder.Decode(result)
	return result, nil
}