esvapi: refactor

18 insertions, 68 deletions! (🚀)

Fixes: https://todo.sr.ht/~jeremy/esv/6
This commit is contained in:
Jeremy Baxter 2024-06-29 20:41:14 +12:00
parent a2be024c45
commit f353279458
3 changed files with 18 additions and 68 deletions

2
esv.d
View file

@ -131,7 +131,7 @@ key = %s
enforceDie(apiKey != null,
"API key not present in configuration file; cannot proceed");
esv = new ESVApi(apiKey);
esv = ESVApi(apiKey);
if (aFlag) {
string tmpf, player;

View file

@ -171,73 +171,24 @@ verseValid(in char[] verse)
}
/++
+ ESV API object containing the authentication key,
+ the API URL, any parameters to use when contacting the
+ API as well as the temporary directory to use when
+ fetching audio passages.
+ Structure containing the authentication key, API URL,
+ any parameters to use when making a request as well as the
+ temporary directory to use when fetching audio passages.
+/
class ESVApi
struct ESVApi
{
protected {
string _key;
string _tmp;
string _url;
}
ESVApiOptions opts;
string key; /++ API key +/
string tmp; /++ Tempfile directory +/
string url; /++ API URL +/
string extraParameters; /++ Additional request parameters +/
/++ Additional request parameters +/
string extraParameters;
/++ Called whenever progress is made on a request. +/
int delegate(size_t, size_t, size_t, size_t) onProgress;
/++
+ Constructs an ESVApi object using the given authentication key.
+/
this(string key, string tmpName = "esv")
this(string apiKey)
{
_key = key;
_tmp = tempDir() ~ tmpName;
_url = "https://api.esv.org/v3/passage";
key = apiKey;
tmp = tempDir() ~ "esv";
url = "https://api.esv.org/v3/passage";
opts = ESVApiOptions(true);
extraParameters = "";
onProgress = delegate int (size_t dlTotal, size_t dlNow,
size_t ulTotal, size_t ulNow)
{
return 0;
};
}
/++
+ Returns the API authentication key that was given when the object
+ was constructed. This authentication key cannot be changed.
+/
final @property string
key() const nothrow pure @nogc
=> _key;
/++
+ Returns the subdirectory used to store temporary audio passages.
+/
final @property string
tmpDir() const nothrow pure @nogc
=> _tmp;
/++
+ Returns the API URL currently in use.
+/
final @property string
url() const nothrow pure @nogc
=> _url;
/++
+ Sets the API URL currently in use to the given url argument.
+/
final @property void
url(immutable(string) url)
in (!url.matchAll(`^https?://.+\\..+(/.+)?`).empty, "Invalid URL format")
{
_url = url;
}
/++
@ -308,11 +259,11 @@ class ESVApi
{
File tmpFile;
tmpFile = File(_tmp, "w");
tmpFile = File(tmp, "w");
tmpFile.write(makeRequest(format!"audio/?q=%s+%s"(
book.capitalize().tr(" ", "+"), verse)));
return _tmp;
return tmp;
}
/++
@ -397,15 +348,14 @@ class ESVApi
HTTP request;
response = [];
request = HTTP(_url ~ "/" ~ query);
request.onProgress = onProgress;
request = HTTP(url ~ "/" ~ query);
request.onReceive =
(ubyte[] data)
{
response ~= data;
return data.length;
};
request.addRequestHeader("Authorization", "Token " ~ _key);
request.addRequestHeader("Authorization", "Token " ~ key);
request.perform();
return response;

View file

@ -97,7 +97,7 @@ main(string[] args)
enforceDie(apiKey != null,
"API key not present in configuration file; cannot proceed");
esv = new ESVApi(apiKey);
esv = ESVApi(apiKey);
try
writeln(esv.searchFormat(args[1]));