Skip to content

Commit

Permalink
Merge pull request #7 from jackofdiamond5/bpenkov/1.0.2
Browse files Browse the repository at this point in the history
Bpenkov/1.0.2
  • Loading branch information
jackofdiamond5 authored Jan 19, 2019
2 parents b8f38d3 + e72466e commit f23576a
Show file tree
Hide file tree
Showing 7 changed files with 218 additions and 47 deletions.
108 changes: 74 additions & 34 deletions src/BackpackTfApi/BackpackTfUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@ public WebUsersData GetUsersByIds(ICollection<string> ids)
$"steamids={string.Join(',', ids)}");
using (var client = new WebClient())
return WebUsersData.FromJson(client.DownloadString(uri));

}

/// <summary>
Expand Down Expand Up @@ -211,19 +210,15 @@ public UserToken.Classifieds.ClassifiedsSearch.Models.Response GetClassifieds()
/// <summary>
/// Fetches the currently opened user's classifieds from backpack.tf.
/// </summary>
/// <param name="intent">0 - Buy Listings 1 - Sell Listings. Returns both if not set.</param>
/// <param name="intent">0 - Buy Listings. 1 - Sell Listings. Returns both if not set.</param>
/// <param name="inactive">0 - Include inactive listings. 1 - Skip inactive listings.</param>
/// <returns></returns>
/// <exception cref="WebException"></exception>
/// <exception cref="NotSupportedException"></exception>
public UserToken.Classifieds.UserListings.Models.Response GetOwnClassifieds(int? intent = null, int inactive = 1)
{
var uri = this.BuildUri(BaseUris.UserListings,
this.AccessToken,
$"intent={intent}",
$"inactive={inactive}");
using (var client = new WebClient())
return UserListingsData.FromJson(client.DownloadString(uri));
var uri = this.BuildUri(BaseUris.UserListings, this.AccessToken);
return UserListingsHandler.GetUserListings(uri, intent, inactive);
}

/// <summary>
Expand All @@ -235,7 +230,7 @@ public UserToken.Classifieds.UserListings.Models.Response GetOwnClassifieds(int?
/// <param name="price">The price for which the listing will be created.</param>
/// <param name="message">The text that will be typed in the message section of the listing.</param>
/// <exception cref="ItemNotFoundException"></exception>
/// <exception cref="ItemCreationFailureException"></exception>
/// <exception cref="ClassifiedCreationFailureException"></exception>
/// <returns></returns>
public UserToken.Classifieds.ListingsCreator.Models.Response CreateSellListing(
string fullItemName, string currency, decimal price, string message)
Expand All @@ -245,27 +240,40 @@ public UserToken.Classifieds.ListingsCreator.Models.Response CreateSellListing(
{
itemAsset = InventoryHandler.GetItemAsset(this.userInventory, fullItemName);
}
catch (WebException)
catch (InvalidOperationException)
{
throw new ItemNotFoundException(Messages.ItemNotFoundError);
throw new ItemNotFoundException($"{Messages.ItemNotFoundError} Item - {fullItemName}");
}

var currencies = new Dictionary<string, decimal>
{
{ currency, price }
};
var listings = new List<InputListing>
if (itemAsset != null)
{
new InputListing(1, currencies, message, itemAsset.AssetId)
};
var listings = this.CreateListingsCollection(1, currency, price, message, itemAsset.AssetId);
return this.PostListing(listings);
}

if (itemAsset != null)
throw new ClassifiedCreationFailureException($"Failed to create listing for item - {fullItemName}");
}

/// <summary>
/// Creates a sell listing / classified on backpack.tf.
/// </summary>
/// <param name="item">An item extracted from the user's inventory.</param>
/// <param name="currency">The currency type.</param>
/// <param name="price">The price for which the listing will be created.</param>
/// <param name="message">The text that will be typed in the message section of the listing.</param>
/// <exception cref="ClassifiedCreationFailureException"></exception>
/// <returns></returns>
public UserToken.Classifieds.ListingsCreator.Models.Response CreateSellListing(
InventoryItem item, string currency, decimal price, string message)
{
var itemAssetId = item.Asset.AssetId;
if (itemAssetId != null)
{
var uri = this.BuildUri(BaseUris.ClassifiedsCreate, this.AccessToken);
return UserListingsHandler.CreateListings(new Input(listings), uri);
var listings = this.CreateListingsCollection(1, currency, price, message, itemAssetId);
return this.PostListing(listings);
}

throw new ItemCreationFailureException($"Failed to create listing for item - {fullItemName}");
throw new ClassifiedCreationFailureException($"Failed to create listing for item - {item.Name}");
}

