private static string?ExtractMessage(string response) { try { Debug.Print($"[{DateTime.UtcNow}] => RESPONSE (application/json): \"{JsonUtils.Prettify(response)}\""); var moe = Query.Deserialize <MessageOrError>(response); Debug.Print($"[{DateTime.UtcNow}] => MESSAGE: \"{moe?.Message}\""); return(moe?.Message); } catch { /* keep calm and fall through */ } return(null); }
/// <summary>Looks up the specified URL.</summary> /// <param name="mbid">The MBID for the URL to look up.</param> /// <param name="inc">Additional information to include in the result.</param> /// <returns>The requested URL.</returns> /// <exception cref="QueryException">When the web service reports an error.</exception> /// <exception cref="WebException">When something goes wrong with the web request.</exception> public IUrl LookupUrl(Guid mbid, Include inc = Include.None) { var json = this.PerformRequest("url", mbid, Query.BuildExtraText(inc)); return(Query.Deserialize <Url>(json)); }
/// <summary>Looks up the specified series.</summary> /// <param name="mbid">The MBID for the series to look up.</param> /// <param name="inc">Additional information to include in the result.</param> /// <returns>The requested series.</returns> /// <exception cref="QueryException">When the web service reports an error.</exception> /// <exception cref="WebException">When something goes wrong with the web request.</exception> public ISeries LookupSeries(Guid mbid, Include inc = Include.None) { var json = this.PerformRequest("series", mbid, Query.BuildExtraText(inc)); return(Query.Deserialize <Series>(json)); }
/// <summary>Looks up the specified disc ID.</summary> /// <param name="discid"> /// The disc ID to look up. /// When <paramref name="toc"/> is specified, this can be <c>"-"</c> to indicate that only a fuzzy TOC lookup should be done. /// </param> /// <param name="toc"> /// The TOC (table of contents) to use for a fuzzy lookup if <paramref name="discid"/> has no exact matches. /// The array should contain the first track number, last track number and the address of the disc's lead-out (in sectors), /// followed by the start address of each track (in sectors). /// </param> /// <param name="inc">Additional information to include in the result.</param> /// <param name="allMediaFormats">If true, all media formats are considered for a fuzzy lookup; otherwise, only CDs are considered.</param> /// <param name="noStubs">If true, CD stubs are not returned.</param> /// <returns>An asynchronous operation returning the result of the disc ID lookup. This can be a single disc or CD stub, or a list of matching releases.</returns> /// <exception cref="QueryException">When the web service reports an error.</exception> /// <exception cref="WebException">When something goes wrong with the web request.</exception> public async Task <IDiscIdLookupResult> LookupDiscIdAsync(string discid, int[]?toc = null, Include inc = Include.None, bool allMediaFormats = false, bool noStubs = false) { var json = await this.PerformRequestAsync("discid", discid, Query.BuildExtraText(inc, toc, allMediaFormats, noStubs)).ConfigureAwait(false); return(Query.Deserialize <DiscIdLookupResult>(json)); }
/// <summary>Looks up the specified collection.</summary> /// <param name="mbid">The MBID for the collection to look up.</param> /// <param name="inc">Additional information to include in the result.</param> /// <returns>The requested collection.</returns> /// <exception cref="QueryException">When the web service reports an error.</exception> /// <exception cref="WebException">When something goes wrong with the web request.</exception> public ICollection LookupCollection(Guid mbid, Include inc = Include.None) { var json = this.PerformRequest("collection", mbid, Query.BuildExtraText(inc)); return(Query.Deserialize <Collection>(json)); }
/// <summary>Looks up the specified work.</summary> /// <param name="mbid">The MBID for the work to look up.</param> /// <param name="inc">Additional information to include in the result.</param> /// <returns>The requested work.</returns> /// <exception cref="QueryException">When the web service reports an error.</exception> /// <exception cref="WebException">When something goes wrong with the web request.</exception> public IWork LookupWork(Guid mbid, Include inc = Include.None) { var json = this.PerformRequest("work", mbid, Query.BuildExtraText(inc)); return(Query.Deserialize <Work>(json)); }
/// <summary>Looks up the specified artist.</summary> /// <param name="mbid">The MBID for the artist to look up.</param> /// <param name="inc">Additional information to include in the result.</param> /// <param name="type"> /// The release type to filter on; applies only when <paramref name="inc"/> includes <see cref="Include.ReleaseGroups"/> and/or /// <see cref="Include.Releases"/>. /// </param> /// <param name="status"> /// The release status to filter on; applies only when <paramref name="inc"/> includes <see cref="Include.Releases"/>. /// </param> /// <returns>The requested artist.</returns> /// <exception cref="QueryException">When the web service reports an error.</exception> /// <exception cref="WebException">When something goes wrong with the web request.</exception> public IArtist LookupArtist(Guid mbid, Include inc = Include.None, ReleaseType?type = null, ReleaseStatus?status = null) { var json = this.PerformRequest("artist", mbid, Query.BuildExtraText(inc, status, type)); return(Query.Deserialize <Artist>(json)); }
/// <summary>Looks up the recordings associated with the specified ISRC value.</summary> /// <param name="isrc">The ISRC to look up.</param> /// <param name="inc">Additional information to include in the result.</param> /// <returns>The recordings associated with the requested ISRC.</returns> /// <exception cref="QueryException">When the web service reports an error.</exception> /// <exception cref="WebException">When something goes wrong with the web request.</exception> public IIsrc LookupIsrc(string isrc, Include inc = Include.None) { var json = this.PerformRequest("isrc", isrc, Query.BuildExtraText(inc)); return(Query.Deserialize <Isrc>(json)); }
/// <summary>Looks up the recordings associated with the specified ISRC value.</summary> /// <param name="isrc">The ISRC to look up.</param> /// <param name="inc">Additional information to include in the result.</param> /// <returns>An asynchronous operation returning the recordings associated with the requested ISRC.</returns> /// <exception cref="QueryException">When the web service reports an error.</exception> /// <exception cref="WebException">When something goes wrong with the web request.</exception> public async Task <IIsrc> LookupIsrcAsync(string isrc, Include inc = Include.None) { var json = await this.PerformRequestAsync("isrc", isrc, Query.BuildExtraText(inc)).ConfigureAwait(false); return(Query.Deserialize <Isrc>(json)); }
/// <summary>Looks up the specified instrument.</summary> /// <param name="mbid">The MBID for the instrument to look up.</param> /// <param name="inc">Additional information to include in the result.</param> /// <returns>The requested instrument.</returns> /// <exception cref="QueryException">When the web service reports an error.</exception> /// <exception cref="WebException">When something goes wrong with the web request.</exception> public IInstrument LookupInstrument(Guid mbid, Include inc = Include.None) { var json = this.PerformRequest("instrument", mbid, Query.BuildExtraText(inc)); return(Query.Deserialize <Instrument>(json)); }
/// <summary>Looks up the specified instrument.</summary> /// <param name="mbid">The MBID for the instrument to look up.</param> /// <param name="inc">Additional information to include in the result.</param> /// <returns>An asynchronous operation returning the requested instrument.</returns> /// <exception cref="QueryException">When the web service reports an error.</exception> /// <exception cref="WebException">When something goes wrong with the web request.</exception> public async Task <IInstrument> LookupInstrumentAsync(Guid mbid, Include inc = Include.None) { var json = await this.PerformRequestAsync("instrument", mbid, Query.BuildExtraText(inc)).ConfigureAwait(false); return(Query.Deserialize <Instrument>(json)); }
/// <summary>Looks up the specified genre.</summary> /// <param name="mbid">The MBID for the genre to look up.</param> /// <returns>An asynchronous operation returning the requested genre.</returns> /// <exception cref="QueryException">When the web service reports an error.</exception> /// <exception cref="WebException">When something goes wrong with the web request.</exception> public async Task <IGenre> LookupGenreAsync(Guid mbid) { var json = await this.PerformRequestAsync("genre", mbid, string.Empty).ConfigureAwait(false); return(Query.Deserialize <Genre>(json)); }
/// <summary>Looks up the specified area.</summary> /// <param name="mbid">The MBID for the area to look up.</param> /// <param name="inc">Additional information to include in the result.</param> /// <returns>The requested area.</returns> /// <exception cref="QueryException">When the web service reports an error.</exception> /// <exception cref="WebException">When something goes wrong with the web request.</exception> public IArea LookupArea(Guid mbid, Include inc = Include.None) { var json = this.PerformRequest("area", mbid, Query.BuildExtraText(inc)); return(Query.Deserialize <Area>(json)); }
/// <summary>Looks up the specified genre.</summary> /// <param name="mbid">The MBID for the genre to look up.</param> /// <returns>The requested genre.</returns> /// <exception cref="QueryException">When the web service reports an error.</exception> /// <exception cref="WebException">When something goes wrong with the web request.</exception> public IGenre LookupGenre(Guid mbid) { var json = this.PerformRequest("genre", mbid, string.Empty); return(Query.Deserialize <Genre>(json)); }
/// <summary>Looks up the specified URL.</summary> /// <param name="resource">The resource to look up.</param> /// <param name="inc">Additional information to include in the result.</param> /// <returns>The requested URL.</returns> /// <exception cref="QueryException">When the web service reports an error.</exception> /// <exception cref="WebException">When something goes wrong with the web request.</exception> public IUrl LookupUrl(Uri resource, Include inc = Include.None) { var json = this.PerformRequest("url", null, Query.BuildExtraText(inc, resource)); return(Query.Deserialize <Url>(json)); }
/// <summary>Looks up the specified place.</summary> /// <param name="mbid">The MBID for the place to look up.</param> /// <param name="inc">Additional information to include in the result.</param> /// <returns>The requested place.</returns> /// <exception cref="QueryException">When the web service reports an error.</exception> /// <exception cref="WebException">When something goes wrong with the web request.</exception> public IPlace LookupPlace(Guid mbid, Include inc = Include.None) { var json = this.PerformRequest("place", mbid, Query.BuildExtraText(inc)); return(Query.Deserialize <Place>(json)); }
/// <summary>Looks up the specified URL.</summary> /// <param name="resource">The resource to look up.</param> /// <param name="inc">Additional information to include in the result.</param> /// <returns>An asynchronous operation returning the requested URL.</returns> /// <exception cref="QueryException">When the web service reports an error.</exception> /// <exception cref="WebException">When something goes wrong with the web request.</exception> public async Task <IUrl> LookupUrlAsync(Uri resource, Include inc = Include.None) { var json = await this.PerformRequestAsync("url", null, Query.BuildExtraText(inc, resource)).ConfigureAwait(false); return(Query.Deserialize <Url>(json)); }
/// <summary>Looks up the specified recording.</summary> /// <param name="mbid">The MBID for the recording to look up.</param> /// <param name="inc">Additional information to include in the result.</param> /// <param name="type">The release type to filter on; applies only when <paramref name="inc"/> includes <see cref="Include.Releases"/>.</param> /// <param name="status">The release status to filter on; applies only when <paramref name="inc"/> includes <see cref="Include.Releases"/>.</param> /// <returns>The requested recording.</returns> /// <exception cref="QueryException">When the web service reports an error.</exception> /// <exception cref="WebException">When something goes wrong with the web request.</exception> public IRecording LookupRecording(Guid mbid, Include inc = Include.None, ReleaseType?type = null, ReleaseStatus?status = null) { var json = this.PerformRequest("recording", mbid, Query.BuildExtraText(inc, status, type)); return(Query.Deserialize <Recording>(json)); }
/// <summary>Looks up the specified work.</summary> /// <param name="mbid">The MBID for the work to look up.</param> /// <param name="inc">Additional information to include in the result.</param> /// <returns>An asynchronous operation returning the requested work.</returns> /// <exception cref="QueryException">When the web service reports an error.</exception> /// <exception cref="WebException">When something goes wrong with the web request.</exception> public async Task <IWork> LookupWorkAsync(Guid mbid, Include inc = Include.None) { var json = await this.PerformRequestAsync("work", mbid, Query.BuildExtraText(inc)).ConfigureAwait(false); return(Query.Deserialize <Work>(json)); }
/// <summary>Looks up the specified release.</summary> /// <param name="mbid">The MBID for the release to look up.</param> /// <param name="inc">Additional information to include in the result.</param> /// <returns>The requested release.</returns> /// <exception cref="QueryException">When the web service reports an error.</exception> /// <exception cref="WebException">When something goes wrong with the web request.</exception> public IRelease LookupRelease(Guid mbid, Include inc = Include.None) { var json = this.PerformRequest("release", mbid, Query.BuildExtraText(inc)); return(Query.Deserialize <Release>(json)); }
/// <summary>Looks up the specified artist.</summary> /// <param name="mbid">The MBID for the artist to look up.</param> /// <param name="inc">Additional information to include in the result.</param> /// <param name="type"> /// The release type to filter on; applies only when <paramref name="inc"/> includes <see cref="Include.ReleaseGroups"/> and/or /// <see cref="Include.Releases"/>. /// </param> /// <param name="status"> /// The release status to filter on; applies only when <paramref name="inc"/> includes <see cref="Include.Releases"/>. /// </param> /// <returns>An asynchronous operation returning the requested artist.</returns> /// <exception cref="QueryException">When the web service reports an error.</exception> /// <exception cref="WebException">When something goes wrong with the web request.</exception> public async Task <IArtist> LookupArtistAsync(Guid mbid, Include inc = Include.None, ReleaseType?type = null, ReleaseStatus?status = null) { var json = await this.PerformRequestAsync("artist", mbid, Query.BuildExtraText(inc, status, type)).ConfigureAwait(false); return(Query.Deserialize <Artist>(json)); }
/// <summary>Looks up the specified release group.</summary> /// <param name="mbid">The MBID for the release group to look up.</param> /// <param name="inc">Additional information to include in the result.</param> /// <param name="status">The release status to filter on; applies only when <paramref name="inc"/> includes <see cref="Include.Releases"/>.</param> /// <returns>The requested release group.</returns> /// <exception cref="QueryException">When the web service reports an error.</exception> /// <exception cref="WebException">When something goes wrong with the web request.</exception> public IReleaseGroup LookupReleaseGroup(Guid mbid, Include inc = Include.None, ReleaseStatus?status = null) { var json = this.PerformRequest("release-group", mbid, Query.BuildExtraText(inc, status)); return(Query.Deserialize <ReleaseGroup>(json)); }
/// <summary>Looks up the specified collection.</summary> /// <param name="mbid">The MBID for the collection to look up.</param> /// <param name="inc">Additional information to include in the result.</param> /// <returns>An asynchronous operation returning the requested collection.</returns> /// <exception cref="QueryException">When the web service reports an error.</exception> /// <exception cref="WebException">When something goes wrong with the web request.</exception> public async Task <ICollection> LookupCollectionAsync(Guid mbid, Include inc = Include.None) { var json = await this.PerformRequestAsync("collection", mbid, Query.BuildExtraText(inc)).ConfigureAwait(false); return(Query.Deserialize <Collection>(json)); }
/// <summary>Looks up the specified release group.</summary> /// <param name="mbid">The MBID for the release group to look up.</param> /// <param name="inc">Additional information to include in the result.</param> /// <param name="status">The release status to filter on; applies only when <paramref name="inc"/> includes <see cref="Include.Releases"/>.</param> /// <returns>An asynchronous operation returning the requested release group.</returns> /// <exception cref="QueryException">When the web service reports an error.</exception> /// <exception cref="WebException">When something goes wrong with the web request.</exception> public async Task <IReleaseGroup> LookupReleaseGroupAsync(Guid mbid, Include inc = Include.None, ReleaseStatus?status = null) { var json = await this.PerformRequestAsync("release-group", mbid, Query.BuildExtraText(inc, status)).ConfigureAwait(false); return(Query.Deserialize <ReleaseGroup>(json)); }
private static void MaybeMapException(WebException we) { var response = we.Response; if (response == null || response.ContentLength == 0) { return; } try { using var stream = response.GetResponseStream(); if (stream == null) { return; } if (response.ContentType.StartsWith("application/xml")) { Debug.Print($"[{DateTime.UtcNow}] => RESPONSE ({response.ContentType}): <{response.ContentLength} byte(s)>"); StringBuilder?sb = null; var xpath = new XPathDocument(stream).CreateNavigator().Select("/error/text"); while (xpath.MoveNext()) { if (sb == null) { sb = new StringBuilder(); } else { sb.AppendLine(); } sb.Append(xpath.Current?.InnerXml); } if (sb == null) { Debug.Print($"[{DateTime.UtcNow}] => NO ERROR TEXT FOUND"); return; } Debug.Print($"[{DateTime.UtcNow}] => ERROR: \"{sb}\""); throw new QueryException(sb.ToString(), we); } if (response.ContentType.StartsWith("application/json")) { var characterSet = "utf-8"; if (response is HttpWebResponse httpResponse) { characterSet = httpResponse.CharacterSet; if (string.IsNullOrWhiteSpace(characterSet)) { characterSet = "utf-8"; } } var enc = Encoding.GetEncoding(characterSet); using var sr = new StreamReader(stream, enc); var json = sr.ReadToEnd(); Debug.Print($"[{DateTime.UtcNow}] => RESPONSE ({response.ContentType}): \"{JsonUtils.Prettify(json)}\""); var moe = Query.Deserialize <MessageOrError>(json); if (moe?.Error == null) { Debug.Print($"[{DateTime.UtcNow}] => NO ERROR TEXT FOUND"); return; } Debug.Print($"[{DateTime.UtcNow}] => ERROR: \"{moe.Error}\" ({moe.Help})"); throw new QueryException(moe.Error, we) { HelpLink = moe.Help }; } Debug.Print($"[{DateTime.UtcNow}] => UNHANDLED ERROR RESPONSE ({response.ContentType}): <{response.ContentLength} byte(s)>"); } catch (QueryException) { throw; } catch (Exception e) { Debug.Print($"[{DateTime.UtcNow}] => FAILED TO PROCESS ERROR RESPONSE: [{e.GetType()}] {e.Message}"); /* keep calm and fall through */ } }
/// <summary>Looks up the specified disc ID.</summary> /// <param name="discid"> /// The disc ID to look up. /// When <paramref name="toc"/> is specified, this can be <c>"-"</c> to indicate that only a fuzzy TOC lookup should be done. /// </param> /// <param name="toc"> /// The TOC (table of contents) to use for a fuzzy lookup if <paramref name="discid"/> has no exact matches. /// The array should contain the first track number, last track number and the address of the disc's lead-out (in sectors), /// followed by the start address of each track (in sectors). /// </param> /// <param name="inc">Additional information to include in the result.</param> /// <param name="allMedia">If true, all media types are considered for a fuzzy lookup; otherwise, only CDs are considered.</param> /// <param name="noStubs">If true, CD stubs are not returned.</param> /// <returns>The result of the disc ID lookup. This can be a single disc or CD stub, or a list of matching releases.</returns> /// <exception cref="QueryException">When the web service reports an error.</exception> /// <exception cref="WebException">When something goes wrong with the web request.</exception> public IDiscIdLookupResult LookupDiscId(string discid, int[]?toc = null, Include inc = Include.None, bool allMedia = false, bool noStubs = false) { var json = this.PerformRequest("discid", discid, Query.BuildExtraText(inc, toc, allMedia, noStubs)); return(Query.Deserialize <DiscIdLookupResult>(json)); }