tm.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. // GetQuery sends GET query and returns response.
  13. func GetQuery(url string) *http.Response {
  14. resp, err := http.Get(url)
  15. if err != nil {
  16. log.Printf("Error getting query: %v", err)
  17. }
  18. return resp
  19. }
  20. // GetTMs returns list of TMs for given target language.
  21. func (app *Application) GetTMs(language string) []TM {
  22. tmURL := app.BaseURL + "tms/"
  23. var queryURL string
  24. if language == "" {
  25. queryURL = tmURL + app.AuthString
  26. } else {
  27. queryURL = tmURL + app.AuthString + "&targetLang=" + language
  28. }
  29. resp := GetQuery(queryURL)
  30. defer resp.Body.Close()
  31. var results []TM
  32. if resp.StatusCode == 401 {
  33. time.Sleep(app.Delay)
  34. status, err := app.login()
  35. if !status || err != nil {
  36. log.Printf("Couldn't log in: %s", err)
  37. return results
  38. }
  39. return app.GetTMs(language)
  40. }
  41. jsonDecoder(resp.Body, &results)
  42. return results
  43. }