/// <summary>
Expand All @@ -277,28 +285,41 @@ public UserToken.Classifieds.ListingsCreator.Models.Response CreateSellListing(
/// <param name="currency">The currency type.</param>
/// <param name="price">The price for which the listing will be created.</param>
/// <param name="message">The text that will be typed in the message section of the listing.</param>
/// <exception cref="ItemCreationFailureException"></exception>
/// <exception cref="ClassifiedCreationFailureException"></exception>
/// <returns></returns>
public UserToken.Classifieds.ListingsCreator.Models.Response CreateBuyListing(
string fullItemName, string qualityIndex, string currency, decimal price, string message)
{
var item = new ListingItem(fullItemName, qualityIndex);
var currencies = new Dictionary<string, decimal>
{
{ currency, price }
};
var listings = new List<InputListing>()
var listings = this.CreateListingsCollection(0, currency, price, message, null, item);
if (listings.Count > 0)
{
new InputListing(0, currencies, message, null, item)
};
return this.PostListing(listings);
}

throw new ClassifiedCreationFailureException($"Failed to create listing for item - {fullItemName}");
}

/// <summary>
/// Creates a buy listing / classified on backpack.tf.
/// </summary>
/// <param name="item">An item extracted from the user's inventory.</param>
/// <param name="currency">The currency type.</param>
/// <param name="price">The price for which the listing will be created.</param>
/// <param name="message">The text that will be typed in the message section of the listing.</param>
/// <exception cref="ClassifiedCreationFailureException"></exception>
/// <returns></returns>
public UserToken.Classifieds.ListingsCreator.Models.Response CreateBuyListing(
InventoryItem item, string currency, decimal price, string message)
{
var listingItem = new ListingItem(item.Name, item.QualityIndex);
var listings = this.CreateListingsCollection(0, currency, price, message, null, listingItem);
if (listings.Count > 0)
{
var uri = this.BuildUri(BaseUris.ClassifiedsCreate, this.AccessToken);
return UserListingsHandler.CreateListings(new Input(listings), uri);
return this.PostListing(listings);
}

throw new ItemCreationFailureException($"Failed to create listing for item - {fullItemName}");
throw new ClassifiedCreationFailureException($"Failed to create listing for item - {item.Name}");
}

/// <summary>
Expand Down Expand Up @@ -346,7 +367,7 @@ public InventoryItem GetItemFromInventory(string itemName)

if (itemAsset == null || itemDescription == null)
{
throw new ItemNotFoundException(Messages.ItemNotFoundError);
throw new ItemNotFoundException($"{Messages.ItemNotFoundError} Item name - {itemName}");
}

return new InventoryItem(itemAsset, itemDescription);
Expand All @@ -363,5 +384,24 @@ private string BuildUri(string baseUri, params string[] args)

return builder.ToString().TrimEnd('&');
}

private UserToken.Classifieds.ListingsCreator.Models.Response PostListing(ICollection<InputListing> listings)
{
var uri = this.BuildUri(BaseUris.ClassifiedsCreate, this.AccessToken);
return UserListingsHandler.CreateListings(new Input(listings), uri);
}

private ICollection<InputListing> CreateListingsCollection(
int intent, string currency, decimal price, string message, string assetId = null, ListingItem item = null)
{
var currencies = new Dictionary<string, decimal>
{
{ currency, price }
};
return new List<InputListing>
{
new InputListing(intent, currencies, message, assetId, item)
};
}
}
}
10 changes: 10 additions & 0 deletions src/BackpackTfApi/Exceptions/ClassifiedCreationFailureException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;

namespace BackpackTfApi.Exceptions
{
public class ClassifiedCreationFailureException : Exception
{
public ClassifiedCreationFailureException(string message)
: base(message) { }
}
}
10 changes: 0 additions & 10 deletions src/BackpackTfApi/Exceptions/ItemCreationFailureException.cs

This file was deleted.

