12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- package discourse
- import (
- "encoding/json"
- "fmt"
- "net/http"
- "strings"
- )
- // AboutResponse - Structure of a discourse user api response
- type AboutResponse struct {
- About About `json:"about"`
- Errors []string `json:"errors"`
- ErrorType string `json:"error_type"`
- }
- // About data for the site
- type About struct {
- Version string `json:"version"`
- }
- // GetAbout - Get a discourse site about
- func GetAbout(config APIConfig) (About, error) {
- url := fmt.Sprintf("%s/about.json", config.Endpoint)
- req, _ := newGetRequest(config, url)
- client := getClient(rl)
- response, err := client.Do(req)
- if err != nil {
- return About{}, fmt.Errorf("Unable to connect to discourse server at %s -- %s", url, err)
- }
- if response.StatusCode == http.StatusOK || response.StatusCode == http.StatusForbidden {
- var result *AboutResponse
- json.NewDecoder(response.Body).Decode(&result)
- if result.ErrorType != "" {
- return About{}, fmt.Errorf("Failed to get discourse version. Error: %s", strings.Join(result.Errors, "; "))
- }
- return result.About, nil
- }
- return About{}, fmt.Errorf("Unexpected response from discourse server at %s -- %s", url, response.Status)
- }
|