Example #1
0
        public override async Task <bool> Open(long auditKey, SelectQuery query, CancellationToken cancellationToken)
        {
            AuditKey     = auditKey;
            _selectQuery = query;

            if (_isOpen)
            {
                throw new ConnectionException("The file reader connection is already open.");
            }

            // if a filename was specified in the query, use this, otherwise, get a list of files from the incoming directory.
            if (string.IsNullOrEmpty(query?.FileName))
            {
                _files = await _fileConnection.GetFileEnumerator(CacheFlatFile, EFlatFilePath.Incoming,
                                                                 CacheFlatFile.FileMatchPattern);
            }
            else
            {
                _files = await _fileConnection.GetFileEnumerator(CacheFlatFile, query.Path,
                                                                 query.FileName);
            }

            if (_files.MoveNext() == false)
            {
                throw new ConnectionException($"There are no matching files in the incoming directory.");
            }

            var fileStream = await _fileConnection.GetReadFileStream(CacheFlatFile, EFlatFilePath.Incoming, _files.Current.FileName);

            if (_fileNameOrdinal >= 0)
            {
                _baseRow[_fileNameOrdinal] = _files.Current.FileName;
            }

            try
            {
                await _fileHandler.SetStream(fileStream, query);
            }
            catch (Exception ex)
            {
                throw new ConnectionException($"Failed to read the file {_files.Current.FileName}.  {ex.Message}", ex);
            }

            return(true);
        }
Example #2
0
        public override async Task <bool> Open(long auditKey, SelectQuery query, CancellationToken cancellationToken)
        {
            AuditKey = auditKey;

            try
            {
                if (_isOpen)
                {
                    throw new ConnectionException("The information hub connection is already open.");
                }

                var downloadUrl = await _dexihConnection.GetDownloadUrl();

                var intanceId = await _dexihConnection.GetRemoteAgentInstanceId();

                // call the central web server to requet the query start.
                var message = Json.SerializeObject(new
                {
                    HubName = ReferenceConnection.DefaultDatabase,
                    CacheTable.SourceConnectionName,
                    TableName   = CacheTable.Name,
                    TableSchema = CacheTable.Schema,
                    Query       = query,
                    DownloadUrl = downloadUrl,
                    InstanceId  = intanceId
                }, "");

                var content  = new StringContent(message, Encoding.UTF8, "application/json");
                var response = await _dexihConnection.HttpPost("OpenTableQuery", content);

                if ((bool)response["success"])
                {
                    _dataUrl = response["value"].ToString();
                }
                else
                {
                    throw new ConnectionException($"Error {response?["message"]}", new Exception(response["exceptionDetails"].ToString()));
                }

                // use the returned url, to start streaming the data.
                using (var httpClient = new HttpClient())
                {
                    var response2 = await httpClient.GetAsync(_dataUrl, HttpCompletionOption.ResponseHeadersRead, cancellationToken);

                    if (response2.StatusCode == HttpStatusCode.InternalServerError)
                    {
                        var responseString = await response2.Content.ReadAsStringAsync();

                        var result      = JObject.Parse(responseString);
                        var returnValue = result.ToObject <ReturnValue>();
                        throw new ConnectionException("Dexih Reader Failed.  " + returnValue.Message,
                                                      returnValue.Exception);
                    }

                    var responseStream = await response2.Content.ReadAsStreamAsync();

                    var config = new FileConfiguration();
                    _fileHandler = new FileHandlerText(CacheTable, config);
                    await _fileHandler.SetStream(responseStream, null);

                    _baseRow = new object[CacheTable.Columns.Count];

                    return(true);
                }
            }
            catch (Exception ex)
            {
                throw new ConnectionException($"Opening connection to information hub failed.  {ex.Message}", ex);
            }
        }