Skip to content
This repository has been archived by the owner on Jul 30, 2024. It is now read-only.

TradeOffer.Accept changes #933

Merged
Next Next commit
Added a parameter to SteamWeb.Fetch that allows to return data from e…
…rror responses (when HTTP code is not 200)

TradeOffer.Accept now returns TradeOfferAcceptResponse
  • Loading branch information
MadBender committed Jan 21, 2015
commit 5e6280bd24504cebc667b649d307a14e35198ee9
8 changes: 4 additions & 4 deletions SteamBot/TradeOfferUserHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ public override void OnNewTradeOffer(TradeOffer offer)

//do validation logic etc
if (DummyValidation(myItems, theirItems))
{
string tradeid;
if (offer.Accept(out tradeid))
{
TradeOfferAcceptResponse acceptResp = offer.Accept();
if (acceptResp.Accepted)
{
Log.Success("Accepted trade offer successfully : Trade ID: " + tradeid);
Log.Success("Accepted trade offer successfully : Trade ID: " + acceptResp.TradeId);
}
}
else
Expand Down
28 changes: 23 additions & 5 deletions SteamTrade/SteamWeb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ public class SteamWeb
public string TokenSecure { get; private set; }
private CookieContainer _cookies = new CookieContainer();

public string Fetch(string url, string method, NameValueCollection data = null, bool ajax = true, string referer = "")
public string Fetch(string url, string method, NameValueCollection data = null, bool ajax = true, string referer = "", bool fetchError = false)
{
using (HttpWebResponse response = Request(url, method, data, ajax, referer))
using (HttpWebResponse response = Request(url, method, data, ajax, referer, fetchError))
{
using (Stream responseStream = response.GetResponseStream())
{
Expand All @@ -33,9 +33,11 @@ public string Fetch(string url, string method, NameValueCollection data = null,
}
}
}
}
}

public HttpWebResponse Request(string url, string method, NameValueCollection data = null, bool ajax = true, string referer = "")

/// <param name="fetchError">Return response even if its status code is not 200</param>
public HttpWebResponse Request(string url, string method, NameValueCollection data = null, bool ajax = true, string referer = "", bool fetchError = false)
{
//Append the data to the URL for GET-requests
bool isGetMethod = (method.ToLower() == "get");
Expand Down Expand Up @@ -82,7 +84,23 @@ public HttpWebResponse Request(string url, string method, NameValueCollection da
}

// Get the response
return request.GetResponse() as HttpWebResponse;
try
{
return request.GetResponse() as HttpWebResponse;
}
catch (WebException ex)
{
//this is thrown if response code is not 200
if (fetchError)
{
var resp = ex.Response as HttpWebResponse;
if (resp != null)
{
return resp;
}
}
throw;
}
}

/// <summary>
Expand Down
32 changes: 14 additions & 18 deletions SteamTrade/TradeOffer/OfferSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,46 +27,40 @@ public OfferSession(TradeOfferWebAPI webApi, SteamWeb steamWeb)
JsonSerializerSettings.Formatting = Formatting.None;
}

public bool Accept(string tradeOfferId, out string tradeId)
{
tradeId = "";
public TradeOfferAcceptResponse Accept(string tradeOfferId)
{
var data = new NameValueCollection();
data.Add("sessionid", steamWeb.SessionId);
data.Add("serverid", "1");
data.Add("tradeofferid", tradeOfferId);

string url = string.Format("https://steamcommunity.com/tradeoffer/{0}/accept", tradeOfferId);
string referer = string.Format("https://steamcommunity.com/tradeoffer/{0}/", tradeOfferId);

string resp = steamWeb.Fetch(url, "POST", data, false, referer);
string resp = steamWeb.Fetch(url, "POST", data, false, referer, true);

if (!String.IsNullOrEmpty(resp))
{
try
{
var result = JsonConvert.DeserializeObject<TradeOfferAcceptResponse>(resp);
if (!String.IsNullOrEmpty(result.TradeId))
{
tradeId = result.TradeId;
return true;
}
//todo: log the error
Debug.WriteLine(result.TradeError);
var res = JsonConvert.DeserializeObject<TradeOfferAcceptResponse>(resp);
res.Accepted = res.TradeError == null;
return res;
}
catch (JsonException jsex)
catch (JsonException)
{
Debug.WriteLine(jsex);
return new TradeOfferAcceptResponse { TradeError = "Error parsing server response: " + resp };
}
}
else
{
var state = webApi.GetOfferState(tradeOfferId);
if (state == TradeOfferState.TradeOfferStateAccepted)
{
return true;
return new TradeOfferAcceptResponse { Accepted = true };
}
}
return false;
return new TradeOfferAcceptResponse();
}

public bool Decline(string tradeOfferId)
Expand Down Expand Up @@ -286,7 +280,9 @@ public class OfferAccessToken
}

public class TradeOfferAcceptResponse
{
{
public bool Accepted { get; set; }

[JsonProperty("tradeid")]
public string TradeId { get; set; }

Expand Down
21 changes: 4 additions & 17 deletions SteamTrade/TradeOffer/TradeOffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,31 +155,18 @@ public bool SendWithToken(out string offerId, string token, string message = "")
/// </summary>
/// <param name="tradeId">the tradeid if successful</param>
/// <returns>true if successful, otherwise false</returns>
public bool Accept(out string tradeId)
public TradeOfferAcceptResponse Accept()
{
tradeId = String.Empty;
if (TradeOfferId == null)
{
Debug.WriteLine("Can't accept a trade without a tradeofferid");
throw new ArgumentException("TradeOfferId");
return new TradeOfferAcceptResponse { TradeError = "Can't accept a trade without a tradeofferid" };
}
if (!IsOurOffer && OfferState == TradeOfferState.TradeOfferStateActive)
{
return Session.Accept(TradeOfferId, out tradeId);
return Session.Accept(TradeOfferId);
}
//todo: log wrong state
Debug.WriteLine("Can't accept a trade that is not active");
return false;
}

/// <summary>
/// Accepts the current offer
/// </summary>
/// <returns>true if successful, otherwise false</returns>
public bool Accept()
{
string tradeId;
return Accept(out tradeId);
return new TradeOfferAcceptResponse { TradeError = "Can't accept a trade that is not active" };
}

/// <summary>
Expand Down