github.com/kaisawind/go-swagger@v0.19.0/docs/faq/faq_client.md (about)

     1  <!-- Questions about client generation -->
     2  ## Client generation
     3  
     4  ### Example for dynamic client?
     5  _Use-case_: I have the swagger spec file for an existing 3rd party REST service for their application.
     6  A dynamic client would allow me to load the swagger spec file and provide the ability to formulate requests and parse responses
     7  based on the loaded spec file.
     8  
     9  For e.g. there are REST client packages for go, like go-resty, that provide a nice interface
    10  to interact with a REST server, but go-resty doesn't understand swagger specs.
    11  
    12  - *Just like the untyped dynamic server example in go-swagger, is there an example for a dynamic client?*
    13  - *Can a REST client be created at runtime by loading a swagger spec file, without going through a code generation and compilation?*
    14  - *Can I get the REST client functionality without first generating a client using go-swagger and then*
    15  compiling it back into the code.
    16  - *Can this be done dynamically like the dynamic server example?*
    17  
    18  **Answer**: **you can't currently**.
    19  
    20  You'd have to do by hand everything the code generator does for you. Every time the API changes, you would have to do this again.
    21  The use case for the server side is covered but not the client side.
    22  
    23  That being said, you might find this test useful as an example:
    24  [runtime test](https://github.com/go-openapi/runtime/blob/master/client/runtime_test.go#L144-L188)
    25  
    26  Originally from issue [#996](https://github.com/go-swagger/go-swagger/issues/996).
    27  
    28  ### Can we set a User-Agent header?
    29  _Use-Case_: we would like to be able to set an arbitrary user-agent header either at client generation time or at compile time.
    30  
    31  *Is it possible to do this?*
    32  
    33  >The Swagger specification is irrelevant in this case because we are using the same specification to generate many clients.
    34  
    35  **Answer**: here is the outline of how to achieve that.
    36  
    37  - You can use a custom transport which allows you to set the user agent.
    38  https://github.com/go-openapi/runtime/blob/master/client/runtime.go#L132
    39  - Then you can configure it with this constructor method
    40  https://github.com/go-swagger/go-swagger/blob/master/examples/todo-list/client/todo_list_client.go#L52
    41  - You can also configure that runtime with a `stdlib http.Client`
    42  https://github.com/go-openapi/runtime/blob/master/client/runtime.go#L167
    43  - You can extend intercept a http request with the `http.RoundTripper interface`. https://godoc.org/net/http#RoundTripper
    44  which you can set here: https://github.com/go-openapi/runtime/blob/master/client/runtime.go#L116
    45  - so for the client here:
    46  
    47  ```golang
    48  var myRoundTripper http.RoundTripper = createRoundTripper()
    49  transport := httptransport.New(cfg.Host, cfg.BasePath, cfg.Schemes)
    50  transport.Transport = myRoundTripper
    51  todoListClient := New(transport, nil)
    52  ```
    53  
    54  _Other use-Case_: can the same pattern of using an `http.RoundTripper` be used to implement the AWS Signature v4 which requires
    55  reading and modifying the `*http.Request` before its sent?
    56  
    57  **Answer**: **yes it can**.
    58  
    59  >The roundtripper is the last thing executed before sending the request on the wire.
    60  
    61  See also issue [#935](https://github.com/go-swagger/go-swagger/issues/935).
    62  
    63  Originally from issue [#911](https://github.com/go-swagger/go-swagger/issues/911).
    64  
    65  -------------
    66  
    67  Back to [all contributions](README.md#all-contributed-questions)