Ejemplo n.º 1
0
        private async Task <AistantArticle> CreateVersionAsync(AistantArticle article)
        {
            if (string.IsNullOrEmpty(_accessToken))
            {
                await Login();
            }

            if (_currentKb == null)
            {
                _currentKb = await GetKnowledgeBaseAsync(_settings.Kb);
            }

            if (_currentKb == null)
            {
                throw new KbRequestError("Knowledge base dose not exist: " + _settings.Kb);
            }

            article.LastVersion++;

            _httpClient.SetBearerToken(_accessToken);

            var url = _settings.ApiHost
                      .CombineWithUri(_settings.ArticlesEndpoint)
                      .CombineWithUri(article.Id)
                      .CombineWithUri("versions");

            var content = new StringContent(
                JsonConvert.SerializeObject(article),
                System.Text.Encoding.UTF8,
                "application/json"
                );

            var response = await _httpClient.PostAsync(url, content);

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

            if (response.StatusCode == HttpStatusCode.OK)
            {
            }

            if (!string.IsNullOrEmpty(responseStr))
            {
                article = JsonConvert.DeserializeObject <AistantArticle>(responseStr);
                Info($"VERSION UPDATE: {article.Uri} - {article.IndexTitle} (version: {article.LastVersion}");
                return(article);
            }
            else
            {
                var responseCodeStr = ((int)response.StatusCode).ToString();
                throw new KbRequestError($"Version has not been updated in Aistant. Reason: {responseCodeStr} - {responseStr}");
            }
        }
Ejemplo n.º 2
0
        private async Task <AistantArticle> GetSectionAsync(string uri)
        {
            if (string.IsNullOrEmpty(_accessToken))
            {
                await Login();
            }

            if (_currentKb == null)
            {
                _currentKb = await GetKnowledgeBaseAsync(_settings.Kb);
            }

            if (_currentKb == null)
            {
                throw new Exception("Knowledge base dose not exist: " + _settings.Kb);
            }

            _httpClient.SetBearerToken(_accessToken);

            var url = _settings.ApiHost
                      .CombineWithUri(_settings.ArticlesEndpoint)
                      .CombineWithUri("uri");

            var uriBuilder = new UriBuilder(url);
            var query      = HttpUtility.ParseQueryString(uriBuilder.Query);

            query["kb"]      = _currentKb.Id;
            query["uri"]     = uri;
            uriBuilder.Query = query.ToString();
            url = uriBuilder.ToString();

            var response = await _httpClient.GetAsync(url);

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

            if (response.StatusCode == HttpStatusCode.OK)
            {
                return(!string.IsNullOrEmpty(responseStr)
                  ? JsonConvert.DeserializeObject <AistantArticle>(responseStr)
                  : null);
            }
            else if (response.StatusCode == HttpStatusCode.NotFound)
            {
                return(null);
            }
            else
            {
                throw new KbRequestError(ReturnResponseError(responseStr));
            }
        }
Ejemplo n.º 3
0
        private async Task <AistantArticle> CreateArticleAsync(AistantArticle article)
        {
            if (article.Kind == DocItemKind.Section)
            {
                throw new InvalidActionException("Use method CreateSectionAsync to create section");
            }

            if (string.IsNullOrEmpty(_accessToken))
            {
                await Login();
            }

            if (_currentKb == null)
            {
                _currentKb = await GetKnowledgeBaseAsync(_settings.Kb);
            }

            if (_currentKb == null)
            {
                throw new KbRequestError("Knowledge base dose not exist: " + _settings.Kb);
            }

            _httpClient.SetBearerToken(_accessToken);

            var url = _settings.ApiHost.CombineWithUri(_settings.ArticlesEndpoint);

            var content = new StringContent(
                JsonConvert.SerializeObject(article),
                System.Text.Encoding.UTF8,
                "application/json"
                );

            var response = await _httpClient.PostAsync(url, content);

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

            if (response.StatusCode == HttpStatusCode.OK ||
                response.StatusCode == HttpStatusCode.Created)
            {
                article = JsonConvert.DeserializeObject <AistantArticle>(responseStr);
                Info($"CREATED: {article.Uri} - {article.IndexTitle}");
                return(article);
            }
            else
            {
                var responseCodeStr = ((int)response.StatusCode).ToString();
                throw new KbRequestError($"Version has not been created in Aistant. Reason: {responseCodeStr} - {responseStr}");
            }
        }
