tm.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package main
  2. import (
  3. "log"
  4. "net/http"
  5. "time"
  6. )
  7. // TM stores information about TM.
  8. type TM struct {
  9. NumEntries, AccessLevel int
  10. Client, Domain, FriendlyName, Project, SourceLangCode, Subject, TMGuid, TMOwner, TargetLangCode string
  11. }
  12. func getQuery(url string) *http.Response {
  13. resp, err := http.Get(url)
  14. if err != nil {
  15. log.Printf("Error getting query: %s", err)
  16. }
  17. return resp
  18. }
  19. func (app *Application) getTMs(language string) []TM {
  20. tmURL := app.BaseURL + "tms/"
  21. var queryURL string
  22. if language == "" {
  23. queryURL = tmURL + app.AuthString
  24. } else {
  25. queryURL = tmURL + app.AuthString + "&targetLang=" + language
  26. }
  27. resp := getQuery(queryURL)
  28. defer resp.Body.Close()
  29. var results []TM
  30. if resp.StatusCode == 401 {
  31. time.Sleep(app.Delay)
  32. status, err := app.login()
  33. if !status || err != nil {
  34. log.Printf("Couldn't log in: %s", err)
  35. return results
  36. }
  37. return app.getTMs(language)
  38. }
  39. jsonDecoder(resp.Body, &results)
  40. return results
  41. }