Beispiel #1
0
        /// <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 static string SendPostRequest <T1>(string url, string user, T1 item)
        {
            string responseContent = "";
            int    nonce           = new Random().Next(Int32.MinValue, Int32.MaxValue);
            string token           = SimpleCtyptoLibrary.CreateToken(User, nonce);
            //Trace.WriteLine("user==User: "******"\nuser.equals(User): "+(user.Equals(User)));
            var     auth     = new { user, token, nonce };
            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;
                responseContent = result?.Content?.ReadAsStringAsync().Result;
                try
                {
                    return(SimpleCtyptoLibrary.Decrypt(responseContent));
                }
#pragma warning disable CS0168 // Variable is declared but never used
                catch (Exception e)
#pragma warning restore CS0168 // Variable is declared but never used
                {
                    return(SendPostRequest <T1>(url, user, item));
                }
            }
        }
Beispiel #2
0
        public int SendBuyRequest(int price, int commodity, int amount)
        {
            SimpleHTTPClient client = new SimpleHTTPClient();



            string token = SimpleCtyptoLibrary.CreateToken("user52", key);
            var    item  = new MarketItems.BuySellRequest();

            item.type      = "buy";
            item.price     = price;
            item.commodity = commodity;
            item.amount    = amount;
            mainLog.Debug("buying requset send to the server. information: " + item.ToString());
            string output = client.SendPostRequest <MarketItems.BuySellRequest> ("http://ise172.ise.bgu.ac.il", "user52", token, item);

            mainLog.Debug("return answere from the server after buing request: " + output);


            if (!(checkMarketResponse(output)))
            {
                throw new ApplicationException(output);
            }
            int integerOutput;

            int.TryParse(output, out integerOutput);
            return(integerOutput);
        }
Beispiel #3
0
        /// <summary>
        /// Sends a rquest for sale of given amount of commodities for given price.
        /// </summary>
        /// <param name="price">The price offer for the sale</param>
        /// <param name="commodity">The commodity id</param>
        /// <param name="amount">The amount of commodities up for sale</param>
        /// <returns>A request id of the sale</returns>
        public int SendSellRequest(int price, int commodity, int amount)
        {
            SimpleHTTPClient client   = new SimpleHTTPClient();
            SellRequest      request  = new SellRequest("sell", commodity, amount, price);
            string           token    = SimpleCtyptoLibrary.CreateToken(User, PrivateKey);
            string           response = client.SendPostRequest(Url, User, token, request);
            int a;

            if (!int.TryParse(response, out a))
            {
                Console.ForegroundColor = ConsoleColor.Red;
                if (response == "Bad commodity")
                {
                    throw (new MarketException("The request relates to a non existing commodity Id."));
                }
                if (response == "Insufficient commodity")
                {
                    throw (new MarketException("Not enough  amount of commodity to complete the sell."));
                }
            }

            Console.ForegroundColor = ConsoleColor.Green;
            int id = int.Parse(response);

            Console.WriteLine("You have successfully sold the commodities, here is your request Id: " + id);
            Console.ResetColor();
            return(id);
        }
Beispiel #4
0
        /// <summary>
        /// Offers best Bid price and best Ask price for a given commodity.
        /// </summary>
        /// <param name="commodity">The wanted commidity id.</param>
        /// <returns>Prints the best bid price and best ask price for given commodity.</returns>
        public IMarketCommodityOffer SendQueryMarketRequest(int commodity)
        {
            SimpleHTTPClient      client   = new SimpleHTTPClient();
            QueryMarket           request  = new QueryMarket("queryMarket", commodity);
            string                token    = SimpleCtyptoLibrary.CreateToken(User, PrivateKey);
            IMarketCommodityOffer response = client.SendPostRequest <QueryMarket, MarketCommodityOffer>(Url, User, token, request);

            return(response);
        }
Beispiel #5
0
        /// <summary>
        /// Describes user information, including inventory, funds and active request ID's description.
        /// </summary>
        /// <returns>Prints a description of user information, including inventory, funds and active request ID's</returns>
        public IMarketUserData SendQueryUserRequest()
        {
            SimpleHTTPClient client   = new SimpleHTTPClient();
            QueryUser        request  = new QueryUser("queryUser");
            string           token    = SimpleCtyptoLibrary.CreateToken(User, PrivateKey);
            IMarketUserData  response = client.SendPostRequest <QueryUser, MarketUserData>(Url, User, token, request);

            return(response);
        }
Beispiel #6
0
        /// <summary>
        /// Describes a Sell/Purchase by given request id.
        /// </summary>
        /// <param name="id">The request id of the Purchase/Sell</param>
        /// <returns>Prints a description of the wanted Purchase/Sell, including information such as: commodity, price, amount, username and request type.</returns>
        public IMarketItemQuery SendQueryBuySellRequest(int id)
        {
            SimpleHTTPClient    client  = new SimpleHTTPClient();
            QueryBuySellRequest request = new QueryBuySellRequest(id, "queryBuySell");
            string           token      = SimpleCtyptoLibrary.CreateToken(User, PrivateKey);
            IMarketItemQuery response   = client.SendPostRequest <QueryBuySellRequest, MarketItemQuery>(Url, User, token, request);

            return(response);
        }
Beispiel #7
0
        public void TestSimpleHTTPPost()
        {
            // Attantion!, this code is not good practice! this was made for the sole purpose of being an example.
            // A real good code, should use defined classes and and not hardcoded values!
            SimpleHTTPClient client = new SimpleHTTPClient();
            var request             = new{
                type = "queryUser",
            };
            string response = client.SendPostRequest(Url, User, SimpleCtyptoLibrary.CreateToken(User, PrivateKey), request);

            Trace.Write($"Server response is: {response}");
        }
