-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
IMP: Way to run the site without needing API keys
With the API.Offline mode, you can now run the site without needing any API keys
- Loading branch information
Owain
committed
Jul 8, 2022
1 parent
f941e3b
commit d66979d
Showing
7 changed files
with
108 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace h5yr.Settings; | ||
|
||
public class APISettings | ||
{ | ||
public string? Offline { get; set; } | ||
public string? CreateOfflineFile { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,73 @@ | ||
using H5YR.Core.Services; | ||
using h5yr.Settings; | ||
using H5YR.Core.Services; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.Extensions.Options; | ||
using Newtonsoft.Json; | ||
using System.Text.Json; | ||
|
||
namespace h5yr.ViewComponents | ||
{ | ||
public class TweetsViewComponent : ViewComponent | ||
{ | ||
private readonly ITwitterHelper twitterHelper; | ||
private readonly IOptions<APISettings> apiSettings; | ||
private readonly ILogger<TweetsViewComponent> logger; | ||
|
||
public TweetsViewComponent(ITwitterHelper twitterHelper) | ||
|
||
public TweetsViewComponent(ITwitterHelper twitterHelper, IOptions<APISettings> apiSettings, ILogger<TweetsViewComponent> logger) | ||
{ | ||
this.twitterHelper = twitterHelper; | ||
this.apiSettings = apiSettings; | ||
this.logger = logger; | ||
} | ||
|
||
public IViewComponentResult Invoke(string loadmore = "false") | ||
{ | ||
|
||
HttpContext.Session.SetInt32("NumberOfTweetsDisplayed", 12); | ||
|
||
List<TweetModel> tweets; | ||
|
||
tweets = twitterHelper.GetAllTweets(0, 12); | ||
List<TweetModel> tweet = new(); | ||
|
||
// var json = JsonConvert.SerializeObject(tweets); | ||
// string fileName = "TestTweets.json"; | ||
// string jsonString = System.Text.Json.JsonSerializer.Serialize(tweets); | ||
// File.WriteAllText(fileName, jsonString); | ||
if(apiSettings.Value.Offline == null || apiSettings.Value.Offline.ToLowerInvariant() != "true") | ||
{ | ||
tweet = twitterHelper.GetAllTweets(0, 12); | ||
|
||
string fileName = "TestTweets.json"; | ||
string jsonString = File.ReadAllText(fileName); | ||
List<TweetModel> tweet = System.Text.Json.JsonSerializer.Deserialize<List<TweetModel>>(jsonString)!; | ||
// You can only create a new TestTweets file if you have | ||
// a valid Twitter API key setup | ||
if (apiSettings.Value.CreateOfflineFile != null || apiSettings.Value.CreateOfflineFile?.ToLowerInvariant() == "true") | ||
{ | ||
try | ||
{ | ||
var json = JsonConvert.SerializeObject(tweet); | ||
string fileName = "TestTweets.json"; | ||
string jsonString = System.Text.Json.JsonSerializer.Serialize(json); | ||
File.WriteAllText(fileName, jsonString); | ||
} | ||
catch(Exception ex) | ||
{ | ||
logger.LogError("Error: Unable to write Test Tweets Json file", ex); | ||
} | ||
|
||
|
||
} | ||
} | ||
else | ||
{ | ||
try | ||
{ | ||
string fileName = "TestTweets.json"; | ||
string jsonString = File.ReadAllText(fileName); | ||
tweet = System.Text.Json.JsonSerializer.Deserialize<List<TweetModel>>(jsonString)!; | ||
} | ||
catch (Exception ex) | ||
{ | ||
logger.LogError("Error: Unable to read Tweet Json file", ex); | ||
} | ||
|
||
} | ||
|
||
return View(tweet); | ||
} | ||
|
||
|
||
|
||
|
||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters