Skip to content

Commit

Permalink
Merge pull request #166 from daisy/tts-engines-props
Browse files Browse the repository at this point in the history
feat(tts): differenciated engine props update
  • Loading branch information
marisademeglio authored Oct 25, 2023
2 parents 503c4c2 + 6bc7ebc commit f328155
Show file tree
Hide file tree
Showing 6 changed files with 404 additions and 152 deletions.
142 changes: 4 additions & 138 deletions src/main/data/apis/pipeline.ts
Original file line number Diff line number Diff line change
@@ -1,139 +1,5 @@
import {
aliveXmlToJson,
datatypesXmlToJson,
datatypeXmlToJson,
jobRequestToXml,
jobsXmlToJson,
jobXmlToJson,
scriptsXmlToJson,
scriptXmlToJson,
voicesToJson,
ttsConfigToXml,
} from 'shared/parser/pipelineXmlConverter'
import {
Datatype,
baseurl,
Job,
ResultFile,
Script,
Webservice,
NamedResult,
} from 'shared/types'
import fetch from 'node-fetch'
import { info } from 'electron-log'
import { PipelineAPI } from 'shared/data/apis/pipeline'

import fetch, { Response, RequestInit } from 'node-fetch'

import { info, error } from 'electron-log'
import { jobResponseXmlToJson } from 'shared/parser/pipelineXmlConverter/jobResponseToJson'
import { selectTtsConfig } from 'shared/data/slices/settings'
import { store } from '../store'

/**
* Create a fetch function on the pipeline webservice
* for which the resulting pipeline xml is parsed and converted to a js object
* @type T return type of the parser
* @param webserviceUrlBuilder method to build a url,
* optionnaly using a webservice (like ``(ws) => `${baseurl(ws)}/scripts` ``)
* @param parser method to convert pipeline xml to an object object
* @param options options to be passed to the fetch call
* (like `{method:'POST', body:whateveryoulike}`)
* @returns a customized fetch function from the webservice
* `` (ws:Webservice) => Promise<Awaited<T>> ``
*/
function createPipelineFetchFunction<T>(
webserviceUrlBuilder: (ws: Webservice) => string,
parser: (text: string) => T,
options?: RequestInit
) {
return (ws?: Webservice) => {
info('fetching ', webserviceUrlBuilder(ws))
return fetch(webserviceUrlBuilder(ws), options)
.then((response: Response) => response.text())
.then((text: string) => parser(text))
}
}

/**
* Create a simple request on the pipeline webservice
* @param webserviceUrlBuilder method to build a url, optionnaly using a webservice (like ``(ws) => `${baseurl(ws)}/scripts` ``)
* @param options options to be passed to the fetch call (like `{method:'POST', body:whateveryoulike}`)
* @returns a customized fetch function from the webservice `` (ws:Webservice) => Promise<Awaited<T>> ``
*/
function createPipelineRequestFunction(
webserviceUrlBuilder: (ws: Webservice) => string,
options?: RequestInit
) {
return (ws?: Webservice) => {
info('request', options.method ?? '', webserviceUrlBuilder(ws))
return fetch(webserviceUrlBuilder(ws), options)
}
}

