Код: Выделить всё
export const webApiConfig: WebApiConfig = {
endpointUrl: process.env.REACT_APP_WEBAPI_ENDPOINT as string,
subscriptionKey: process.env.REACT_APP_WEBAPI_GATEWAY_SUBSCRIPTION_KEY as string,
};
export const trainProjectAsync = async (projectName: string): Promise => {
try {
// Build the request url and config
const url: string = `${webApiConfig.endpointUrl}/Nlu/${projectName}/Train`;
const token = await getToken();
const requestConfig: AxiosRequestConfig = {
headers: {
Authorization: token,
"Content-Type": "application/json",
"Ocp-Apim-Subscription-Key": webApiConfig.subscriptionKey,
},
signal: new AbortController().signal,
};
// Perform the POST request
const response = await axios.post(url, requestConfig);
return response.data satisfies NluTrainingState;
} catch (error) {
// check whether the error is an axios error or a stock error.
if (axios.isAxiosError(error)) {
throw new Error(`API call axios malfunction code: ${error.code} ${JSON.stringify(error.response)}`);
} else {
throw new Error(`API call stock malfunction, reason: ${error}`);
}
}
};
Код: Выделить всё
[Authorize]
[HttpPost("{projectName}/Train")]
[Produces("application/json")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task TrainAsync(string projectName)
{
try
{
// Launch a training job.
NluTrainingStateDto result = await _nluService.TrainAsync(projectName);
// Return the correct status based on whether the request was a success.
if (result != null)
{
return Ok(result);
}
else
{
return NotFound();
}
}
catch (ServiceAccessException serviceEx)
{
// TODO: add logging
IError error = _errorFactory.CreateError(ErrorCode.ControllerNotServiced, _exceptionResourceManager.GetString("ConnectionNotServicedExc")!);
ControllerNotServicedException _controllerEx = new(error, serviceEx);
return StatusCode((int)HttpStatusCode.InternalServerError, $"Internal Server Error: {_controllerEx.Message}");
}
}
EDIT: APIM создается с помощью спецификации OpenAPI через URL-адрес swagger.
Подробнее здесь: https://stackoverflow.com/questions/787 ... larger-set
Мобильная версия