/// <summary>
        /// Calls API Methods.
        /// </summary>
        /// <typeparam name="T">Type to which the response content will be converted.</typeparam>
        /// <param name="method">HTTPMethod (POST-GET-PUT-DELETE)</param>
        /// <param name="endpoint">Url endpoing.</param>
        /// <param name="isSigned">Specifies if the request needs a signature.</param>
        /// <param name="parameters">Request parameters.</param>
        /// <returns>returns raw JSON output as string</returns>
        public async Task <string> CallAsyncRaw(ApiMethod method, string endpoint, bool isSigned = false, string parameters = null)
        {
            var finalEndpoint = endpoint + (string.IsNullOrWhiteSpace(parameters) ? "" : $"?{parameters}");

            if (isSigned)
            {
                parameters += (!string.IsNullOrWhiteSpace(parameters) ? "&timestamp=" : "timestamp=") + Utilities.GenerateTimeStamp(DateTime.Now);
                var signature = Utilities.GenerateSignature(APISecret, parameters);
                finalEndpoint = $"{endpoint}?{parameters}&signature={signature}";
            }

            var request = new HttpRequestMessage(Utilities.CreateHttpMethod(method.ToString()), finalEndpoint);

            var response = await ConnectionClient.SendAsync(request).ConfigureAwait(false);

            response.EnsureSuccessStatusCode();

            var result = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

            return(result);
        }
Example #2
0
        protected virtual void FlushPacket(Packet packet)
        {
            if (ConnectionClient == null || !ConnectionClient.Connected)
            {
                return;
            }

            try
            {
                packet.Direction = Direction;

                EventHandler <PacketEventArgs> packetSending = PacketSending;
                if (packetSending != null)
                {
                    packetSending(this, new PacketEventArgs(Proxy, packet));
                }

                if (packet.Ignore)
                {
                    return;
                }

                byte[] buffer;

                GenericPacket gp = packet as GenericPacket;

                if (gp != null)
                {
                    buffer = gp.Data;
                }
                else
                {
                    buffer = PacketSerializer.Serialize(packet);
                }

                bool compressed = buffer.Length >= 4096 || packet.AlwaysCompress;

                if (compressed)
                {
                    buffer = ZlibStream.CompressBuffer(buffer);
                }

                int length = compressed ? -buffer.Length : buffer.Length;

                byte[] lenBuf = VLQ.CreateSigned(length);

                byte[] finalBuffer = new byte[1 + lenBuf.Length + buffer.Length];
                finalBuffer[0] = packet.PacketId;
                Buffer.BlockCopy(lenBuf, 0, finalBuffer, 1, lenBuf.Length);
                Buffer.BlockCopy(buffer, 0, finalBuffer, 1 + lenBuf.Length, buffer.Length);

                SocketAsyncEventArgs args = new SocketAsyncEventArgs();
                args.UserToken  = packet;
                args.Completed += Operation_Completed;
                args.SetBuffer(finalBuffer, 0, finalBuffer.Length);

                if (ConnectionClient != null && !ConnectionClient.SendAsync(args))
                {
                    Operation_Completed(this, args);
                }
            }
            catch
            {
                CloseAsync().Wait();
            }
        }