示例#1
0
        internal async Task <JsonMessageEnvelope> PostMessageAsync(YamsterNewMessage messageToPost)
        {
            var parameters = messageToPost.BuildParameters();

            try
            {
                byte[] result = await this.asyncRestCaller.PostFormAsync("/api/v1/messages", parameters);

                var untypedEnvelope = YamsterApi.ParseJsonResponse <JsonMessageEnvelopeUntyped>(result);
                var envelope        = ConvertArchiveMessageEnvelope(untypedEnvelope);
                return(envelope);
            }
            catch (WebException ex)
            {
                var response = (HttpWebResponse)ex.Response;
                if (response.StatusCode == HttpStatusCode.BadRequest)
                {
                    // Fish out the JSON error message from the response, e.g. something like this:
                    //   {"base":["Cannot reply to deleted message: 12345"]}
                    using (var reader = new StreamReader(ex.Response.GetResponseStream()))
                    {
                        string json = reader.ReadToEnd();
                        throw new YamsterProtocolException("POST failed:\r\n\r\n" + json);
                    }
                }
                throw;
            }
        }
示例#2
0
        public T ProcessRequest <T>(YamsterHttpRequest request)
        {
            AsyncRestCall call = CreateRequestObject(request);

            call.ProcessThreadsafe();
            byte[] bytes = call.GetResult();
            return(YamsterApi.ParseJsonResponse <T>(bytes));
        }
示例#3
0
        public MessagePuller(AppContext appContext)
        {
            this.appContext = appContext;

            yamsterArchiveDb = appContext.YamsterArchiveDb;
            yamsterCoreDb    = appContext.YamsterCoreDb;
            yamsterApi       = appContext.YamsterApi;

            HistoryLimitDays = 90;

            UpToDate = false;
        }
示例#4
0
        public AppContext()
        {
            this.ValidateOsEnvironment();

            this.foregroundThreadId = System.Threading.Thread.CurrentThread.ManagedThreadId;

            this.settings = new YamsterApiSettings(this);
            this.settings.Load();
            this.settings.Save(); // normalize settings file

            this.asyncRestCaller = new AsyncRestCaller(this);
            this.yamsterApi      = new YamsterApi(this);

            this.userManager = new LightweightUserManager(this);

            this.imageCache = new ImageCache(this.asyncRestCaller);
        }
示例#5
0
        // This is the part that is safe to execute outside the main thread
        internal void ProcessThreadsafe()
        {
            if (this.IsCompleted)
            {
                throw new InvalidOperationException("Process() was already called for this object");
            }

            try
            {
                DateTime startTime = DateTime.Now;

                using (HttpWebResponse response = (HttpWebResponse)this.HttpWebRequest.GetResponse())
                {
                    TimeSpan duration = DateTime.Now - startTime;

                    Debug.WriteLine("Status: {0}, received {1} bytes, from cache: {2:l}, {3:N2}ms",
                                    response.StatusCode, response.ContentLength, response.IsFromCache.ToString().ToLower(), duration.TotalMilliseconds);

                    using (var reader = new BinaryReader(response.GetResponseStream()))
                    {
                        this.ResultBytes = AsyncRestCall.ReadAllBytes(reader);
                    }
                }
            }
            catch (WebException ex)
            {
                try
                {
                    YamsterApi.CheckForErrors(ex);
                    this.ResultException = ex; // if Handle() didn't throw, then keep the original exception
                }
                catch (Exception ex2)
                {
                    this.ResultException = ex2;
                }
            }
            Debug.Assert(this.IsCompleted);

            // Signal that the operation is complete
            if (Semaphore != null)
            {
                Semaphore.Release();
            }
        }
示例#6
0
        internal async Task SetMessageLikeStatusAsync(long messageId, bool liked)
        {
            // TODO: Need to schedule this request rather than executing it immediately
            TallyRequest();

            var parameters = new NameValueCollection();

            parameters["message_id"] = messageId.ToString();

            try
            {
                if (liked)
                {
                    await this.asyncRestCaller.PostFormAsync("/api/v1/messages/liked_by/current.json", parameters,
                                                             YamsterHttpMethod.Post);
                }
                else
                {
                    parameters["_method"] = "DELETE";
                    await this.asyncRestCaller.PostFormAsync("/api/v1/messages/liked_by/current.json", parameters,
                                                             YamsterHttpMethod.Delete);
                }
            }
            catch (WebException ex)
            {
                try
                {
                    YamsterApi.CheckForErrors(ex);
                }
                catch (RateLimitExceededException)
                {
                    NotifyRateLimitExceeded();
                    throw;
                }
                throw;
            }
        }
示例#7
0
        public async Task <T> ProcessRequestAsync <T>(YamsterHttpRequest request)
        {
            byte[] bytes = await this.ProcessRawRequestAsync(request);

            return(YamsterApi.ParseJsonResponse <T>(bytes));
        }