public async Task PostLike(string modelId, string sketchFabToken, TokenType tokenType)
        {
            try
            {
                _logger.LogInformation($"Like model {modelId}");

                HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, $"{SketchfabApiUrl}/me/likes");
                httpRequestMessage.AddAuthorizationHeader(sketchFabToken, tokenType);

                using var form = new MultipartFormDataContent();
                form.Headers.ContentType.MediaType = "multipart/form-data";

                form.Add(new StringContent(modelId), "model");

                httpRequestMessage.Content = form;

                var httpClient = _httpClientFactory.CreateClient();
                var response   = await httpClient.SendAsync(httpRequestMessage);

                _logger.LogInformation($"{nameof(PostLike)} responded {response.StatusCode}");
                response.EnsureSuccessStatusCode();
            }
            catch (Exception ex)
            {
                _logger.LogError($"Sketchfab Like error: {ex.Message}");
                throw;
            }
        }
Beispiel #2
0
        public async Task <List <Collection> > GetMyCollectionsAsync(string sketchFabToken, TokenType tokenType)
        {
            try
            {
                _logger.LogInformation($"Get collections");

                HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, $"{SketchfabApiUrl}/me/collections");
                httpRequestMessage.AddAuthorizationHeader(sketchFabToken, tokenType);

                var httpClient = _httpClientFactory.CreateClient();
                var response   = await httpClient.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseContentRead);

                _logger.LogInformation($"{nameof(GetMyCollectionsAsync)} responded {response.StatusCode}");
                response.EnsureSuccessStatusCode();

                var collectionsJson = await response.Content.ReadAsStringAsync();

                var collections = JsonConvert.DeserializeObject <PagedResult <Collection> >(collectionsJson);

                _logger.LogInformation($"GetMyCollectionsAsync got {collections.results.Count} collection(s).");

                return(collections.results);
            }
            catch (Exception ex)
            {
                _logger.LogError($"Sketchfab upload error: {ex.Message}");
                throw;
            }
        }
        public async Task <Account> GetMyAccountAsync(string sketchFabToken, TokenType tokenType)
        {
            try
            {
                _logger.LogInformation($"Get Account");

                HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, $"{SketchfabApiUrl}/me/account");
                httpRequestMessage.AddAuthorizationHeader(sketchFabToken, tokenType);

                var httpClient = _httpClientFactory.CreateClient();
                var response   = await httpClient.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseContentRead);

                _logger.LogInformation($"{nameof(GetMyAccountAsync)} responded {response.StatusCode}");
                response.EnsureSuccessStatusCode();

                var jsonPayload = await response.Content.ReadAsStringAsync();

                var account = JsonConvert.DeserializeObject <Account>(jsonPayload);

                return(account);
            }
            catch (Exception ex)
            {
                _logger.LogError($"Sketchfab get account error: {ex.Message}");
                throw;
            }
        }
        public async Task UpdateModelAsync(string modelId, UpdateModelRequest request, string sketchFabToken, TokenType tokenType)
        {
            try
            {
                _logger.LogInformation($"Updating model [{modelId}].");
                if (string.IsNullOrWhiteSpace(modelId))
                {
                    throw new ArgumentNullException(nameof(modelId));
                }

                HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Patch, $"{SketchfabApiUrl}/models/{modelId}");
                httpRequestMessage.AddAuthorizationHeader(sketchFabToken, tokenType);

                using var form = new MultipartFormDataContent();
                form.Headers.ContentType.MediaType = "multipart/form-data";

                AddCommonModelFields(form, request);

                httpRequestMessage.Content = form;

                var httpClient = _httpClientFactory.CreateClient();
                var response   = await httpClient.SendAsync(httpRequestMessage);

                _logger.LogInformation($"{nameof(UpdateModelAsync)} responded {response.StatusCode}");
                response.EnsureSuccessStatusCode();
            }
            catch (Exception ex)
            {
                _logger.LogError($"Sketchfab update error: {ex.Message}");
                throw;
            }
        }
        public async IAsyncEnumerable <Model> GetMyModelsAsync(string sketchFabToken, TokenType tokenType)
        {
            _logger.LogInformation($"Get my models");

            HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, $"{SketchfabApiUrl}/me/models");

            httpRequestMessage.AddAuthorizationHeader(sketchFabToken, tokenType);

            await foreach (var result in GetPagedResultAsync <Model>(httpRequestMessage))
            {
                yield return(result);
            }
        }
        public async Task <Model> GetModelAsync(string modelId, string sketchFabToken, TokenType tokenType)
        {
            try
            {
                _logger.LogInformation($"Get model");

                if (string.IsNullOrWhiteSpace(modelId))
                {
                    throw new ArgumentNullException(nameof(modelId));
                }

                HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, $"{SketchfabApiUrl}/models/{modelId}");
                httpRequestMessage.Headers.CacheControl = new CacheControlHeaderValue()
                {
                    NoCache = true
                };
                httpRequestMessage.AddAuthorizationHeader(sketchFabToken, tokenType);

                var httpClient = _httpClientFactory.CreateClient();

                var response = await httpClient.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseContentRead);

                _logger.LogInformation($"{nameof(GetModelAsync)} responded {response.StatusCode}");
                response.EnsureSuccessStatusCode();

                var json = await response.Content.ReadAsStringAsync();

                var model = JsonConvert.DeserializeObject <Model>(json);

                _logger.LogInformation($"GetModelAsync OK");

                return(model);
            }
            catch (Exception ex)
            {
                _logger.LogError($"Sketchfab GetModelAsync error: {ex.Message}");
                throw;
            }
        }
