tm.go 1.1 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. // 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. if resp.StatusCode == 401 {
  32. time.Sleep(app.Delay)
  33. status, err := app.login()
  34. if !status || err != nil {
  35. log.Fatalf("Couldn't log in: %s", err)
  36. }
  37. return app.GetTMs(language)
  38. }
  39. var results []TM
  40. jsonDecoder(resp.Body, &results)
  41. return results
  42. }