sdApi4.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package sdApi4
  2. import (
  3. "bytes"
  4. "net/http"
  5. "strconv"
  6. )
  7. var (
  8. apiKey string
  9. )
  10. // example is here: https://devel.mephi.ru/dyokunev/cps-api/src/bf961a7c6732fd3bc146d260b22848d4263ebe3f/app/init.go#L302
  11. func SetApiKey(newApiKey string) {
  12. apiKey = newApiKey
  13. }
  14. func getPhoto(parameterName, parameterValue string) ([]byte, error) {
  15. client := &http.Client{}
  16. req, _ := http.NewRequest("GET", "https://sd.mephi.ru/api/4/get_photo.jpeg", nil)
  17. q := req.URL.Query()
  18. q.Add("api_key", apiKey)
  19. q.Add(parameterName, parameterValue)
  20. req.URL.RawQuery = q.Encode()
  21. response, err := client.Do(req)
  22. if err != nil {
  23. return nil, err
  24. }
  25. defer response.Body.Close()
  26. var bytesArray []byte
  27. buf := bytes.NewBuffer(bytesArray)
  28. _, err = buf.ReadFrom(response.Body)
  29. if err != nil {
  30. return nil, err
  31. }
  32. return buf.Bytes(), nil
  33. }
  34. // example is here: https://devel.mephi.ru/dyokunev/cps-api/src/bf961a7c6732fd3bc146d260b22848d4263ebe3f/app/controllers/people.go#L92
  35. func GetPhoto(personId int) ([]byte, error) {
  36. return getPhoto("person_id", strconv.Itoa(personId))
  37. }
  38. func GetPhotoByEmpGUID(empGUID int) ([]byte, error) {
  39. return getPhoto("e_id", strconv.Itoa(empGUID))
  40. }