github.com/FusionAuth/go-client@v0.0.0-20240425220342-2317e10dfcf5/README.md (about)

     1  ## FusionAuth Go Client ![semver 2.0.0 compliant](http://img.shields.io/badge/semver-2.0.0-brightgreen.svg?style=flat-square) [![Documentation](https://godoc.org/github.com/FusionAuth/go-client?status.svg)](http://godoc.org/github.com/FusionAuth/go-client/pkg/fusionauth)
     2  
     3  
     4  Use this client to access the FusionAuth APIs in your Go application. For additional information and documentation on FusionAuth refer to [https://fusionauth.io](https://fusionauth.io).
     5  
     6  ## Credits
     7  - [@medhir](https://github.com/medhir) Thank you for the initial commit and initial implementation of the Go client!
     8  - [@markschmid](https://github.com/markschmid) Thank you for your PRs, feedback and suggestions! 
     9  - [@MCBrandenburg](https://github.com/MCBrandenburg) Thank you for the feedback and PRs!
    10  - [@matthewhartstonge](https://github.com/matthewhartstonge) Thank you for the PR!
    11  - The FusionAuth team - couldn't have done it without you!
    12  
    13  ## Installation
    14  
    15  ```
    16  go get github.com/FusionAuth/go-client/pkg/fusionauth
    17  ```
    18  
    19  ## Example Usage
    20  
    21  The following example uses the FusionAuth Go client to create a request handling function that logs in a user: 
    22  ```go
    23  package example
    24  
    25  import (
    26      "encoding/json"
    27      "net/http"
    28      "net/url"
    29      "time"
    30      
    31      "github.com/FusionAuth/go-client/pkg/fusionauth"
    32  )
    33  
    34  const host = "http://localhost:9011"
    35  
    36  var apiKey = "YOUR_FUSIONAUTH_API_KEY"
    37  var httpClient = &http.Client{
    38  	Timeout: time.Second * 10,
    39  }
    40  
    41  var baseURL, _ = url.Parse(host)
    42  
    43  // Construct a new FusionAuth Client
    44  var auth = fusionauth.NewClient(httpClient, baseURL, apiKey)
    45  
    46  // Login logs in the user using the FusionAuth Go client library
    47  func Login() http.HandlerFunc {
    48      return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    49          // Read response body
    50          var credentials fusionauth.LoginRequest
    51          defer r.Body.Close()
    52          json.NewDecoder(r.Body).Decode(&credentials)
    53          // Use FusionAuth Go client to log in the user
    54          authResponse, errors, err := auth.Login(credentials)
    55          if err != nil {
    56              http.Error(w, err.Error(), http.StatusBadRequest)
    57              return
    58          }
    59          // Write the response from the FusionAuth client as JSON
    60          var responseJSON []byte
    61          if errors != nil {
    62              responseJSON, err = json.Marshal(errors)
    63          } else {
    64              responseJSON, err = json.Marshal(authResponse)
    65          }
    66          if err != nil {
    67              http.Error(w, err.Error(), http.StatusInternalServerError)
    68              return
    69          }
    70          w.Header().Set("Content-Type", "application/json")
    71          w.WriteHeader(http.StatusOK)
    72          w.Write(responseJSON)
    73      })
    74  }
    75  ```
    76  
    77  You can also call the API directly without logging a user in. This code uses an API key to determine the number of tenants in a FusionAuth installation.
    78  
    79  ```
    80  package main
    81  
    82  import (
    83      "net/http"
    84      "net/url"
    85      "time"
    86      "fmt"
    87      
    88      "github.com/FusionAuth/go-client/pkg/fusionauth"
    89  )
    90  
    91  const host = "http://localhost:9011"
    92  
    93  var apiKey = "API KEY"
    94  var httpClient = &http.Client{
    95      Timeout: time.Second * 10,
    96  }
    97  
    98  func main() {
    99      var baseURL, _ = url.Parse(host)
   100  
   101      // Construct a new FusionAuth Client
   102      var client = fusionauth.NewClient(httpClient, baseURL, apiKey)
   103      
   104      // for production code, don't ignore the error!
   105      tenantResponse, _ := client.RetrieveTenants()
   106      
   107      fmt.Print(len(tenantResponse.Tenants))
   108  }
   109  ```
   110  
   111  ## Testing source builds
   112  
   113  If you are modifying the go client and want to test it locally, follow these steps:
   114  
   115  go to directory where you have go code checked out.
   116  
   117  ```
   118  mkdir test2
   119  cd test2
   120  go mod init example.com/test/fusionauth
   121  go mod tidy
   122  vi test.go # put in your test code, in the main package
   123  ```
   124  
   125  Then edit `go.mod` and add these lines at the bottom:
   126  
   127  ```
   128  require (
   129          github.com/FusionAuth/go-client v1.0.0
   130  )
   131  
   132  replace (
   133          github.com/FusionAuth/go-client v1.0.0 => ../go-client
   134  )
   135  ```
   136  
   137  Then you can run it: `go run test.go # or go build`
   138  
   139  HT https://levelup.gitconnected.com/import-and-use-local-packages-in-your-go-application-885c35e5624 for these.
   140  
   141  ## Questions and support
   142  
   143  If you have a question or support issue regarding this client library, we'd love to hear from you.
   144  
   145  If you have a paid edition with support included, please [open a ticket in your account portal](https://account.fusionauth.io/account/support/). Learn more about [paid editions here](https://fusionauth.io/pricing).
   146  
   147  Otherwise, please [post your question in the community forum](https://fusionauth.io/community/forum/).
   148  
   149  ## Contributing
   150  
   151  Bug reports and pull requests are welcome on GitHub at https://github.com/FusionAuth/go-client.
   152  
   153  If you find an issue with syntax, etc - this is likely a bug in the template. Feel free to submit a PR against the Client Builder project.
   154  - [Client Builder](https://github.com/FusionAuth/fusionauth-client-builder)
   155  - [go.client.ftl](https://github.com/FusionAuth/fusionauth-client-builder/blob/master/src/main/client/go.client.ftl)
   156  - [go.domain.ftl](https://github.com/FusionAuth/fusionauth-client-builder/blob/master/src/main/client/go.domain.ftl)
   157  
   158  ## License
   159  
   160  The code is available as open source under the terms of the [Apache v2.0 License](https://opensource.org/licenses/Apache-2.0).