github.com/newrelic/newrelic-client-go@v1.1.0/templates/nerdgraphclient/client.go.tmpl (about)

     1  // Code generated by tutone: DO NOT EDIT
     2  package {{.PackageName | lower}}
     3  {{$packageName := .PackageName}}
     4  
     5  import(
     6    "fmt"
     7    {{- range .Imports}}
     8    "{{.}}"
     9    {{- end}}
    10  )
    11  
    12  {{range .Mutations}}
    13  {{/*
    14  // TODO The name of the method here could use some love. Perhaps we allow an
    15  // override from the user at config time, so that we are able to replace what
    16  // exists?  Perhps too this is an opporunity for us to use the method prefix as
    17  // some kind of indicator for which package this method should belong to,
    18  // perhaps.
    19  */}}
    20  {{ .Description }}
    21  func (a *{{$packageName|title}}) {{.Name | title}}(
    22    {{- range .Signature.Input}}
    23      {{.Name | untitle}} {{.Type}},
    24    {{- end}}
    25  ) (*{{ .Signature.Return | join ", "}}) {
    26    return a.{{.Name | title}}WithContext(context.Background(),
    27    {{- range .Signature.Input}}
    28      {{.Name | untitle}},
    29    {{- end}}
    30    )
    31  }
    32  
    33  {{ .Description }}
    34  func (a *{{$packageName|title}}) {{.Name | title}}WithContext(
    35    ctx context.Context,
    36    {{- range .Signature.Input}}
    37      {{.Name | untitle}} {{.Type}},
    38    {{- end}}
    39      ) (*{{ .Signature.Return | join ", "}}) {
    40  
    41  	resp := {{.Name}}QueryResponse{}
    42  	vars := map[string]interface{}{
    43    {{- range .QueryVars}}
    44      "{{.Key}}": {{.Value | untitle}},
    45    {{- end}}
    46  	}
    47  
    48  	if err := a.client.NerdGraphQueryWithContext(ctx, {{.Name}}Mutation, vars, &resp); err != nil {
    49  		return nil, err
    50  	}
    51  
    52    {{   if hasField (first .Signature.Return) "Errors" }}
    53    // If we got errors back, wrap them all up
    54    if len(resp.{{first .Signature.Return}}.Errors) > 0 {
    55      errs := fmt.Errorf("query error")
    56      for _, err := range resp.{{first .Signature.Return}}.Errors {
    57        errs = fmt.Errorf("%w; %s", errs, err.Description)
    58      }
    59      return nil, errs
    60    }
    61    {{   end}}
    62  
    63  	return &resp.{{first .Signature.Return}}, nil
    64  }
    65  
    66  {{   if gt (len .QueryVars) 0 }}
    67  type {{.Name}}QueryResponse struct {
    68    {{first .Signature.Return}} {{first .Signature.Return}} `json:"{{.Name}}"`
    69  }
    70  {{   end}}
    71  
    72  const {{.Name}}Mutation = `{{ .QueryString }}`
    73  
    74  {{ end}}
    75  
    76  {{ range .Queries}}
    77  {{ .Description }}
    78  func (a *{{$packageName|title}}) Get{{.Name | title}}(
    79    {{- range .Signature.Input}}
    80      {{.Name | untitle}} {{.Type}},
    81    {{- end}}
    82  ) (*{{ .Signature.Return | join ", "}}) {
    83    return a.Get{{.Name | title}}WithContext(context.Background(),
    84    {{- range .Signature.Input}}
    85      {{.Name | untitle}},
    86    {{- end}}
    87    )
    88  }
    89  
    90  {{ .Description }}
    91  func (a *{{$packageName|title}}) Get{{.Name | title}}WithContext(
    92    ctx context.Context,
    93    {{- range .Signature.Input}}
    94      {{.Name | untitle}} {{.Type}},
    95    {{- end}}
    96  ) (*{{ .Signature.Return | join ", "}}) {
    97  
    98  	resp := {{.ResponseObjectType}}{}
    99  	vars := map[string]interface{}{
   100    {{- range .QueryVars}}
   101      "{{.Key}}": {{.Value | untitle}},
   102    {{- end}}
   103  	}
   104  
   105  	if err := a.client.NerdGraphQueryWithContext(ctx, get{{.Name}}Query, vars, &resp); err != nil {
   106  		return nil, err
   107  	}
   108  
   109    {{ if .Signature.ReturnSlice}}
   110  	if len(resp.{{.Signature.ReturnPath | join "."}}.{{.Name}}) == 0 {
   111  		return nil, errors.NewNotFound("")
   112  	}
   113    {{- end}}
   114  
   115  	return &resp.{{.Signature.ReturnPath | join "."}}.{{.Name}}, nil
   116  }
   117  
   118  const get{{.Name}}Query = `{{ .QueryString }}`
   119  
   120  {{ end}}