Beispiel #1
0
        public int SendBuyRequest(int price, int commodity, int amount)// The buy request function using the BuyRequest class
        {
            SimpleHTTPClient HTTPClient = new SimpleHTTPClient();
            BuyRequest       request    = new BuyRequest();

            request.type      = "buy";
            request.price     = price;
            request.commodity = commodity;
            request.amount    = amount;
            myNonce           = randomNonce();
            string token    = SimpleCryptoLibrary.CreateToken(user + "_" + myNonce, PrivateKey);
            bool   eflag    = false;
            string response = "";

            try
            {
                String s = SimpleCryptoLibrary.decrypt(myNonce, PrivateKey);
                Convert.ToInt32(s);
            }
            catch (Exception d)
            {
                if (d.ToString().Equals("non unique nonce"))
                {
                    myNonce = randomNonce();
                    String s = SimpleCryptoLibrary.decrypt(myNonce, PrivateKey);
                }
            }
            try
            {
                response = HTTPClient.SendPostRequest(url, user, token, myNonce, PrivateKey, request);
                Convert.ToInt32(response);
            }
            catch (Exception e)
            {
                error = response;
                eflag = true;
            }
            if (!eflag)
            {
                int output = Convert.ToInt32(response);
                logger.Info("A buy request was sent to the server for commodity id " + commodity + ", amount " + amount + ", in the price of " + price + ". The buy was succesful, the request ID was " + output);
                return(output);
            }

            else
            {
                logger.Error("A buy request was sent to the server for commodity id " + commodity + ", amount " + amount + ", in the price of " + price + ". An error occurred: " + error);
                return(-1);
            }
        }
        /// <summary>
        /// Send an object of type T1, @item, parsed as json string embedded with the
        /// authentication token, that is build using @user and @token,
        /// as an HTTP post request to server at address @url.
        /// This method reutens the server response as is.
        /// </summary>
        /// <typeparam name="T1">Type of the data object to send</typeparam>
        /// <param name="url">address of the server</param>
        /// <param name="user">username for authentication data</param>
        /// <param name="token">token for authentication data</param>
        /// <param name="item">the data item to send in the reuqest</param>
        /// <returns>the server response</returns>
        public string SendPostRequest <T1>(string url, string user, string token, string nonce, string privateKey, T1 item)
        {
            var     auth     = new { user, nonce, token };
            JObject jsonItem = JObject.FromObject(item);

            jsonItem.Add("auth", JObject.FromObject(auth));
            StringContent content = new StringContent(jsonItem.ToString());

            using (var client = new HttpClient())
            {
                var result          = client.PostAsync(url, content).Result;
                var responseContent = result?.Content?.ReadAsStringAsync().Result;

                responseContent = SimpleCryptoLibrary.decrypt(responseContent, privateKey);
                return(responseContent);
            }
        }
Beispiel #3
0
        public IMarketCommodityOffer SendQueryMarketRequest(int commodity) //The query market request function using the QueryMarketRequest class and returning a IMarketCommodityOffer object
        {
            SimpleHTTPClient   HTTPClient = new SimpleHTTPClient();
            QueryMarketRequest request    = new QueryMarketRequest();

            request.type      = "queryMarket";
            request.commodity = commodity;
            myNonce           = randomNonce();
            string token = SimpleCryptoLibrary.CreateToken(user + "_" + myNonce, PrivateKey);
            MarketCommodityOffer response = null;
            bool eflag = false;

            try
            {
                String s = SimpleCryptoLibrary.decrypt(myNonce, PrivateKey);
                Convert.ToInt32(s);
            }
            catch (Exception d)
            {
                if (d.ToString().Equals("non unique nonce"))
                {
                    myNonce = randomNonce();
                    String s = SimpleCryptoLibrary.decrypt(myNonce, PrivateKey);
                }
            }
            try
            {
                response = HTTPClient.SendPostRequest <QueryMarketRequest, MarketCommodityOffer>(url, user, token, myNonce, PrivateKey, request);
            }
            catch (Exception e)
            {
                error = e.Message;
                eflag = true;
            }
            if (eflag)
            {
                logger.Error("A query market request was sent to the server for commodity ID " + commodity + ". An error occurred: " + error);
                return(null);
            }

            else
            {
                logger.Info("A query market request was sent to the server for commodity ID " + commodity + ". The response from the server was: " + response);
                return(response);
            }
        }
Beispiel #4
0
        public List <MarketUserRequests> SendUserRequestsQuery()
        {
            SimpleHTTPClient  HTTPClient = new SimpleHTTPClient();
            UserRequestsQuery request    = new UserRequestsQuery();

            request.type = "queryUserRequests";
            myNonce      = randomNonce();
            string token = SimpleCryptoLibrary.CreateToken(user + "_" + myNonce, PrivateKey);
            List <MarketUserRequests> response = new List <MarketUserRequests>();
            bool eflag = false;

            try
            {
                String s = SimpleCryptoLibrary.decrypt(myNonce, PrivateKey);
                Convert.ToInt32(s);
            }
            catch (Exception d)
            {
                if (d.ToString().Equals("non unique nonce"))
                {
                    myNonce = randomNonce();
                    String s = SimpleCryptoLibrary.decrypt(myNonce, PrivateKey);
                }
            }
            try
            {
                response = HTTPClient.SendPostRequest <UserRequestsQuery, List <MarketUserRequests> >(url, user, token, myNonce, PrivateKey, request);
            }
            catch (Exception e)
            {
                error = e.Message;
                eflag = true;
            }
            if (eflag)
            {
                logger.Error("The server received a SendUserRequestsQuery request. An error occurred: " + error);
                return(null);
            }

            else
            {
                logger.Info("The server received a SendUserRequestsQuery request.");
                return(response);
            }
        }
Beispiel #5
0
        public bool SendCancelBuySellRequest(int id) // The cancel request function using the CancelRequest class
        {
            SimpleHTTPClient HTTPClient = new SimpleHTTPClient();
            CancelRequest    request    = new CancelRequest();

            request.type = "cancelBuySell";
            request.id   = id;
            myNonce      = randomNonce();
            string token    = SimpleCryptoLibrary.CreateToken(user + "_" + myNonce, PrivateKey);
            string response = HTTPClient.SendPostRequest(url, user, token, myNonce, PrivateKey, request);

            if (response.Equals("Ok"))
            {
                logger.Info("A cancel request was sent to the server for ID " + id + ". Successfully canceled.");
                return(true);
            }

            else
            {
                error = response;
                logger.Error("A cancel request was sent to the server for ID " + id + ". An error occurred: " + error);
                return(false);
            }
        }