Skip to content

Commit

Permalink
Developer Mode Support (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
pmachapman authored Jul 30, 2021
1 parent becbcfd commit 959c825
Show file tree
Hide file tree
Showing 19 changed files with 600 additions and 258 deletions.
99 changes: 99 additions & 0 deletions GoToBible.Engine/GotoBibleApiRenderer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// -----------------------------------------------------------------------
// <copyright file="GotoBibleApiRenderer.cs" company="Conglomo">
// Copyright 2020-2021 Conglomo Limited. Please see LICENSE for license details.
// </copyright>
// -----------------------------------------------------------------------

namespace GoToBible.Engine
{
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Json;
using System.Threading.Tasks;
using GoToBible.Model;

/// <summary>
/// The GoTo.Bible API Renderer.
/// </summary>
/// <seealso cref="GoToBible.Model.IRenderer" />
public class GotoBibleApiRenderer : IRenderer
{
/// <summary>
/// The HTTP client.
/// </summary>
private readonly HttpClient httpClient;

/// <summary>
/// A value indicating whether or not this instance has been disposed.
/// </summary>
private bool disposedValue;

/// <summary>
/// Initialises a new instance of the <see cref="GotoBibleApiRenderer" /> class.
/// </summary>
public GotoBibleApiRenderer()
{
this.Providers = new List<IProvider>();
this.httpClient = new HttpClient
{
BaseAddress = new Uri("https://goto.bible/", UriKind.Absolute),
};
}

/// <summary>
/// Finalises an instance of the <see cref="GotoBibleApiRenderer"/> class.
/// </summary>
~GotoBibleApiRenderer()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
this.Dispose(false);
}

/// <inheritdoc/>
public IReadOnlyCollection<IProvider> Providers { get; set; }

/// <inheritdoc/>
public void Dispose()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
this.Dispose(disposing: true);
GC.SuppressFinalize(this);
}

/// <inheritdoc/>
public async Task<RenderedPassage> RenderAsync(RenderingParameters parameters, bool renderCompleteHtmlPage)
{
string url = $"RenderPassage?renderCompleteHtmlPage={renderCompleteHtmlPage}";
HttpResponseMessage response = await this.httpClient.PostAsJsonAsync(url, parameters);
if (response.IsSuccessStatusCode)
{
return await response.Content.ReadFromJsonAsync<RenderedPassage>() ?? new RenderedPassage();
}
else
{
return new RenderedPassage();
}
}

/// <summary>
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
protected virtual void Dispose(bool disposing)
{
if (!this.disposedValue)
{
if (disposing)
{
// dispose managed state (managed objects)
this.httpClient.Dispose();
}

// free unmanaged resources (unmanaged objects) and override finalizer
// set large fields to null
this.disposedValue = true;
}
}
}
}
20 changes: 4 additions & 16 deletions GoToBible.Engine/Renderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace GoToBible.Engine
/// <summary>
/// The renderer.
/// </summary>
public class Renderer : IDisposable
public class Renderer : IRenderer
{
/// <summary>
/// A horizontal line.
Expand Down Expand Up @@ -61,13 +61,8 @@ public Renderer(IEnumerable<IProvider> providers)
this.Dispose(false);
}

/// <summary>
/// Gets the provides.
/// </summary>
/// <value>
/// The providers.
/// </value>
public List<IProvider> Providers { get; }
/// <inheritdoc/>
public IReadOnlyCollection<IProvider> Providers { get; set; }

/// <inheritdoc/>
public void Dispose()
Expand All @@ -77,14 +72,7 @@ public void Dispose()
GC.SuppressFinalize(this);
}