Ejemplo n.º 4
0
        private async Task <AistantDocs> GetDocsFromKbAsync()
        {
            if (string.IsNullOrEmpty(_accessToken))
            {
                await Login();
            }

            if (_currentKb == null)
            {
                _currentKb = await GetKnowledgeBaseAsync(_settings.Kb);
            }

            if (_currentKb == null)
            {
                throw new Exception("Knowledge base dose not exist: " + _settings.Kb);
            }

            _httpClient.SetBearerToken(_accessToken);

            var url = _settings.ApiHost
                      .CombineWithUri(_settings.DocsEndpoint)
                      .CombineWithUri("index")
                      .CombineWithUri(_currentKb.Moniker)
                      .CombineWithUri("all");

            var response = await _httpClient.GetAsync(url);

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

            if (response.StatusCode == HttpStatusCode.OK)
            {
                return(JsonConvert.DeserializeObject <AistantDocs>(responseStr));
            }
            else
            {
                throw new KbRequestError(ReturnResponseError(responseStr));
            }
        }
Ejemplo n.º 5
0
        public AistantKbService(AistantSettings settings, ILogger logger = null)
        {
            _logger = logger;

            _settings = settings;

            _currentKb = GetKnowledgeBaseAsync(_settings.Kb).Result;

            if (_currentKb == null)
            {
                throw new Exception("Knowledge base doesn't exist: " + _settings.Kb);
            }

            _indexNumMetaInfo = new Dictionary <string, Dictionary <string, int> > {
                [_nullSectionUri] = new Dictionary <string, int>()
            };

            var docs = GetDocsFromKbAsync().Result;

            AnalizeDocuments(docs.Items);

            if (!string.IsNullOrEmpty(_settings.Section.Uri))
            {
                _mainSection = GetSectionAsync(_settings.Section.Uri).Result;

                //Create section, if it doen't exist
                if (_mainSection == null)
                {
                    _mainSection          = new AistantArticle();
                    _mainSection.KbId     = _currentKb.Id;
                    _mainSection.Title    = _settings.Section.Title;
                    _mainSection.Uri      = _settings.Section.Uri;
                    _mainSection.IndexNum = GetMaxIndexNumForDoc(_nullSectionUri) + 1024;
                    _mainSection.Kind     = DocItemKind.Section;

                    _mainSection = CreateSectionAsync(_mainSection).Result;

                    _indexNumMetaInfo[_nullSectionUri].Add(_mainSection.Uri, _mainSection.IndexNum);
                    _indexNumMetaInfo[_mainSection.Uri] = new Dictionary <string, int>();
                }
                else
                {
                    _mainSection = GetSectionByIdAsync(_mainSection.Id).Result;

                    if (_mainSection.Title != _settings.Section.Title ||
                        _mainSection.IndexTitle != _settings.Section.Title)
                    {
                        _mainSection.Title      = _settings.Section.Title;
                        _mainSection.IndexTitle = _settings.Section.Title;

                        _mainSection = (_settings.AddVersion)
                                       ? CreateVersionAsync(_mainSection).Result
                                       : UpdateLastVersionAsync(_mainSection).Result;
                    }
                    else
                    {
                        Info($"Section [{_mainSection.Title}] already EXISTS");
                    }
                }

                if (_settings.Publish)
                {
                    _mainSection = PublishArticleAsync(_mainSection).Result;
                }
            }
        }