AcoustID.Web.SubmitRequest.WriteQueryString C# (CSharp) Method

WriteQueryString() public method

Write the query string to given stream writer.
public WriteQueryString ( StreamWriter writer, bool append = true, int index = -1 ) : void
writer System.IO.StreamWriter The stream writer.
append bool If true, an ampersand will be prepended.
index int The batch index.
return void
        public void WriteQueryString(StreamWriter writer, bool append = true, int index = -1)
        {
            if (Duration <= 0)
            {
                throw new Exception("Missing submit parameter: duration");
            }

            if (string.IsNullOrEmpty(Fingerprint))
            {
                throw new Exception("Missing submit parameter: fingerprint");
            }

            var batch = string.Empty;

            if (index >= 0)
            {
                batch = "." + index;
            }

            if (append)
            {
                writer.Write("&");
            }

            writer.Write("duration{0}={1}", batch, Duration);
            writer.Write("&fingerprint{0}={1}", batch, Fingerprint);

            if (Bitrate > 0)
            {
                writer.Write("&bitrate{0}={1}", batch, Bitrate);
            }

            if (!string.IsNullOrWhiteSpace(FileFormat))
            {
                writer.Write("&fileformat{0}={1}", batch, FileFormat);
            }

            if (!string.IsNullOrWhiteSpace(MBID))
            {
                writer.Write("&mbid{0}={1}", batch, MBID);
            }

            if (!string.IsNullOrWhiteSpace(Title))
            {
                writer.Write("&track{0}={1}", batch, Title);
            }

            if (!string.IsNullOrWhiteSpace(Artist))
            {
                writer.Write("&artist{0}={1}", batch, Uri.EscapeUriString(Artist));
            }

            if (!string.IsNullOrWhiteSpace(Album))
            {
                writer.Write("&album{0}={1}", batch, Uri.EscapeUriString(Album));
            }

            if (!string.IsNullOrWhiteSpace(AlbumArtist))
            {
                writer.Write("&albumartist{0}={1}", batch, Uri.EscapeUriString(AlbumArtist));
            }

            if (Year > 0)
            {
                writer.Write("&year{0}={1}", batch, Year);
            }

            if (TrackNumber > 0)
            {
                writer.Write("&trackno{0}={1}", batch, TrackNumber);
            }

            if (DiscNumber > 0)
            {
                writer.Write("&discno{0}={1}", batch, DiscNumber);
            }
        }
    }

Usage Example

示例#1
0
        public void TestWriteQueryString()
        {
            var request = new SubmitRequest("X", 200);

            request.MBID = "M";
            request.Title = "T";
            request.Artist = "A";
            request.Year = 2000;

            string expected = "&duration=200&fingerprint=X&mbid=M&track=T&artist=A&year=2000";

            using (var stream = new MemoryStream())
            using (var writer = new StreamWriter(stream))
            {
                request.WriteQueryString(writer);

                writer.Flush();

                stream.Position = 0;

                using (var reader = new StreamReader(stream))
                {
                    var actual = reader.ReadToEnd();

                    Assert.AreEqual(expected, actual);
                }
            }

            request.Year = 0;

            expected = "duration.0=200&fingerprint.0=X&mbid.0=M&track.0=T&artist.0=A";

            using (var stream = new MemoryStream())
            using (var writer = new StreamWriter(stream))
            {
                request.WriteQueryString(writer, false, 0);

                writer.Flush();

                stream.Position = 0;

                using (var reader = new StreamReader(stream))
                {
                    var actual = reader.ReadToEnd();

                    Assert.AreEqual(expected, actual);
                }
            }
        }