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, enforceDie(apiKey != null,
"API key not present in configuration file; cannot proceed"); "API key not present in configuration file; cannot proceed");
esv = new ESVApi(apiKey); esv = ESVApi(apiKey);
if (aFlag) { if (aFlag) {
string tmpf, player; string tmpf, player;

View file

@ -171,73 +171,24 @@ verseValid(in char[] verse)
} }
/++ /++
+ ESV API object containing the authentication key, + Structure containing the authentication key, API URL,
+ the API URL, any parameters to use when contacting the + any parameters to use when making a request as well as the
+ API as well as the temporary directory to use when + temporary directory to use when fetching audio passages.
+ fetching audio passages.
+/ +/
class ESVApi struct ESVApi
{ {
protected {
string _key;
string _tmp;
string _url;
}
ESVApiOptions opts; ESVApiOptions opts;
string key; /++ API key +/
string tmp; /++ Tempfile directory +/
string url; /++ API URL +/
string extraParameters; /++ Additional request parameters +/
/++ Additional request parameters +/ this(string apiKey)
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")
{ {
_key = key; key = apiKey;
_tmp = tempDir() ~ tmpName; tmp = tempDir() ~ "esv";
_url = "https://api.esv.org/v3/passage"; url = "https://api.esv.org/v3/passage";
opts = ESVApiOptions(true); 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; File tmpFile;
tmpFile = File(_tmp, "w"); tmpFile = File(tmp, "w");
tmpFile.write(makeRequest(format!"audio/?q=%s+%s"( tmpFile.write(makeRequest(format!"audio/?q=%s+%s"(
book.capitalize().tr(" ", "+"), verse))); book.capitalize().tr(" ", "+"), verse)));
return _tmp; return tmp;
} }
/++ /++
@ -397,15 +348,14 @@ class ESVApi
HTTP request; HTTP request;
response = []; response = [];
request = HTTP(_url ~ "/" ~ query); request = HTTP(url ~ "/" ~ query);
request.onProgress = onProgress;
request.onReceive = request.onReceive =
(ubyte[] data) (ubyte[] data)
{ {
response ~= data; response ~= data;
return data.length; return data.length;
}; };
request.addRequestHeader("Authorization", "Token " ~ _key); request.addRequestHeader("Authorization", "Token " ~ key);
request.perform(); request.perform();
return response; return response;

View file

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