Beispiel #7
0
        public async Task AddModelToCollectionAsync(string collectionId, string sketchFabToken, TokenType tokenType, params string[] modelIds)
        {
            try
            {
                _logger.LogInformation($"Add model(s) to collection");
                if (modelIds == null || modelIds.Length == 0)
                {
                    throw new ArgumentNullException(nameof(modelIds));
                }
                if (string.IsNullOrWhiteSpace(collectionId))
                {
                    throw new ArgumentNullException(nameof(collectionId));
                }

                HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, $"{SketchfabApiUrl}/collections/{collectionId}/models");
                httpRequestMessage.AddAuthorizationHeader(sketchFabToken, tokenType);

                using var form = new MultipartFormDataContent();
                form.Headers.ContentType.MediaType = "multipart/form-data";

                form.AddRange(modelIds, "models");

                httpRequestMessage.Content = form;

                var httpClient = _httpClientFactory.CreateClient();
                var response   = await httpClient.SendAsync(httpRequestMessage);

                _logger.LogInformation($"{nameof(AddModelToCollectionAsync)} responded {response.StatusCode}");
                response.EnsureSuccessStatusCode();
            }
            catch (Exception ex)
            {
                _logger.LogError($"Sketchfab add model(s) to collection error: {ex.Message}");
                throw;
            }
        }
        public async Task <SketchfabUploadResponse> UploadModelAsync(UploadModelRequest request, string sketchFabToken)
        {
            SketchfabUploadResponse sfResponse = new SketchfabUploadResponse();

            try
            {
                _logger.LogInformation($"Uploading model [{request.FilePath}].");
                if (string.IsNullOrWhiteSpace(request.FilePath))
                {
                    throw new ArgumentNullException(nameof(request.FilePath));
                }

                if (!File.Exists(request.FilePath))
                {
                    throw new FileNotFoundException($"File [{request.FilePath}] not found.");
                }
                HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, $"{SketchfabApiUrl}/models");
                httpRequestMessage.AddAuthorizationHeader(sketchFabToken, request.TokenType);
                using var form                  = new MultipartFormDataContent();
                using var fileContent           = new ByteArrayContent(await File.ReadAllBytesAsync(request.FilePath));
                fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/form-data");
                form.Add(fileContent, "modelFile", Path.GetFileName(request.FilePath));
                if (!string.IsNullOrWhiteSpace(request.Source))
                {
                    form.Add(new StringContent(request.Source), "source");
                }
                else
                {
                    _logger.LogWarning("Sketchfab upload has no source configured. It's better to set one to uniquely identify all the models generated by the exporter, see https://Sketchfab.com/developers/guidelines#source");
                }

                AddCommonModelFields(form, request);

                httpRequestMessage.Content = form;


                var httpClient = _httpClientFactory.CreateClient();
                var response   = await httpClient.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseContentRead);

                _logger.LogInformation($"{nameof(UploadModelAsync)} responded {response.StatusCode}");

                if (response.IsSuccessStatusCode)
                {
                    var uuid = response.Headers.GetValues("Location").FirstOrDefault();
                    sfResponse.ModelId    = uuid;
                    sfResponse.StatusCode = response.StatusCode;
                    sfResponse.Message    = response.ReasonPhrase;
                    request.ModelId       = uuid;
                    _logger.LogInformation("Uploading is complete. Model uuid is " + uuid);
                }
                else
                {
                    _logger.LogError($"Error in Sketchfab upload: {response.StatusCode} {response.ReasonPhrase}");
                    sfResponse.StatusCode = response.StatusCode;
                    sfResponse.Message    = response.ReasonPhrase;
                }

                return(sfResponse);
            }
            catch (Exception ex)
            {
                _logger.LogError($"Sketchfab upload error: {ex.Message}");
                throw;
            }
        }