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; } }
public T ProcessRequest <T>(YamsterHttpRequest request) { AsyncRestCall call = CreateRequestObject(request); call.ProcessThreadsafe(); byte[] bytes = call.GetResult(); return(YamsterApi.ParseJsonResponse <T>(bytes)); }
public MessagePuller(AppContext appContext) { this.appContext = appContext; yamsterArchiveDb = appContext.YamsterArchiveDb; yamsterCoreDb = appContext.YamsterCoreDb; yamsterApi = appContext.YamsterApi; HistoryLimitDays = 90; UpToDate = false; }
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); }
// 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(); } }
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; } }
public async Task <T> ProcessRequestAsync <T>(YamsterHttpRequest request) { byte[] bytes = await this.ProcessRawRequestAsync(request); return(YamsterApi.ParseJsonResponse <T>(bytes)); }