Esempio n. 1
0
            public static async Task <GfycatCreateResponse> Create(GfycatCreateRequest request)
            {
                var response = await client.PostAsJsonAsync("https://api.gfycat.com/v1/gfycats", request);

                response.EnsureSuccessStatusCode();

                return(await response.Content.ReadAsAsync <GfycatCreateResponse>());
            }
Esempio n. 2
0
        public async Task <String> Publish(Stream fileStream, IProgress <GfycatProgress> reporter)
        {
            // Using noMd5 here to skip gfycat's dupe check.
            // In the case of a dupe being detected, the status request will return back
            // the details about the dupe which is not currently handled.
            //
            // In real code, you can extend this to include things like titles and descriptions.
            // See api docs for more details.
            var createRequest = new GfycatCreateRequest()
            {
                noMd5 = true,
            };

            // Create the gfycat
            reporter.Report(new GfycatProgress("Creating gfycat"));
            var createResponse = await Requests.Create(createRequest);

            if (!createResponse.IsOk)
            {
                throw new InvalidOperationException("Something went wrong, send help.");
            }

            // Upload the file
            reporter.Report(new GfycatProgress("Uploading file"));
            Requests.Upload(createResponse.GfyName, fileStream);

            while (true)
            {
                var statusResponse = await Requests.Status(createResponse.GfyName);

                switch (statusResponse.Task)
                {
                case "NotFoundo":
                    // This happening once is expected because there is a gap between
                    // when the file is uploaded, and when gfycat detects it.
                    //
                    // In real code, publish should fail if this happens too many times.
                    await Task.Delay(1000);

                    break;

                case "encoding":
                    reporter.Report(new GfycatProgress("Encoding", statusResponse.Progress));
                    await Task.Delay(1000);

                    break;

                case "complete":
                    reporter.Report(new GfycatProgress("Complete"));
                    return(statusResponse.GfyName);

                case "error":
                default:
                    throw new InvalidOperationException("Something went wrong, send help.");
                }
            }
        }