예제 #1
0
        private async Task <RerumManifestResponse> MakeRequest(WebManifest manifest, string method)
        {
            var rt = new RerumManifestResponse();

            using (var client = new HttpClient())
            {
                client.DefaultRequestHeaders
                .Add("Accept", new[] { Constants.MediaTypes.ApplicationJSon, Constants.MediaTypes.ApplicationJSonLD });

                var authToken = await _tokenManager.GetTokenAsync();

                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authToken.AccessToken);
                var jsonSettings = new JsonSerializerSettings
                {
                    ContractResolver = new DefaultContractResolver
                    {
                        NamingStrategy = new CamelCaseNamingStrategy()
                    },
                    NullValueHandling = NullValueHandling.Ignore
                };

                var json    = JsonConvert.SerializeObject(manifest, jsonSettings);
                var content = new StringContent(json, Encoding.UTF8, Constants.MediaTypes.ApplicationJSon);
                HttpResponseMessage response = null;
                string url = null;
                if (method.Equals("post", StringComparison.InvariantCultureIgnoreCase))
                {
                    url      = $"{_rerumApiUrl}/api/create.action";
                    response = await client.PostAsync(url, content);  // await client.PostAsJsonAsync(url, manifest);
                }
                else if (method.Equals("put", StringComparison.InvariantCultureIgnoreCase))
                {
                    url      = $"{_rerumApiUrl}/api/update.action";
                    response = await client.PutAsync(url, content);
                }
                var jsonResponse = await response.Content.ReadAsStringAsync();

                if (response.StatusCode == HttpStatusCode.OK || response.StatusCode == HttpStatusCode.Created)
                {
                    _logger.LogDebug($"Manifest: Context: {manifest.Context}, id: {manifest.Id} post\\update successful.");
                    rt = JsonConvert.DeserializeObject <RerumManifestResponse>(jsonResponse, jsonSettings);
                }
                else
                {
                    _logger.LogWarning($"Unable to post\\update manifest: Context: {manifest.Context}, id: {manifest.Id}.");
                }
            }

            return(rt);
        }
예제 #2
0
        public async Task <AnnotationModel> Update(AnnotationModel model)
        {
            var updated = await UpdateAnnotation(model);

            var manifest = await _manifests.GetByAnnotationSourceId(updated.Target.SourceId);

            var source = await _annotationSources.SingleOrDefaultAsync(s => s.AnnotationSourceId == updated.Target.SourceId);

            RerumManifestResponse rerumResponse = null;

            // should we care if we save to rerum at this point?
            try
            {
                if (string.IsNullOrWhiteSpace(source.RerumStorageUrl))
                {
                    // Post the manifest
                    rerumResponse = await _rerum.PostManifest(manifest);
                }
                else
                {
                    rerumResponse = await _rerum.PutManifest(manifest);
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "An error occurred saving manifest to rerum.");
            }

            if (rerumResponse != null && (rerumResponse.Code == 200 || rerumResponse.Code == 201))
            {
                if (source.RerumStorageUrl != rerumResponse.NewObjectState.Id)
                {
                    source.RerumStorageUrl = rerumResponse.NewObjectState.Id;
                    await _unitOfWork.SaveChangesAsync();
                }
            }

            return(updated);
        }