/// <summary>
/// Renders the specified parameters.
/// </summary>
/// <param name="parameters">The parameters.</param>
/// <param name="renderCompleteHtmlPage">If set to <c>true</c>, render the complete HTML page.</param>
/// <returns>
/// The output of the rendering.
/// </returns>
/// <inheritdoc/>
public async Task<RenderedPassage> RenderAsync(RenderingParameters parameters, bool renderCompleteHtmlPage)
{
// Set up the rendered passage
Expand Down
4 changes: 2 additions & 2 deletions GoToBible.Model/GoToBible.Model.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
<ApplicationIcon>App.ico</ApplicationIcon>
<PackageLicenseExpression>LGPL-3.0-or-later</PackageLicenseExpression>
<RepositoryType>git</RepositoryType>
<PackageReleaseNotes>The RenderColour class is now used instead of Color, and AsPassageReference now has a default chapter parameter.</PackageReleaseNotes>
<Version>1.1</Version>
<PackageReleaseNotes>Added IRenderer Interface</PackageReleaseNotes>
<Version>1.1.1</Version>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
Expand Down
39 changes: 39 additions & 0 deletions GoToBible.Model/IRenderer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// -----------------------------------------------------------------------
// <copyright file="IRenderer.cs" company="Conglomo">
// Copyright 2020-2021 Conglomo Limited. Please see LICENSE for license details.
// </copyright>
// -----------------------------------------------------------------------

namespace GoToBible.Model
{
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

/// <summary>
/// The renderer interface.
/// </summary>
public interface IRenderer : IDisposable
{
/// <summary>
/// Gets or sets the providers.
/// </summary>
/// <value>
/// The providers.
/// </value>
/// <remarks>
/// These can be all disposed by the renderer.
/// </remarks>
IReadOnlyCollection<IProvider> Providers { get; set; }

/// <summary>
/// Renders the specified parameters.
/// </summary>
/// <param name="parameters">The parameters.</param>
/// <param name="renderCompleteHtmlPage">If set to <c>true</c>, render the complete HTML page.</param>
/// <returns>
/// The output of the rendering.
/// </returns>
Task<RenderedPassage> RenderAsync(RenderingParameters parameters, bool renderCompleteHtmlPage);
}
}
46 changes: 21 additions & 25 deletions GoToBible.Providers/BundledTranslations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,11 @@ public class BundledTranslations : IProvider
public void Dispose() => GC.SuppressFinalize(this);

/// <inheritdoc/>
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
public async IAsyncEnumerable<Book> GetBooksAsync(string translation, bool includeChapters)
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
{
foreach (Book book in Canon[translation].GetBooks(includeChapters))
{
yield return book;
yield return await Task.FromResult(book);
}
}

Expand Down Expand Up @@ -176,11 +174,9 @@ public async Task<Chapter> GetChapterAsync(string translation, string book, int
}

/// <inheritdoc/>
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
public async IAsyncEnumerable<Translation> GetTranslationsAsync()
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
{
yield return new Translation
yield return await Task.FromResult(new Translation
{
CanBeExported = true,
Code = "BCPPSALMS",
Expand All @@ -189,8 +185,8 @@ public async IAsyncEnumerable<Translation> GetTranslationsAsync()
Name = "Coverdale Psalter",
Provider = this.Id,
Year = 1539,
};
yield return new Translation
});
yield return await Task.FromResult(new Translation
{
CanBeExported = true,
Code = "BCPPSALMSALT",
Expand All @@ -199,8 +195,8 @@ public async IAsyncEnumerable<Translation> GetTranslationsAsync()
Name = "Coverdale Psalter (Alternate Versification)",
Provider = this.Id,
Year = 1539,
};
yield return new Translation
});
yield return await Task.FromResult(new Translation
{
CanBeExported = true,
Code = "BSB",
Expand All @@ -209,8 +205,8 @@ public async IAsyncEnumerable<Translation> GetTranslationsAsync()
Name = "Berean Study Bible",
Provider = this.Id,
Year = 2020,
};
yield return new Translation
});
yield return await Task.FromResult(new Translation
{
Author = "Joseph Barber Lightfoot; Updated by Peter Chapman",
CanBeExported = true,
Expand All @@ -220,8 +216,8 @@ public async IAsyncEnumerable<Translation> GetTranslationsAsync()
Name = "Epistle to the Laodiceans (Greek)",
Provider = this.Id,
Year = 2021,
};
yield return new Translation
});
yield return await Task.FromResult(new Translation
{
Author = "Joseph Barber Lightfoot; Updated by Peter Chapman",
CanBeExported = true,
Expand All @@ -231,8 +227,8 @@ public async IAsyncEnumerable<Translation> GetTranslationsAsync()
Name = "Epistle to the Laodiceans (Latin)",
Provider = this.Id,
Year = 2021,
};
yield return new Translation
});
yield return await Task.FromResult(new Translation
{
Author = "John Wycliffe",
CanBeExported = true,
Expand All @@ -242,8 +238,8 @@ public async IAsyncEnumerable<Translation> GetTranslationsAsync()
Name = "Epistle to the Laodiceans (Old English)",
Provider = this.Id,
Year = 2021,
};
yield return new Translation
});
yield return await Task.FromResult(new Translation
{
CanBeExported = true,
Code = "NTA",
Expand All @@ -252,8 +248,8 @@ public async IAsyncEnumerable<Translation> GetTranslationsAsync()
Name = "New Translation of the Apocrypha",
Provider = this.Id,
Year = 2021,
};
yield return new Translation
});
yield return await Task.FromResult(new Translation
{
CanBeExported = true,
Commentary = true,
Expand All @@ -263,8 +259,8 @@ public async IAsyncEnumerable<Translation> GetTranslationsAsync()
Name = "New Translation of the Apocrypha Notes",
Provider = this.Id,
Year = 2021,
};
yield return new Translation
});
yield return await Task.FromResult(new Translation
{
Author = "Michael W. Holmes",
Code = "SBLGNT",
Expand All @@ -273,8 +269,8 @@ public async IAsyncEnumerable<Translation> GetTranslationsAsync()
Name = "SBL Greek New Testament",
Provider = this.Id,
Year = 2010,
};
yield return new Translation
});
yield return await Task.FromResult(new Translation
{
Author = "Michael W. Holmes",
Code = "SBLGNTAPP",
Expand All @@ -284,7 +280,7 @@ public async IAsyncEnumerable<Translation> GetTranslationsAsync()
Name = "SBL Greek New Testament Apparatus",
Provider = this.Id,
Year = 2010,
};
});
}
}
}
8 changes: 2 additions & 6 deletions GoToBible.Providers/EsvBible.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,11 @@ public EsvBible(IOptions<EsvBibleOptions> options, IDistributedCache cache)
public override string Name => "ESV API";

/// <inheritdoc/>
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
public override async IAsyncEnumerable<Book> GetBooksAsync(string translation, bool includeChapters)
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
{
foreach (Book book in Canon.GetBooks(includeChapters))
{
yield return book;
yield return await Task.FromResult(book);
}
}

Expand Down Expand Up @@ -272,13 +270,11 @@ public override async Task<Chapter> GetChapterAsync(string translation, string b
}

/// <inheritdoc/>
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
public override async IAsyncEnumerable<Translation> GetTranslationsAsync()
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
{
if (!string.IsNullOrWhiteSpace(this.options.ApiKey))
{
yield return Translation;
yield return await Task.FromResult(Translation);
}
}

Expand Down
Loading

0 comments on commit 959c825

Please sign in to comment.