getDoc<T extends FrappeDocument> method

  1. @override
Future<RequestResponse<T?>> getDoc<T extends FrappeDocument>(
  1. T obj,
  2. String docName, {
  3. bool? forceFetch = false,
})
override

Returns the document specified by its docName.

By default, will return the document if available locally in locals, otherwise it's fetched from the backend.

If fetched from the backend, the document is added to locals using addToLocals.

If forceFetch is true, the document will be fetched from the backend even if it exists locally.

Throws EmptyDoctypeError if the doctype field is not set within the model class.

Implementation

@override
Future<RequestResponse<T?>> getDoc<T extends FrappeDocument>(
    T obj, String docName,
    {bool? forceFetch = false}) async {
  await getFrappe()
      .checkAppInstalled(features: ['getDoc'], throwError: false);

  EmptyDoctypeError.verify(obj.doctype);
  EmptyDocNameError.verify(docName);

  final cachedDoc = getDocFromCache<T>(obj.doctype, docName);

  if (cachedDoc != null && !forceFetch!) {
    return RequestResponse.success(cachedDoc);
  }

  RequestResponse<FrappeResponse?> response;
  // Default URL
  var url =
      '${config.hostUrl}/api/resource/${Uri.encodeComponent(obj.doctype!)}/${Uri.encodeComponent(docName)}';
  if (getFrappe().getAppsVersion('renovation_core') != null) {
    unawaited(config.coreInstance.meta.getDocMeta(doctype: obj.doctype));
    url =
        '${config.hostUrl}/api/method/renovation/doc/${Uri.encodeComponent(obj.doctype!)}/${Uri.encodeComponent(docName)}';
  }

  response = await Request.initiateRequest(
      url: url, method: HttpMethod.GET, isFrappeResponse: false);
  if (response.isSuccess) {
    final dynamic responseObj = response.data!.message;
    if (responseObj != null && responseObj != 'failed') {
      final temp = obj.fromJson<T>(responseObj);
      temp.rawResponse = responseObj;

      addToLocals(temp);
      return RequestResponse.success(temp, rawResponse: response.rawResponse);
    }
    response.isSuccess = false;
    return RequestResponse.fail(handleError(
        'get_doc',
        response.error ??
            ErrorDetail(
                info: Information(
                    data: response.data,
                    httpCode: response.httpCode,
                    rawResponse: response.rawResponse)
                  ..rawError = response.error?.info?.rawError)));
  }

  return RequestResponse.fail(handleError('get_doc', response.error));
}