/// <summary>
        /// Gets the inventory of the specifeid user.
        /// </summary>
        /// <param name="userID">An int containing the user identifier.</param>
        /// <returns>An ItemList containing the inventory.</returns>
        public ItemList GetInventory(int userID)
        {
            ItemList ReturnValue = null;

            try
            {
                NameValueCollection RequestData;
                ApiResponse <GetInventoryResponse> Result;

                // Build the post data
                RequestData = new NameValueCollection
                {
                    { "uid", userID.ToString() }
                };

                // Execute the request
                Result = OpSkinsWebRequest <GetInventoryResponse> .ExecuteGet(BuildUrl("/ITrade/GetUserInventory/v1/"), _AuthToken, RequestData);

                if (Result != null && Result.Response != null)
                {
                    ReturnValue = Result.Response.Items;
                }
                else
                {
                    throw new InvalidOperationException("No result data was received");
                }
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException("Could not get trade offers", ex);
            }

            return(ReturnValue);
        }
Beispiel #2
0
        /// <summary>
        /// Executes a web request against the OpSkins API.
        /// </summary>
        /// <param name="url">A string containing the URL for the web request.</param>
        /// <param name="method">A string indicating the method of the web request.</param>
        /// <param name="authToken">A string containing the OpSkins authentication token to use for the request.</param>
        /// <param name="data">A string containing data relating to the web request.</param>
        /// <returns>An ApiResponse of T containing the result returned by the web request.</returns>
        internal static ApiResponse <T> ExecuteRequest(string url, string method, string authToken, string data = null)
        {
            string          Response;
            ApiResponse <T> ReturnValue;

            Response    = OpSkinsWebRequest.ExecuteRequest(url, method, authToken, data);
            ReturnValue = ProcessResponse(Response);

            return(ReturnValue);
        }
Beispiel #3
0
        /// <summary>
        /// Executes a POST against the OpSkins API.
        /// </summary>
        /// <param name="url">A string containing the URL for the web request.</param>
        /// <param name="authToken">A string containing the OpSkins authentication token to use for the request.</param>
        /// <param name="postData">A NameValueCollection containing any data to be POST'd as part of the web request.</param>
        /// <returns>An ApiResponse of T containing the result returned by the web request.</returns>
        internal static ApiResponse <T> ExecutePost(string url, string authToken, NameValueCollection postData = null)
        {
            string          Response;
            ApiResponse <T> ReturnValue;

            Response    = OpSkinsWebRequest.ExecutePost(url, authToken, postData);
            ReturnValue = ProcessResponse(Response);

            return(ReturnValue);
        }
