Example #1
0
        private static bool TryGetTwitterClient(ProxyContext ctx, string requestBody, out TwitterClient twitClient)
        {
            twitClient = null;
            if (!TryGetOwnerId(ctx.Request.RequestUri, ctx.Request.Headers, requestBody, out var ownerId))
            {
                return(false);
            }

            twitClient = TwitterClientFactory.GetInsatnce(ownerId);
            if (twitClient == null)
            {
                return(false);
            }

            return(true);
        }
Example #2
0
        private bool HostStreaming(ProxyContext ctx)
        {
            var desc = $"{ctx.Request.RequestUri}";

            Debug.WriteLine($"streaming connected : {desc}");

            if (!ctx.Request.RequestUri.AbsolutePath.Equals("/1.1/user.json", StringComparison.OrdinalIgnoreCase))
            {
                ctx.Response.StatusCode = HttpStatusCode.NotFound;
                return(true);
            }

            if (!TryGetOwnerId(ctx.Request.RequestUri, ctx.Request.Headers, null, out var ownerId))
            {
                ctx.Response.StatusCode = HttpStatusCode.Unauthorized;
                return(true);
            }

            var twitterClient = TwitterClientFactory.GetClient(ownerId);

            if (twitterClient == null)
            {
                ctx.Response.StatusCode = HttpStatusCode.Unauthorized;
                return(true);
            }

            ctx.Response.StatusCode = HttpStatusCode.OK;

            ctx.Response.Headers.Set("Content-type", "application/json; charset=utf-8");
            ctx.Response.Headers.Set("Connection", "close");
            ctx.Response.SetChunked();

            using (var sc = new StreamingConnection(new WaitableStream(ctx.Response.ResponseStream), twitterClient))
            {
                twitterClient.AddConnection(sc);

                sc.Stream.WaitHandle.WaitOne();

                twitterClient.RemoveStream(sc);
            }

            Debug.WriteLine($"streaming disconnected : {desc}");

            return(true);
        }