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> 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.º 3
0
        /// <summary>
        /// Uploads article to Aistant
        /// </summary>
        /// <param name="uri">uri of article</param>
        /// <param name="title">title of article</param>
        /// <param name="body">body of article</param>
        /// <returns></returns>
        public async Task <bool> UploadArticleAsync(string uri, string title, string body, string excerpt, bool isSection = false)
        {
            if (string.IsNullOrEmpty(_accessToken))
            {
                await Login();
            }

            var http = new HttpClient();

            http.SetBearerToken(_accessToken);

            var            articleUri = (_mainSection != null) ? _mainSection.Uri.CombineWithUri(uri) : uri;
            AistantArticle article    = await GetArticleAsync(articleUri);

            if (article == null)
            {
                article            = new AistantArticle();
                article.Content    = body;
                article.IndexTitle = title;
                article.Title      = title;
                article.Uri        = articleUri;
                article.Excerpt    = excerpt;
                article.KbId       = _currentKb.Id;
                article.Kind       = (isSection) ? DocItemKind.Section : DocItemKind.Article;

                if (_mainSection != null)
                {
                    article.ParentId = _mainSection.Id;
                    article.IndexNum = GetMaxIndexNumForDoc(_mainSection.Uri) + 1024;
                    _indexNumMetaInfo[_mainSection.Uri].Add(article.Uri, article.IndexNum);
                }
                else
                {
                    article.IndexNum = GetMaxIndexNumForDoc(_nullSectionUri) + 1024;
                    _indexNumMetaInfo[_nullSectionUri].Add(article.Uri, article.IndexNum);
                }

                if (isSection)
                {
                    _indexNumMetaInfo[article.Uri] = new Dictionary <string, int>();
                }

                article.FormatType = FormatType.Markdown;

                article = (isSection) ? await CreateSectionAsync(article) : await CreateArticleAsync(article);
            }
            else
            {
                if (article.Content != body)
                {
                    article.Content = body;

                    article = (_settings.AddVersion)
                        ? await CreateVersionAsync(article)
                        : await UpdateLastVersionAsync(article);
                }
                else
                {
                    Info($"Article [{article.Title}] already EXISTS");
                }
            }

            if (_settings.Publish)
            {
                article = await PublishArticleAsync(article);
            }

            return(article != null);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Upload an article to Aistant into a specified section. If this section doesn't exist - it will be created automatically
        /// </summary>
        /// <param name="sectionUri">Section URI</param>
        /// <param name="sectionTitle">Ssection's title</param>
        /// <param name="articleUri">Article's URI</param>
        /// <param name="articleTitle">Article's title</param>
        /// <param name="articleBody">Article's body</param>
        /// <returns></returns>
        public async Task <bool> UploadArticleAsync(string sectionUri, string sectionTitle, string articleUri, string articleTitle, string articleBody, string articleExcerpt, bool isSection = false)
        {
            if (string.IsNullOrEmpty(sectionUri))
            {
                return(await UploadArticleAsync(articleUri, articleTitle, articleBody, articleExcerpt, isSection));
            }

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

            var http = new HttpClient();

            http.SetBearerToken(_accessToken);

            string currentSectionUri = (_mainSection != null) ? _mainSection.Uri.CombineWithUri(sectionUri) : sectionUri;

            AistantArticle currentSection = await GetSectionAsync(currentSectionUri);

            if (!_loadedSections.Contains(currentSectionUri))
            {
                if (currentSection == null)
                {
                    currentSection       = new AistantArticle();
                    currentSection.KbId  = _currentKb.Id;
                    currentSection.Kind  = DocItemKind.Section;
                    currentSection.Title = sectionTitle;
                    currentSection.Uri   = currentSectionUri;

                    if (_mainSection != null)
                    {
                        currentSection.ParentId = _mainSection.Id;
                        currentSection.IndexNum = GetMaxIndexNumForDoc(_mainSection.Uri) + 1024;
                        _indexNumMetaInfo[_mainSection.Uri].Add(currentSection.Uri, currentSection.IndexNum);
                    }
                    else
                    {
                        currentSection.IndexNum = GetMaxIndexNumForDoc(_nullSectionUri) + 1024;
                        _indexNumMetaInfo[_nullSectionUri].Add(currentSection.Uri, currentSection.IndexNum);
                    }
                    _indexNumMetaInfo[currentSection.Uri] = new Dictionary <string, int>();

                    currentSection = await CreateSectionAsync(currentSection);
                }
                else
                {
                    currentSection = GetSectionByIdAsync(currentSection.Id).Result;

                    if (sectionTitle != null && (currentSection.Title != sectionTitle ||
                                                 currentSection.IndexTitle != sectionTitle))
                    {
                        currentSection.Title      = sectionTitle;
                        currentSection.IndexTitle = sectionTitle;

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

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

                _loadedSections.Add(currentSectionUri);
            }

            var            newArticleUri = currentSectionUri.CombineWithUri(articleUri);
            AistantArticle article       = await GetArticleAsync(newArticleUri);

            if (article == null)
            {
                article            = new AistantArticle();
                article.Content    = articleBody;
                article.IndexTitle = articleTitle;
                article.Title      = articleTitle;
                article.Uri        = newArticleUri;
                article.KbId       = _currentKb.Id;
                article.Excerpt    = articleExcerpt;
                article.Kind       = (isSection) ? DocItemKind.Section : DocItemKind.Article;
                article.ParentId   = currentSection.Id;
                article.IndexNum   = GetMaxIndexNumForDoc(currentSection.Uri) + 1024;

                _indexNumMetaInfo[currentSection.Uri].Add(article.Uri, article.IndexNum);

                if (isSection)
                {
                    _indexNumMetaInfo[article.Uri] = new Dictionary <string, int>();
                }


                article.FormatType = FormatType.Markdown;

                article = (isSection) ? await CreateSectionAsync(article) : await CreateArticleAsync(article);
            }
            else
            {
                if (article.Content != articleBody || article.Excerpt != articleExcerpt)
                {
                    article.Content = articleBody;
                    article.Excerpt = articleExcerpt;

                    article = (_settings.AddVersion)
                        ? await CreateVersionAsync(article)
                        : await UpdateLastVersionAsync(article);
                }
                else
                {
                    Info("Article '" + article.Title + "' already EXISTS with such content in Aistant");
                }
            }

            if (_settings.Publish)
            {
                article = await PublishArticleAsync(article);
            }

            return(article != null);
        }
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;
                }
            }
        }