예제 #1
0
        public async Task <JsonMessageEnvelope> GetMessagesInFeedAsync(long feedId, long?olderThan = null)
        {
            string restPath;

            if (feedId == YamsterGroup.AllCompanyGroupId)
            {
                restPath = "/api/v1/messages/general.json";
            }
            else if (feedId == YamsterGroup.ConversationsGroupId)
            {
                restPath = "/api/v1/messages/private.json";
            }
            else if (feedId == YamsterGroup.InboxFeedId)
            {
                restPath = "/api/v1/messages/inbox.json";
            }
            else
            {
                restPath = string.Format("/api/v1/messages/in_group/{0}.json", feedId);
            }

            var request = new YamsterHttpRequest(restPath);

            request.Parameters["threaded"] = "extended";
            if (olderThan != null)
            {
                request.Parameters["older_than"] = olderThan.Value.ToString();
            }

            TallyRequest();
            return(await GetMessageEnvelopeJsonAsync(request));
        }
예제 #2
0
        private async Task <JsonMessageEnvelope> GetMessageEnvelopeJsonAsync(YamsterHttpRequest request)
        {
            JsonMessageEnvelopeUntyped untypedEnvelope = await this.asyncRestCaller
                                                         .ProcessRequestAsync <JsonMessageEnvelopeUntyped>(request);

            return(ConvertArchiveMessageEnvelope(untypedEnvelope));
        }
예제 #3
0
        AsyncRestCall CreateRequestObject(YamsterHttpRequest request)
        {
            string methodString = request.Method.ToString().ToUpperInvariant();

            // Transform the URL, given the oauth settings
            // This should probably be a no-op
            var urlWithQuerystring = getFinalUrl(request);

            Debug.WriteLine("AsyncRestCaller: Starting request {0}: {1}", request.Method,
                            new Uri(urlWithQuerystring).GetLeftPart(UriPartial.Path));

            var webRequest = (HttpWebRequest)HttpWebRequest.Create(urlWithQuerystring);

            webRequest.Method      = methodString;
            webRequest.UserAgent   = userAgent;
            webRequest.CachePolicy = new RequestCachePolicy(RequestCacheLevel.Revalidate);

            // Modify the request, given the oauth settings
            if (request.AddAuthorizationHeader)
            {
                string token = this.appContext.Settings.OAuthToken;
                webRequest.Headers.Add("Authorization", string.Format("Bearer {0}", token));
            }

            setBody(webRequest, request);

            return(new AsyncRestCall(webRequest));
        }
예제 #4
0
        public T ProcessRequest <T>(YamsterHttpRequest request)
        {
            AsyncRestCall call = CreateRequestObject(request);

            call.ProcessThreadsafe();
            byte[] bytes = call.GetResult();
            return(YamsterApi.ParseJsonResponse <T>(bytes));
        }
예제 #5
0
        public IList <JsonSearchedGroup> SearchForGroups(string keyword, int maxResults)
        {
            var url = this.settings.YammerServiceUrl + "/api/v1/autocomplete/ranked";

            var request = new YamsterHttpRequest(url);

            request.Parameters["prefix"] = keyword;
            request.Parameters["models"] = "group:" + maxResults.ToString();

            JsonAutoCompleteResult result = this.asyncRestCaller.ProcessRequest <JsonAutoCompleteResult>(request);

            return(result.Groups);
        }
예제 #6
0
        public async Task <JsonMessageEnvelope> GetMessagesInThreadAsync(long threadId, long?olderThan = null)
        {
            string restPath = string.Format("/api/v1/messages/in_thread/{0}.json", threadId);

            var request = new YamsterHttpRequest(restPath);

            if (olderThan != null)
            {
                request.Parameters["older_than"] = olderThan.Value.ToString();
            }

            TallyRequest();

            return(await GetMessageEnvelopeJsonAsync(request));
        }
예제 #7
0
        private void setBody(HttpWebRequest webRequest, YamsterHttpRequest request)
        {
            var parameters = request.Parameters;

            if (parameters == null || !sendParamsAsBody(request.Method))
            {
                return;
            }

            var serializedContent = buildBody(parameters);

            webRequest.ContentType   = "application/json";
            webRequest.ContentLength = serializedContent.Length;

            var stream = webRequest.GetRequestStream();

            stream.Write(serializedContent, 0, serializedContent.Length);
        }
예제 #8
0
        public async Task <byte[]> ProcessRawRequestAsync(YamsterHttpRequest request)
        {
            AsyncRestCall asyncRestCall = CreateRequestObject(request);

            asyncRestCall.Semaphore = new SemaphoreSlim(initialCount: 0, maxCount: 1);
            this.EnsureBackgroundThreadStarted();
            lock (this.lockObject)
            {
                RequestQueue.AddLast(asyncRestCall);
                backgroundThreadEvent.Set();
            }

            await asyncRestCall.Semaphore.WaitAsync();

            asyncRestCall.Semaphore.Dispose();

            byte[] bytes = asyncRestCall.GetResult();
            return(bytes);
        }
예제 #9
0
        public JsonUserReference LoginWith(string accessToken)
        {
            if (accessToken == null)
            {
                throw new ArgumentException("Missing access token");
            }

            // Assign the new accessToken, which we will for ProcessRequest() below
            UpdateOAuth(accessToken);

            var request = new YamsterHttpRequest("/api/v1/users/current.json");

            request.Parameters.Add("include_groups", "false");
            JsonUserReference user = this.appContext.AsyncRestCaller.ProcessRequest <JsonUserReference>(request);

            if (LoginSuccess != null)
            {
                LoginSuccess(this, EventArgs.Empty);
            }

            return(user);
        }
예제 #10
0
        public Pixbuf TryGetImageResized(YamsterHttpRequest request, Size resizeDimensions, out Exception loadError)
        {
            CachedImageKey key = new CachedImageKey(request.Url, resizeDimensions);

            CachedImage   cachedImage = null;
            Task <byte[]> requestTask;

            if (!imagesByUrl.TryGetValue(key, out cachedImage))
            {
                // Check hack to prevent the cache from growing too large
                if (imagesByUrl.Count > MaxCacheItems)
                {
                    imagesByUrl.Clear();
                }

                requestTask = this.asyncRestCaller.ProcessRawRequestAsync(request);

                cachedImage = new CachedImage(requestTask, request.Url, resizeDimensions);
                imagesByUrl.Add(key, cachedImage);
            }
            else
            {
                requestTask = cachedImage.RequestTask;
            }

            if (requestTask != null)
            {
                if (requestTask.IsCanceled || requestTask.IsCompleted || requestTask.IsFaulted)
                {
                    FinishLoadingImage(cachedImage);
                }
            }

            loadError = cachedImage.LoadError;
            return(cachedImage.Pixbuf);
        }
예제 #11
0
        public void TestServerConnection()
        {
            var request = new YamsterHttpRequest("/api/v1/messages.json");

            this.asyncRestCaller.ProcessRequest <JsonMessageEnvelope>(request);
        }
예제 #12
0
 public Pixbuf TryGetImage(YamsterHttpRequest request, out Exception loadError)
 {
     return(this.TryGetImageResized(request, new Size(), out loadError));
 }
예제 #13
0
        private string getFinalUrl(YamsterHttpRequest request)
        {
            string part = buildAbsoluteUrl(request.Url);

            return(!sendParamsAsBody(request.Method) ? finishUrl(part, request.Parameters) : part);
        }
예제 #14
0
        public async Task <T> ProcessRequestAsync <T>(YamsterHttpRequest request)
        {
            byte[] bytes = await this.ProcessRawRequestAsync(request);

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