Beispiel #4
0
        /// <summary>
        /// Executes a GET against the OpSkins API.
        /// </summary>
        /// <param name="url">A string containing the URL for the web request.</param>
        /// <param name="authToken">A string containing the OpSkins authentication token to use for the request.</param>
        /// <returns>An ApiResponse of T containing the result returned by the web request.</returns>
        internal static ApiResponse <T> ExecuteGet(string url, string authToken)
        {
            string          Response;
            ApiResponse <T> ReturnValue;

            Response    = OpSkinsWebRequest.ExecuteGet(url, authToken);
            ReturnValue = ProcessResponse(Response);

            return(ReturnValue);
        }
        /// <summary>
        /// Cancels the specified trade offer.
        /// </summary>
        /// <param name="tradeOfferID">An int indicating the identifier of the trade offer.</param>
        /// <returns>A bool indicating success.</returns>
        public bool CancelTradeOffer(int tradeOfferID)
        {
            bool ReturnValue = false;

            try
            {
                NameValueCollection PostData;
                ApiResponse <CancelOfferResponse> Result;

                // Build the post data
                PostData = new NameValueCollection
                {
                    { "offer_id", tradeOfferID.ToString() }
                };

                // Execute the request
                Result = OpSkinsWebRequest <CancelOfferResponse> .ExecutePost(BuildUrl("ITrade/CancelOffer/v1/"), _AuthToken, PostData);

                if (Result != null && Result.Response != null && Result.Response.TradeOffer != null)
                {
                    TradeOffer TheOffer;

                    TheOffer = Result.Response.TradeOffer;
                    if (TheOffer.ID == tradeOfferID)
                    {
                        if (TheOffer.State == TradeOfferState.Cancelled || TheOffer.State == TradeOfferState.Declined)
                        {
                            ReturnValue = true;
                        }
                    }
                    else
                    {
                        throw new InvalidDataException(string.Format("The API returned an invalid trade offer identifier ({0}) during cancellation", TheOffer.ID));
                    }
                }
                else
                {
                    if (Result.Message.Contains("cancel an inactive"))
                    {
                    }
                    else
                    {
                        throw new InvalidOperationException("No result data was received");
                    }
                }
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException("Trade offer could not be cancelled", ex);
            }

            return(ReturnValue);
        }
        /// <summary>
        /// Gets a list of trade offers from the specified data.
        /// </summary>
        /// <param name="data">A string containing the data to get trade offers from.</param>
        public List <TradeOffer> GetTradeOffers(string data)
        {
            List <TradeOffer> ReturnValue = null;
            ApiResponse <GetOffersResponse> Result;

            Result = OpSkinsWebRequest <GetOffersResponse> .ProcessResponse(data);

            if (Result != null && Result.Response != null)
            {
                ReturnValue = Result.Response.TradeOffers;
            }

            return(ReturnValue);
        }
        /// <summary>
        /// Gets the specified trade offer.
        /// </summary>
        /// <param name="tradeID">An int indicating the identifier of the trade offer.</param>
        public TradeOffer GetTradeOffer(int tradeOfferID)
        {
            int        Retries     = 0;
            TradeOffer ReturnValue = null;

            while (Retries++ < 3)
            {
                try
                {
                    NameValueCollection            RequestData;
                    ApiResponse <GetOfferResponse> Result;

                    // Build the post data
                    RequestData = new NameValueCollection
                    {
                        { "offer_id", tradeOfferID.ToString() }
                    };

                    // Execute the request
                    Result = OpSkinsWebRequest <GetOfferResponse> .ExecuteGet(BuildUrl("/ITrade/GetOffer/v1/"), _AuthToken, RequestData);

                    if (Result != null && Result.Response != null)
                    {
                        ReturnValue = Result.Response.TradeOffer;
                        Retries     = 5;
                    }
                    else
                    {
                        throw new InvalidOperationException("No result data was received");
                    }
                }
                catch (Exception ex)
                {
                    LogWriter.LogException(ex);
                }

                if (Retries < 5)
                {
                    Thread.Sleep(5);
                }
            }

            return(ReturnValue);
        }
        /// <summary>
        /// Accepts the specified trade offer.
        /// </summary>
        /// <param name="tradeOfferID">An int containing the identifier of the trade offer.</param>
        /// <returns>A bool indicating success.</returns>
        public bool AcceptTradeOffer(int tradeOfferID)
        {
            bool ReturnValue = false;

            try
            {
                string Code;
                NameValueCollection PostData;
                ApiResponse <AcceptTradeResponse> Result;

                Code = Authenticator.Compute2FA(_2FA);

                // Build the post data
                PostData = new NameValueCollection
                {
                    { "twofactor_code", Code },
                    { "offer_id", tradeOfferID.ToString() }
                };

                // Execute the request
                Result = OpSkinsWebRequest <AcceptTradeResponse> .ExecutePost(BuildUrl("ITrade/AcceptOffer/v1/"), _AuthToken, PostData);

                if (Result != null && Result.Response.TradeOffer != null)
                {
                    ReturnValue = true;
                }
                else
                {
                    throw new InvalidOperationException("No result data was received");
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Trade offer could not be accepted", ex);
            }

            return(ReturnValue);
        }
        /// <summary>
        /// Sends a trade with the specified assets to the specified URL, displaying the specified message.
        /// </summary>
        /// <param name="tradeURL">A string containing the trade URL to send the trade to.</param>
        /// <param name="itemsToSend">A List of string containing the identifiers of the assets to send.</param>
        /// <param name="itemsToReceive">A List of string containing the identifiers of the assets to receive.</param>
        /// <param name="message">A string containing the message to include with the trade.</param>
        /// <param name="tradeID">A string that will be set to the identifier of the trade.</param>
        /// <param name="tradeMessage">A string that will be set to any message associated with the trade.</param>
        /// <returns>A bool indicating whether the request was successful.</returns>
        public bool SendTrade(string tradeURL, List <string> itemsToSend, List <string> itemsToReceive, string message, out string tradeID, out string tradeMessage)
        {
            bool ReturnValue = false;

            // Defaults
            tradeID      = null;
            tradeMessage = null;

            try
            {
                // Check the trade URL is valid
                if (HelperFunctions.TryParseTradeURL(tradeURL, out string UID, out string Token))
                {
                    // Check that there are items
                    if ((itemsToSend != null && itemsToSend.Count > 0) ||
                        (itemsToReceive != null && itemsToReceive.Count > 0))
                    {
                        int Retries = 0;

                        while (Retries++ < 5)
                        {
                            string Code;
                            NameValueCollection             PostData;
                            ApiResponse <SendOfferResponse> Result;

                            Code = Authenticator.Compute2FA(_2FA);

                            // Build the post data
                            PostData = new NameValueCollection
                            {
                                { "twofactor_code", Code },
                                { "uid", UID },
                                { "token", Token }
                            };
                            //PostData.Add("items", BuildItemList(items));
                            if (itemsToReceive != null && itemsToReceive.Count > 0)
                            {
                                PostData.Add("items_to_receive", BuildItemList(itemsToReceive));
                            }
                            if (itemsToSend != null && itemsToSend.Count > 0)
                            {
                                PostData.Add("items_to_send", BuildItemList(itemsToSend));
                            }
                            if (message != null)
                            {
                                PostData.Add("message", message);
                            }

                            // Execute the request
                            Result = OpSkinsWebRequest <SendOfferResponse> .ExecutePost(BuildUrl("/ITrade/SendOffer/v1/"), _AuthToken, PostData);

                            if (Result != null)
                            {
                                if (Result.Response != null && Result.Response.TradeOffer != null)
                                {
                                    TradeOffer TheOffer;

                                    TheOffer = Result.Response.TradeOffer;
                                    switch (TheOffer.State)
                                    {
                                    case TradeOfferState.Active:
                                        tradeID     = TheOffer.ID.ToString();
                                        ReturnValue = true;
                                        break;

                                    default:
                                        tradeMessage = TheOffer.StateName;
                                        break;
                                    }

                                    Retries = 10;
                                }
                                else if (Result.Message != null)
                                {
                                    if (Result.Message == "Two-factor code already used.")
                                    {
                                        tradeMessage = "The bot reported an error whilst sending the trade";
                                        throw new InvalidOperationException("The specified 2FA code has already been used");
                                    }
                                    else if (Result.Message.Contains("that do not belong to you"))
                                    {
                                        tradeMessage = "The chosen items are not valid for this trade";
                                        throw new InvalidOperationException("The specified items do not belong to the user");
                                    }
                                    else
                                    {
                                        tradeMessage = Result.Message;
                                        throw new InvalidOperationException(string.Format("The Send Trade call returned: {0}", Result.Message));
                                    }
                                }
                            }
                            else
                            {
                                throw new InvalidOperationException("No result data was received");
                            }
                        }
                    }
                    else
                    {
                        throw new ArgumentException("No items were specified for the trade", "itemsToSend");
                    }
                }
                else
                {
                    tradeMessage = "The user's trade URL appears to be invalid";
                    throw new ArgumentException("The specified trade URL is invalid", "tradeURL");
                }
            }
        /// <summary>
        /// Gets a list of trade offers for the specified trade offer states.
        /// </summary>
        /// <param name="states">A List of TradeOfferState containing the states to get trade offers for.</param>
        /// <param name="state">A TradeOfferTypes indicating the types of trade offer to get.</param>
        public List <TradeOffer> GetTradeOffers(List <TradeOfferState> states, TradeOfferTypes tradeOfferTypes)
        {
            List <TradeOffer> ReturnValue = null;
            int Retries = 0;

            while (Retries++ < 3)
            {
                try
                {
                    NameValueCollection             RequestData;
                    ApiResponse <GetOffersResponse> Result;

                    // Build the post data
                    RequestData = new NameValueCollection
                    {
                        { "twofactor_code", Authenticator.Compute2FA(_2FA) }
                    };
                    if (states != null && states.Count > 0)
                    {
                        string StateList = string.Empty;

                        foreach (TradeOfferState State in states)
                        {
                            StateList += string.Format("{0},", (int)State);
                        }
                        StateList = StateList.TrimEnd(new char[] { ',' });
                        RequestData.Add("state", StateList);
                    }

                    // Add the trade offer type
                    switch (tradeOfferTypes)
                    {
                    case TradeOfferTypes.Received:
                        RequestData.Add("type", "received");
                        break;

                    case TradeOfferTypes.Sent:
                        RequestData.Add("type", "sent");
                        break;
                    }

                    // Execute the request
                    Result = OpSkinsWebRequest <GetOffersResponse> .ExecuteGet(BuildUrl("ITrade/GetOffers/v1/"), _AuthToken, RequestData);

                    if (Result != null && Result.Response != null)
                    {
                        ReturnValue = Result.Response.TradeOffers;
                    }
                    else
                    {
                        throw new InvalidOperationException("No result data was received");
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Could not get trade offers: {0}", ex);
                }
            }

            return(ReturnValue);
        }
        /// <summary>
        /// Gets all items for the specified application identifier.
        /// </summary>
        /// <param name="appID">An int indicating the application to get all items for.</param>
        /// <param name="pageNumber">An int indicating the current page to get items from.</param>
        /// <param name="perPage">An int indicating the size of each page that should be returned.</param>
        /// <param name="skuFilter">A List of int indicating the SKUs of items to return.</param>
        /// <param name="nameFilter">A string containing the name to filter items by.</param>
        /// <param name="showAllItems">A bool that indicates whether to return all items (some items are excluded by default).</param>
        /// <param name="sortOrder">An ItemSorts specifying the sort order of the items in the result set.</param>
        public void GetAllItems(int appID, int pageNumber = 1, int perPage = 100, List <int> skuFilter = null, string nameFilter = null, bool showAllItems = false, ItemSorts sortOrder = ItemSorts.Undefined)
        {
            ItemList ReturnValue = null;

            // Validate parameters
            if (appID < 1)
            {
                throw new ArgumentException("The specified application identifier is invalid", "appID");
            }
            if (pageNumber < 1)
            {
                throw new ArgumentException("The specified page number is invalid", "pageNumber");
            }
            if (perPage < 1 || perPage > 100)
            {
                throw new ArgumentException("The specified results per page is invalid", "perPage");
            }

            try
            {
                NameValueCollection RequestData;
                ApiResponse <GetInventoryResponse> Result;

                // Build the post data
                RequestData = new NameValueCollection
                {
                    { "app_id", appID.ToString() },
                    { "page", pageNumber.ToString() },
                    { "per_page", perPage.ToString() }
                };
                if (skuFilter != null && skuFilter.Count > 0)
                {
                    string SkuList = string.Empty;

                    foreach (int Item in skuFilter)
                    {
                        SkuList += string.Format("{0},", Item);
                    }
                    SkuList = SkuList.Trim(',');
                    RequestData.Add("sku", SkuList);
                }
                if (sortOrder != ItemSorts.Undefined)
                {
                    RequestData.Add("sort", ((int)sortOrder).ToString());
                }
                if (showAllItems)
                {
                    RequestData.Add("no_exclusions", "1");
                }
                if (nameFilter != null && nameFilter.Length > 0)
                {
                    RequestData.Add("name", nameFilter);
                }

                GenerateAuthToken();
                // Execute the request
                Result = OpSkinsWebRequest <GetInventoryResponse> .ExecuteGet("https://api-trade.opskins.com/IItem/GetAllItems/v1/", _AuthToken, RequestData);

                if (Result != null && Result.Response != null)
                {
                    ReturnValue = Result.Response.Items;
                }
                else
                {
                    throw new InvalidOperationException("No result data was received");
                }
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException("Could not get trade offers", ex);
            }

            // return ReturnValue;
        }