export function adaptClients()

in packages/autorest.go/src/m4togocodemodel/clients.ts [25:81]


export function adaptClients(m4CodeModel: m4.CodeModel, codeModel: go.CodeModel) {
  for (const group of values(m4CodeModel.operationGroups)) {
    const client = adaptClient(codeModel.type, group);

    for (const op of values(group.operations)) {
      const httpPath = <string>op.requests![0].protocol.http!.path;
      const httpMethod = op.requests![0].protocol.http!.method;
      let method: go.Method | go.LROMethod | go.LROPageableMethod | go.PageableMethod;
      const naming = adaptMethodNaming(op);

      if (helpers.isLROOperation(op) && helpers.isPageableOperation(op)) {
        method = new go.LROPageableMethod(op.language.go!.name, client, httpPath, httpMethod, getStatusCodes(op), naming);
        (<go.LROPageableMethod>method).finalStateVia = (op.extensions?.['x-ms-long-running-operation-options']?.['final-state-via']);
        (<go.LROPageableMethod>method).nextLinkName = op.language.go!.paging.nextLinkName;
        if (op.language.go!.paging.nextLinkOperation) {
          // adapt the next link operation
          const nextPageMethod = new go.NextPageMethod(op.language.go!.paging.nextLinkOperation.language.go.name, client, httpPath, httpMethod, getStatusCodes(op.language.go!.paging.nextLinkOperation));
          populateMethod(op.language.go!.paging.nextLinkOperation, nextPageMethod, m4CodeModel, codeModel);
          (<go.LROPageableMethod>method).nextPageMethod = nextPageMethod;
        }
      } else if (helpers.isLROOperation(op)) {
        method = new go.LROMethod(op.language.go!.name, client, httpPath, httpMethod, getStatusCodes(op), naming);
        (<go.LROMethod>method).finalStateVia = (op.extensions?.['x-ms-long-running-operation-options']?.['final-state-via']);
      } else if (helpers.isPageableOperation(op)) {
        if (op.language.go!.paging.isNextOp) {
          continue;
        }
        method = new go.PageableMethod(op.language.go!.name, client, httpPath, httpMethod, getStatusCodes(op), naming);
        (<go.PageableMethod>method).nextLinkName = op.language.go!.paging.nextLinkName;
        if (op.language.go!.paging.nextLinkOperation) {
          // adapt the next link operation
          const nextPageMethod = adaptNextPageMethod(op, m4CodeModel, client, codeModel);
          (<go.PageableMethod>method).nextPageMethod = nextPageMethod;
        }
      } else {
        method = new go.Method(op.language.go!.name, client, httpPath, httpMethod, getStatusCodes(op), naming);
      }

      populateMethod(op, method, m4CodeModel, codeModel);

      client.methods.push(method);
    }

    // if any client parameters were adapted, add them to the client
    if (group.language.go!.clientParams) {
      for (const param of <Array<m4.Parameter>>group.language.go!.clientParams) {
        const adaptedParam = clientParams.get(param.language.go!.name);
        if (!adaptedParam) {
          throw new Error(`missing adapted client parameter ${param.language.go!.name}`);
        }
        client.parameters.push(adaptedParam);
      }
    }

    codeModel.clients.push(client);
  }
}