Пример #1
0
        public async Task SendTrigger(string eventName, Dictionary <string, string> values)
        {
            try
            {
                using (AdvancedHttpClient client = new AdvancedHttpClient())
                {
                    JObject jobj = new JObject();
                    foreach (var kvp in values)
                    {
                        jobj[kvp.Key] = kvp.Value;
                    }
                    HttpContent content = new StringContent(JSONSerializerHelper.SerializeToString(jobj), Encoding.UTF8, "application/json");

                    HttpResponseMessage response = await client.PostAsync(string.Format(WebHookURLFormat, eventName, this.token.accessToken), content);

                    if (!response.IsSuccessStatusCode)
                    {
                        Logger.Log(await response.Content.ReadAsStringAsync());
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Log(ex);
            }
        }
Пример #2
0
        public static async Task PostAsync(string path, object body)
        {
            AdvancedHttpClient client = new AdvancedHttpClient
            {
                BaseAddress = new Uri(BaseUri)
            };

            var         json    = JsonConvert.SerializeObject(body);
            HttpContent content = new StringContent(json, UnicodeEncoding.UTF8, "application/json");
            await client.PostAsync(path, content);
        }
 /// <summary>
 /// Performs a POST REST request using the provided request URI.
 /// </summary>
 /// <param name="requestUri">The request URI to use</param>
 /// <param name="content">The content to send</param>
 /// <param name="autoRefreshToken">Whether to auto refresh the OAuth token</param>
 /// <returns>A type-casted object of the contents of the response</returns>
 protected async Task <T> PostAsync <T>(string requestUri, HttpContent content, bool autoRefreshToken = true)
 {
     using (AdvancedHttpClient client = await this.GetHttpClient(autoRefreshToken))
     {
         try
         {
             client.RateLimitUpdateOccurred += Client_RateLimitUpdateOccurred;
             return(await client.PostAsync <T>(requestUri, content));
         }
         finally
         {
             client.RateLimitUpdateOccurred -= Client_RateLimitUpdateOccurred;
         }
     }
 }
        /// <summary>
        /// Creates a TwitchConnection object from an app access token.
        /// </summary>
        /// <param name="clientID">The ID of the client application</param>
        /// <param name="clientSecret">The secret of the client application</param>
        /// <returns>The TwitchConnection object</returns>
        public static async Task <TwitchConnection> ConnectViaAppAccess(string clientID, string clientSecret)
        {
            Validator.ValidateString(clientID, "clientID");
            Validator.ValidateString(clientSecret, "clientSecret");

            OAuthTokenModel token = null;

            using (AdvancedHttpClient client = new AdvancedHttpClient())
            {
                token = await client.PostAsync <OAuthTokenModel>(await TwitchConnection.GetTokenURLForAppAccess(clientID, clientSecret));
            }

            if (token == null)
            {
                throw new InvalidOperationException("OAuth token was not acquired");
            }
            token.clientID = clientID;
            return(new TwitchConnection(token));
        }
Пример #5
0
        public static async Task <T> PostAsync <T>(string path, object body)
        {
            AdvancedHttpClient client = new AdvancedHttpClient
            {
                BaseAddress = new Uri(BaseUri)
            };

            var         json    = JsonConvert.SerializeObject(body);
            HttpContent content = new StringContent(json, UnicodeEncoding.UTF8, "application/json");

            HttpResponseMessage message = await client.PostAsync(path, content);

            if (message.IsSuccessStatusCode)
            {
                return(JsonConvert.DeserializeObject <T>(await message.Content.ReadAsStringAsync()));
            }

            return(default(T));
        }
        /// <summary>
        /// Sends a broadcast to PubSub for the extension.
        /// </summary>
        /// <param name="clientID">The client ID of the extension</param>
        /// <param name="clientSecret">The client secret of the extension</param>
        /// <param name="ownerID">The owner user ID of the extension</param>
        /// <param name="channelID">The channel ID to broadcast to</param>
        /// <param name="data">The data to broadcast</param>
        /// <returns>An awaitable task</returns>
        /// <exception cref="HttpRestRequestException">Throw in the event of a failed request</exception>
        public static async Task SendBroadcast(string clientID, string clientSecret, string ownerID, string channelID, object data)
        {
            Validator.ValidateString(clientID, "clientID");
            Validator.ValidateString(clientSecret, "clientSecret");
            Validator.ValidateString(ownerID, "ownerID");
            Validator.ValidateString(channelID, "channelID");
            Validator.ValidateVariable(data, "data");

            using (AdvancedHttpClient client = TwitchExtensionService.GetHttpClient(clientID, clientSecret, ownerID, channelID))
            {
                HttpResponseMessage response = await client.PostAsync("https://api.twitch.tv/extensions/message/" + channelID,
                                                                      AdvancedHttpClient.CreateContentFromString(JSONSerializerHelper.SerializeToString(new BroadcastBodyModel(data))));

                if (!response.IsSuccessStatusCode)
                {
                    throw new HttpRestRequestException(response);
                }
            }
        }
Пример #7
0
        private async Task PostAsync(string endpoint, object data, bool logException = true)
        {
            try
            {
                using (AdvancedHttpClient client = new AdvancedHttpClient(MixItUpAPIEndpoint))
                {
                    string content = JSONSerializerHelper.SerializeToString(data);
                    HttpResponseMessage response = await client.PostAsync(endpoint, new StringContent(content, Encoding.UTF8, "application/json"));

                    await this.ProcessResponseIfError(response);
                }
            }
            catch (Exception ex)
            {
                if (logException)
                {
                    Logger.Log(ex);
                }
            }
        }
        /// <summary>
        /// Sends a chat message to the specified channel from the extension.
        /// </summary>
        /// <param name="clientID">The client ID of the extension</param>
        /// <param name="clientSecret">The client secret of the extension</param>
        /// <param name="ownerID">The owner user ID of the extension</param>
        /// <param name="version">The version of the extension</param>
        /// <param name="channelID">The channel ID to broadcast to</param>
        /// <param name="message">The message to send</param>
        /// <returns>An awaitable task</returns>
        /// <exception cref="HttpRestRequestException">Throw in the event of a failed request</exception>
        public static async Task SendChatMessage(string clientID, string clientSecret, string ownerID, string version, string channelID, string message)
        {
            Validator.ValidateString(clientID, "clientID");
            Validator.ValidateString(clientSecret, "clientSecret");
            Validator.ValidateString(ownerID, "ownerID");
            Validator.ValidateString(version, "version");
            Validator.ValidateString(channelID, "channelID");
            Validator.ValidateString(message, "message");

            using (AdvancedHttpClient client = TwitchExtensionService.GetHttpClient(clientID, clientSecret, ownerID, channelID))
            {
                HttpResponseMessage response = await client.PostAsync($"https://api.twitch.tv/extensions/{clientID}/{version}/channels/{channelID}/chat",
                                                                      AdvancedHttpClient.CreateContentFromString(JSONSerializerHelper.SerializeToString(new ExtensionChatMessageModel(message))));

                if (!response.IsSuccessStatusCode)
                {
                    throw new HttpRestRequestException(response);
                }
            }
        }
Пример #9
0
        protected async Task <OAuthTokenModel> GetWWWFormUrlEncodedOAuthToken(string endpoint, string clientID, string clientSecret, List <KeyValuePair <string, string> > bodyContent)
        {
            try
            {
                using (AdvancedHttpClient client = new AdvancedHttpClient())
                {
                    client.SetBasicClientIDClientSecretAuthorizationHeader(clientID, clientSecret);
                    using (var content = new FormUrlEncodedContent(bodyContent))
                    {
                        content.Headers.Clear();
                        content.Headers.Add("Content-Type", "application/x-www-form-urlencoded");

                        HttpResponseMessage response = await client.PostAsync(endpoint, content);

                        return(await response.ProcessResponse <OAuthTokenModel>());
                    }
                }
            }
            catch (Exception ex) { Logger.Log(ex); }
            return(null);
        }
Пример #10
0
        private async Task <T> PostAsyncWithResult <T>(string endpoint, object data)
        {
            try
            {
                using (AdvancedHttpClient client = new AdvancedHttpClient(MixItUpAPIEndpoint))
                {
                    string content = JSONSerializerHelper.SerializeToString(data);
                    HttpResponseMessage response = await client.PostAsync(endpoint, new StringContent(content, Encoding.UTF8, "application/json"));

                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        string resultContent = await response.Content.ReadAsStringAsync();

                        return(JSONSerializerHelper.DeserializeFromString <T>(resultContent));
                    }
                    else
                    {
                        await this.ProcessResponseIfError(response);
                    }
                }
            }
            catch (Exception ex) { Logger.Log(ex); }
            return(default(T));
        }