/// <summary> /// Resets this user's stream key. /// Updates channel object. /// Requires authorization. /// Requires channel_stream. /// </summary> /// <returns>The new stream key</returns> public async Task<string> ResetStreamKey() { TwitchConstants.Scope relevantScope = TwitchConstants.Scope.ChannelStream; if (authorized && authorizedScopes.Contains(relevantScope)) { Url url = new Url(TwitchConstants.baseUrl).AppendPathSegments("channels", name, "stream_key"); Uri uri = new Uri(url.ToString()); string responseString; try { responseString = await Twixel.DeleteWebData(uri, accessToken, version); } catch (TwitchException ex) { if (ex.Status == 422) { throw new TwixelException("Error resetting stream key.", ex); } else { throw new TwixelException(TwitchConstants.twitchAPIErrorString, ex); } } JObject responseObject = JObject.Parse(responseString); channel = HelperMethods.LoadChannel(responseObject, version); streamKey = (string)responseObject["stream_key"]; return streamKey; } else { if (!authorized) { throw new TwixelException(NotAuthedError()); } else if (!authorizedScopes.Contains(relevantScope)) { throw new TwixelException(MissingPermissionError(relevantScope)); } else { throw new TwixelException(TwitchConstants.unknownErrorString); } } }
/// <summary> /// Updates a channel's status. /// Updates channel object. /// Requires authorization. /// Requires Twitch partnership if setting a delay above 0. /// Requires channel_editor. /// </summary> /// <param name="status">The new status</param> /// <param name="game">The new game</param> /// <param name="delay">Delay, requires Twitch partnership if above 0</param> /// <returns> /// Returns the channel if the request succeeded. /// Throws an exception if the user is not allowed to use delay. /// </returns> public async Task<Channel> UpdateChannel(string status = "", string game = "", int delay = 0) { TwitchConstants.Scope relevantScope = TwitchConstants.Scope.ChannelEditor; if (authorized && authorizedScopes.Contains(relevantScope)) { Url url = new Url(TwitchConstants.baseUrl).AppendPathSegments("channels", name); Uri uri = new Uri(url.ToString()); JObject content = new JObject(); content["channel"] = new JObject(); content["channel"]["status"] = status; content["channel"]["game"] = game; if (version == Twixel.APIVersion.v3) { content["channel"]["delay"] = delay; } string responseString; try { responseString = await Twixel.PutWebData(uri, accessToken, content.ToString(), version); } catch (TwitchException ex) { if (ex.Status == 422) { throw new TwixelException(name + " is not allowed to use delay.", ex); } else { throw new TwixelException(TwitchConstants.twitchAPIErrorString, ex); } } channel = HelperMethods.LoadChannel(JObject.Parse(responseString), version); return channel; } else { if (!authorized) { throw new TwixelException(NotAuthedError()); } else if (!authorizedScopes.Contains(relevantScope)) { throw new TwixelException(MissingPermissionError(relevantScope)); } else { throw new TwixelException(TwitchConstants.unknownErrorString); } } }
/// <summary> /// Stream constructor, Twitch API v2 /// </summary> /// <param name="id">ID</param> /// <param name="game">Current game, can be null</param> /// <param name="viewers">Number of viewers</param> /// <param name="createdAt">Creation date</param> /// <param name="videoHeight">Video height</param> /// <param name="averageFps">Average FPS</param> /// <param name="name">Name</param> /// <param name="broadcaster">Broadcaster softare used</param> /// <param name="preview">Link to preview image</param> /// <param name="channelO">Channel JSON object</param> /// <param name="baseLinksO">Base links JSON object</param> public Stream(long? id, string game, long? viewers, string createdAt, int? videoHeight, double? averageFps, string name, string broadcaster, string preview, JObject channelO, JObject baseLinksO) : base(baseLinksO) { this.version = Twixel.APIVersion.v2; this.id = id; this.game = game; this.viewers = viewers; if (!string.IsNullOrEmpty(createdAt)) { this.createdAt = DateTime.Parse(createdAt); } this.videoHeight = videoHeight; this.averageFps = averageFps; this.name = name; this.broadcaster = broadcaster; if (!string.IsNullOrEmpty(preview)) { this.preview = new Uri(preview); } this.channel = HelperMethods.LoadChannel(channelO, version); }
/// <summary> /// Gets the channel object for this user. Also retrieves their stream key. /// Updates channel object. /// Requires authorization. /// Requires channel_read. /// </summary> /// <returns>A channel</returns> public async Task<Channel> RetrieveChannel() { TwitchConstants.Scope relevantScope = TwitchConstants.Scope.ChannelRead; if (authorized && authorizedScopes.Contains(relevantScope)) { Url url = new Url(TwitchConstants.baseUrl).AppendPathSegment("channel"); Uri uri = new Uri(url.ToString()); string responseString; try { responseString = await Twixel.GetWebData(uri, accessToken, version); } catch (TwitchException ex) { throw new TwixelException(TwitchConstants.twitchAPIErrorString, ex); } JObject responseObject = JObject.Parse(responseString); streamKey = (string)responseObject["stream_key"]; channel = HelperMethods.LoadChannel(responseObject, version); return channel; } else { if (!authorized) { throw new TwixelException(NotAuthedError()); } else if (!authorizedScopes.Contains(relevantScope)) { throw new TwixelException(MissingPermissionError(relevantScope)); } else { throw new TwixelException(TwitchConstants.unknownErrorString); } } }
/// <summary> /// Stream constructor, Twitch API v3 /// </summary> /// <param name="id">ID</param> /// <param name="game">Current game, can be null</param> /// <param name="viewers">Number of viewers</param> /// <param name="createdAt">Creation date</param> /// <param name="videoHeight">Video height</param> /// <param name="averageFps">Average FPS</param> /// <param name="name">Name</param> /// <param name="broadcaster">Broadcaster softare used</param> /// <param name="previewO">Preview JSON object</param> /// <param name="channelO">Channel JSON object</param> /// <param name="baseLinksO">Base links JSON object</param> public Stream(long? id, string game, long? viewers, string createdAt, int videoHeight, double averageFps, string name, string broadcaster, JObject previewO, JObject channelO, JObject baseLinksO) : base(baseLinksO) { this.version = Twixel.APIVersion.v3; this.id = id; this.game = game; this.viewers = viewers; this.createdAt = DateTime.Parse(createdAt); this.videoHeight = videoHeight; this.averageFps = averageFps; this.name = name; this.broadcaster = broadcaster; this.previewList = HelperMethods.LoadLinks(previewO); this.channel = HelperMethods.LoadChannel(channelO, version); }