export const pipelineAPI = {
fetchScriptDetails: (s: Script) =>
createPipelineFetchFunction(
() => s.href,
(text) => scriptXmlToJson(text)
),
fetchScripts: () =>
createPipelineFetchFunction(
(ws) => `${baseurl(ws)}/scripts`,
(text) => scriptsXmlToJson(text)
),
fetchJobs: () =>
createPipelineFetchFunction(
(ws) => `${baseurl(ws)}/jobs`,
(text) => jobsXmlToJson(text)
),
fetchJobData: (j: Job) =>
createPipelineFetchFunction(
() => j.jobData.href,
(text) => {
return jobXmlToJson(text)
}
),
launchJob: (j: Job) =>
createPipelineFetchFunction(
(ws) => `${baseurl(ws)}/jobs`,
(text) => jobResponseXmlToJson(text),
{
method: 'POST',
body: jobRequestToXml({
...j.jobRequest,
nicename:
j.jobRequest.nicename || j.jobData.nicename || 'Job',
}),
}
),
deleteJob: (j: Job) =>
createPipelineRequestFunction(() => j.jobData.href, {
method: 'DELETE',
}),
fetchResult: (r: ResultFile | NamedResult) => () =>
fetch(r.href)
.then((response) => response.blob())
.then((blob) => blob.arrayBuffer()),
fetchDatatypeDetails: (d: Datatype) =>
createPipelineFetchFunction(
() => d.href,
(text) => datatypeXmlToJson(d.href, d.id, text)
),
fetchDatatypes: () =>
createPipelineFetchFunction(
(ws) => `${baseurl(ws)}/datatypes`,
(text) => datatypesXmlToJson(text)
),
fetchAlive: () =>
createPipelineFetchFunction(
(ws) => `${baseurl(ws)}/alive`,
(text) => aliveXmlToJson(text)
),
fetchTtsVoices: () =>
createPipelineFetchFunction(
(ws) => `${baseurl(ws)}/voices`,
(text) => voicesToJson(text),
{
method: 'POST',
body: ttsConfigToXml(selectTtsConfig(store.getState())),
}
),
}
export const pipelineAPI = new PipelineAPI(fetch, info)
5 changes: 4 additions & 1 deletion src/main/data/middlewares/pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import {
selectDownloadPath,
selectPipelineProperties,
selectSettings,
selectTtsConfig,
} from 'shared/data/slices/settings'
import { ParserException } from 'shared/parser/pipelineXmlConverter/parser'
import { PipelineInstance } from 'main/factories'
Expand Down Expand Up @@ -316,7 +317,9 @@ export function pipelineMiddleware({ getState, dispatch }) {
})
.then((datatypes) => {
dispatch(setDatatypes(datatypes))
return pipelineAPI.fetchTtsVoices()(newWebservice)
return pipelineAPI.fetchTtsVoices(
selectTtsConfig(getState())
)(newWebservice)
})
.then((voices: Array<TtsVoice>) => {
// console.log('TTS Voices', voices)
Expand Down
12 changes: 8 additions & 4 deletions src/main/data/middlewares/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@ import { info } from 'electron-log'
import { existsSync, readFileSync, writeFile } from 'fs'
import { resolve } from 'path'
import { ENVIRONMENT } from 'shared/constants'
import { save, setAutoCheckUpdate } from 'shared/data/slices/settings'
import {
save,
selectTtsConfig,
setAutoCheckUpdate,
} from 'shared/data/slices/settings'
import { checkForUpdate } from 'shared/data/slices/update'
import { ttsConfigToXml } from 'shared/parser/pipelineXmlConverter/ttsConfigToXml'
import { ApplicationSettings, TtsVoice } from 'shared/types'
import { RootState } from 'shared/types/store'
import { resolveUnpacked } from 'shared/utils'
import { fileURLToPath, pathToFileURL } from 'url'
import { pipelineAPI } from '../apis/pipeline'
import { setTtsVoices } from 'shared/data/slices/pipeline'
import { selectWebservice, setTtsVoices } from 'shared/data/slices/pipeline'

const settingsFile = resolve(app.getPath('userData'), 'settings.json')

Expand Down Expand Up @@ -154,8 +158,8 @@ export function settingsMiddleware({ getState, dispatch }) {
)
// re-fetch the /voices endpoint
pipelineAPI
.fetchTtsVoices()(
(getState() as RootState).pipeline.webservice
.fetchTtsVoices(selectTtsConfig(getState()))(
selectWebservice(getState())
)
.then((voices: Array<TtsVoice>) => {
console.log('TTS Voices', voices)
Expand Down
Loading

0 comments on commit f328155

Please sign in to comment.