9 changes: 9 additions & 0 deletions src/BackpackTfApi/Static/BaseUris.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,23 @@
public static class BaseUris
{
public const string AppId = "440";

public const string GetCurrencies = "https://backpack.tf/api/IGetCurrencies/v1?key=";

public const string GetPriceHistory = "https://backpack.tf/api/IGetPriceHistory/v1?key=";

public const string GetPrices = "https://backpack.tf/api/IGetPrices/v4?key=";

public const string GetSpecialItems = "https://backpack.tf/api/IGetSpecialItems/v1?key=";

public const string GetUsers = "https://backpack.tf/api/IGetUsers/v3?key=";

public const string GetImpersonatedUsers = "https://backpack.tf/api/IGetUsers/GetImpersonatedUsers?";

public const string ClassifiedsSearch = "https://backpack.tf/api/classifieds/search/v1?key=";

public const string UserListings = "https://backpack.tf/api/classifieds/listings/v1?token=";

public const string ClassifiedsCreate = "https://backpack.tf/api/classifieds/list/v1?token=";
}
}
2 changes: 2 additions & 0 deletions src/BackpackTfApi/Static/Messages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,7 @@ public static class Messages
public const string ResponseNullError = "Cannot resolve \"response\" argument.";

public const string InventoryNullError = "The user's inventory cannot be null.";

public const string ItemQualityError = "Failed to determine item quality.";
}
}
108 changes: 107 additions & 1 deletion src/BackpackTfApi/SteamUser/UserInventory/Models/InventoryItem.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,121 @@
namespace BackpackTfApi.SteamUser.UserInventory.Models
using System;
using System.Linq;

using BackpackTfApi.Static;
using BackpackTfApi.Exceptions;

namespace BackpackTfApi.SteamUser.UserInventory.Models
{
/// <summary>
/// A Steam inventory item.
/// </summary>
public class InventoryItem
{
private const string TargetTag = "quality";

public InventoryItem(Asset asset, Description description)
{
this.Asset = asset;
this.Description = description;
}

/// <summary>
/// The name of the item.
/// </summary>
public string Name
{
get
{
if (string.IsNullOrWhiteSpace(this.Description.MarketHashName))
{
throw new ItemNotFoundException(Messages.ItemNotFoundError);
}

return this.Description.MarketHashName;
}
}

/// <summary>
/// The quality name of the specified item.
/// </summary>
public string QualityName
{
get
{
try
{
return this.Description
.Tags
.Where(t => t.Category.ToLower() == TargetTag)
.First()
.InternalName;
}
catch (InvalidOperationException)
{
throw new ItemNotFoundException(Messages.ItemNotFoundError);
}
}
}

/// <summary>
/// The quality index of the specified item.
/// </summary>
public string QualityIndex => this.GetItemQualityIndex();

/// <summary>
/// The Steam inventory's asset object.
/// </summary>
public Asset Asset { get; internal set; }

/// <summary>
/// The Steam inventory's description object.
/// </summary>
public Description Description { get; internal set; }

private string GetItemQualityIndex()
{
try
{
return this.GetIndex(this.QualityName);
}
catch (ItemNotFoundException)
{
return this.GetIndex(this.Name);
}
}

private string GetIndex(string value)
{
var quality = value.Split().Take(1).ToArray()[0].ToLower();
switch (quality)
{
case "normal":
return "0";
case "genuine":
return "1";
case "vintage":
return "3";
case "unusual":
return "5";
case "unique":
return "6";
case "community":
return "7";
case "valve":
return "8";
case "self-made":
return "9";
case "strange":
return "11";
case "haunted":
return "13";
case "collector's":
return "14";
case "decorated":
return "15";
default:
throw new ArgumentException(Messages.ItemQualityError);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
using System.Net;
using System;
using System.Net;
using System.Linq;

using BackpackTfApi.Static;
using BackpackTfApi.Exceptions;

namespace BackpackTfApi.SteamUser.UserInventory.Models
{
public static class InventoryHandler
Expand All @@ -23,13 +27,23 @@ public static Response DownloadUserInventory(string uri)
/// <param name="itemName"></param>
/// <returns></returns>
public static Description GetItemDescription(Response steamInventory, string itemName)
=> steamInventory.Descriptions.Where(d => d.Name == itemName).First();
{
try
{
return steamInventory.Descriptions.Where(d => d.Name == itemName).First();
}
catch (InvalidOperationException)
{
throw new ItemNotFoundException($"{Messages.ItemNotFoundError} Item name - {itemName}");
}
}

/// <summary>
/// Gets the Asset object of the targeted item in the user's inventory.
/// </summary>
/// <param name="steamInventory"></param>
/// <param name="itemName"></param>
/// <exception cref="InvalidOperationException"></exception>
/// <returns></returns>
public static Asset GetItemAsset(Response steamInventory, string itemName)
{
Expand Down

0 comments on commit f23576a

Please sign in to comment.