tm.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. app.login()
  34. return app.GetTMs(language)
  35. }
  36. var results []TM
  37. jsonDecoder(resp.Body, &results)
  38. return results
  39. }