Beispiel #8
0
        public IMarketUserData SendQueryUserRequest()
        {
            SimpleHTTPClient client = new SimpleHTTPClient();
            string           token  = SimpleCtyptoLibrary.CreateToken("user52", key);
            var item = new MarketItems.QueryUserRequest();

            item.type = "queryUser";
            mainLog.Info("Send Query User Request to the server. information: " + item.ToString());
            MarketUserData output = client.SendPostRequest <QueryUserRequest, MarketUserData>("http://ise172.ise.bgu.ac.il", "user52", token, item);

            mainLog.Info("return answere from the server after Send Query User Request: " + output);
            return(output);
        }
Beispiel #9
0
        /// <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 virtual string SendPostRequest <T1>(string url, string user, string privateKey, T1 item)
        {
            string  token    = SimpleCtyptoLibrary.CreateToken(user, privateKey);
            var     auth     = new { user, 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;
                return(responseContent);
            }
        }
Beispiel #10
0
        public bool SendCancelBuySellRequest(int id)
        {
            SimpleHTTPClient client = new SimpleHTTPClient();
            string           token  = SimpleCtyptoLibrary.CreateToken("user52", key);
            var item = new MarketItems.CancelBuySellRequest();

            item.type = "cancelBuySell";
            item.id   = id;
            mainLog.Info("Send Cancel Buy Sell Request to the server. information: " + item.ToString());
            var output = client.SendPostRequest <CancelBuySellRequest>("http://ise172.ise.bgu.ac.il", "user52", token, item);

            mainLog.Info("return answere from the server afterSend Cancel Buy Sell Request: " + output);

            if (output == "Ok")
            {
                return(true);
            }
            throw new Exception(output);
        }
Beispiel #11
0
        /// <summary>
        /// Canceles a buy/sell by refunding commodity/funds.
        /// </summary>
        /// <param name="id">The request id of the purchase/sell.</param>
        /// <returns>A boolean type variable validating the cancel.</returns>
        public bool SendCancelBuySellRequest(int id)
        {
            SimpleHTTPClient     client  = new SimpleHTTPClient();
            CancelBuySellRequest request = new CancelBuySellRequest("cancelBuySell", id);
            string token    = SimpleCtyptoLibrary.CreateToken(User, PrivateKey);
            string response = client.SendPostRequest(Url, User, token, request);

            int a;

            if (!int.TryParse(response, out a))
            {
                if (response == "Id not found")
                {
                    throw (new MarketException("The request ID was not found."));
                }
            }
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("You have successfully canceled the request.");
            Console.ResetColor();
            return(true);
        }
        public override string SendPostRequest <T1>(string url, string user, string privateKey, T1 item)
        {
            nonceInt += 1;
            string nonce     = nonceInt.ToString();
            string userNonce = user + "_" + nonce;
            string token     = SimpleCtyptoLibrary.CreateToken(userNonce, privateKey);

            var     auth     = new { user, token, nonce };
            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;

                bool   failed           = false;
                string decryptedContent = "";
                try
                {
                    decryptedContent = SimpleCtyptoLibrary.decrypt(responseContent, privateKey);
                    //responseContent = decryptedContent;
                }
                catch (Exception e)
                {
                    myLogger.Error("Failed decryption of market response. Error: " + e.Message);
                    failed = true;
                }
                if (!failed)
                {
                    return(decryptedContent);
                }
                else
                {
                    return(responseContent);
                }
            }
        }
Beispiel #13
0
        public DefaultLoginInfo()
        {
            this.url      = "http://ise172.ise.bgu.ac.il";
            this.username = "******";
            String PrivateKey = @"-----BEGIN RSA PRIVATE KEY-----
MIICXAIBAAKBgQCuGdcd1NEIrVWC/bjTAWQUfjhC6yJMQF/udGKvO7Yp+Dlnxbhk
1gNQrmD9ICjyEOGrKaubCJnrI0Zcjvfml3n9FhJUJeD6XrE6dSY0znUoNc8juBae
e1dy/29plyXuQOft1fL0MsckfcSWZzJ/64Cpdh515oGeUqQjiZdI6Y/vmQIDAQAB
AoGARiHipgG0suogKERMz7MfvaGayFov1seX3VbE6hIDr6Rue38KaJRNgZK9PzpV
RC3Iukpu9mTgm/f5wA9XjWw3lzGOt0qt08dK/63ESA6e1Bbe2+S3zIqn1JM8SJ6J
ZGys1YCvezEadh00Ia16dZhixVrvtaHERsUHi5dzGP0EYoECQQDHWaJRYEo10lbz
57ajQvTp/oDHGJNjd5l9L1EAQO4+VHc1fIpgYye9g/msPIX6bqpWaTSAMoR7phl5
TuVy9LxPAkEA35NfWoEDbSYw1yjezMwVm2EORYOd18EjGTRYQHSkQbXiyp2NMNKy
DHzSZao7iw38CwYVejo6VNYoyxkQJVUTlwJABjYawqJXbZniL7NWk3uwmeHeLVXs
sbq2Q5pH0dQ0GCkVlcsNnLc6M8N68gzot8be89ZPVnc8fYXNYWQ97fkGLQJAPUT9
1KeWcMsOh2hD5ovnP/WRG6u+Dep32+hkZwWQHhHiXPRgRQj4kkOCxSmpt6nVcI/y
QtTCN42ZEE+GBTUTcQJBAMafJ6ike5spiGCkx2ZzHh9IUu9H9TJ4u5KNxJiP1BIS
rxv9gh/KJgqOXc/YV3RG1FuQdflRy3ZvQutoIrznyKA=
-----END RSA PRIVATE KEY----- ";

            this.token = SimpleCtyptoLibrary.CreateToken(this.username, PrivateKey);
        }