public async Task <List <Message> > FetchMessagesAsync(IMessageProviderContext context)
        {
            if (RepositoryName == null || RepositoryOwner == null)
            {
                throw new InvalidOperationException("Both repository name and repository owner must be set in order to fetch messages.");
            }

            HttpClient     client   = context.AppHttpClient.HttpClient;
            Uri            baseUri  = new Uri(GITHUB_API_URI + RepositoryOwner + "/" + RepositoryName);
            List <Message> messages = new List <Message>();

            //List<Message> commits = await FetchCommits(client, uri);
            //Uri commitsUri = new Uri(uri.ToString() + "/commits");
            Uri commitsUri = new Uri(baseUri + "/commits");

            //Prepare request message
            HttpRequestMessage commitsRequest = new HttpRequestMessage(HttpMethod.Get, commitsUri);

            commitsRequest.Headers.Add("Accept", "*/*");
            commitsRequest.Headers.Add("User-Agent", "curl/7.46.0");

            //Send the GET request asynchronously and retrieve the response as a string.
            HttpResponseMessage commitsHttpResponse = new HttpResponseMessage();
            string httpResponseBody = "";

            try
            {
                //Send the GET request
                commitsHttpResponse = await client.SendRequestAsync(commitsRequest);

                commitsHttpResponse.EnsureSuccessStatusCode();
                httpResponseBody = await commitsHttpResponse.Content.ReadAsStringAsync();
            }
            catch (Exception ex)
            {
                httpResponseBody = "Error: " + ex.HResult.ToString("X") + " Message: " + ex.Message;
            }

            System.Diagnostics.Debug.WriteLine(httpResponseBody);

            // Parse Json Array
            JsonArray jsonResponse = parseJsonArray(httpResponseBody);

            // Parse JsonValues to JsonStrings
            JsonObject[] commitsArray = convertJsonArrayToArrayOfJsonObjects(jsonResponse);

            // Create commits Messages
            foreach (JsonObject commitObject in commitsArray)
            {
                messages.Add(createCommitMessage(commitObject));
            }

            return(messages);
        }
Пример #2
0
        public async Task <List <Message> > FetchMessagesAsync(IMessageProviderContext context)
        {
            if (AuthToken == null)
            {
                throw new InvalidOperationException("Set authorization token in order to fetch Slack messages.");
            }

            HttpClient     client   = context.AppHttpClient.HttpClient;
            Uri            baseUri  = new Uri(SLACK_API_URL);
            List <Message> messages = new List <Message>();

            //List<Message> commits = await FetchCommits(client, uri);
            //Uri commitsUri = new Uri(uri.ToString() + "/commits");
            Uri slackMessagesUri = new Uri(baseUri + "channels.history?token=" + AuthToken + "&channel=" + Channel);

            //Prepare request message
            HttpRequestMessage slackMessagesRequest = new HttpRequestMessage(HttpMethod.Get, slackMessagesUri);

            slackMessagesRequest.Headers.Add("Accept", "*/*");
            slackMessagesRequest.Headers.Add("User-Agent", "curl/7.46.0");

            //Send the GET request asynchronously and retrieve the response as a string.
            HttpResponseMessage slackMessagesResponse = new HttpResponseMessage();
            string httpResponseBody = "";

            try
            {
                //Send the GET request
                slackMessagesResponse = await client.SendRequestAsync(slackMessagesRequest);

                slackMessagesResponse.EnsureSuccessStatusCode();
                httpResponseBody = await slackMessagesResponse.Content.ReadAsStringAsync();
            }
            catch (Exception ex)
            {
                httpResponseBody = "Error: " + ex.HResult.ToString("X") + " Message: " + ex.Message;
                System.Diagnostics.Debug.WriteLine(httpResponseBody);
                return(null);
            }

            System.Diagnostics.Debug.WriteLine(httpResponseBody);

            // Parse response JSON
            JsonObject jsonResponseObject;

            JsonObject.TryParse(httpResponseBody, out jsonResponseObject);
            IJsonValue slackMessagesArrayValue;

            jsonResponseObject.TryGetValue("messages", out slackMessagesArrayValue);

            JsonArray slackMessagesArray;

            JsonArray.TryParse(slackMessagesArrayValue.ToString(), out slackMessagesArray);

            // Parse JsonValues to JsonStrings
            JsonObject[] slackMessagesObjectsArray = convertJsonArrayToArrayOfJsonObjects(slackMessagesArray);

            // Create actual Messages
            foreach (JsonObject slackMessageObject in slackMessagesObjectsArray)
            {
                messages.Add(createSlackMessage(slackMessageObject));
            }

            return(messages);
        }
 public MessagesContainer(IMessageProviderContext messageProviderContext)
 {
     this.MessageProviderContext = messageProviderContext;
     this.Messages = new ObservableCollection <Message>();
 }
 public MessageProviderManager(IMessageProviderContext context)
 {
     this.Context = context;
 }