Просмотр исходного кода

Merge branch 'master' of https://devel.mephi.ru/dyokunev/go-sdapi

Dmitry Yu Okunev лет назад: 6
Родитель
Сommit
b4ff824d34
3 измененных файлов с 56 добавлено и 0 удалено
  1. 5 0
      README.md
  2. 49 0
      sdApi1/sdApi1.go
  3. 2 0
      sdApi4/sdApi4.go

+ 5 - 0
README.md

@@ -0,0 +1,5 @@
+Реализация API https://sd.mephi.ru/api/.
+
+Данный сервер используется для интеграции различных информационных систем. Документация по доступным API доступна по ссылке:
+
+* https://sd.mephi.ru/dictionary/

+ 49 - 0
sdApi1/sdApi1.go

@@ -0,0 +1,49 @@
+package sdApi1
+
+import (
+	"bytes"
+	"encoding/json"
+	"fmt"
+	"net/http"
+	models "devel.mephi.ru/dyokunev/go-asu-models"
+)
+
+var (
+	apiKey string
+)
+
+func SetApiKey(newApiKey string) {
+	apiKey = newApiKey
+}
+
+func GetUnits() (models.Units, error) {
+	client := &http.Client{}
+
+	req, _ := http.NewRequest("GET", "https://sd.mephi.ru/api/1/units.json", nil)
+	q := req.URL.Query()
+	q.Add("api_key", apiKey)
+	req.URL.RawQuery = q.Encode()
+	response, err := client.Do(req)
+	if err != nil {
+		return nil, err
+	}
+	defer response.Body.Close()
+
+	var bytesArray []byte
+
+	buf := bytes.NewBuffer(bytesArray)
+	_, err = buf.ReadFrom(response.Body)
+	if err != nil {
+		return nil, err
+	}
+
+	var units models.Units
+	if err := json.Unmarshal(buf.Bytes(), &units); err != nil {
+		return nil, fmt.Errorf("Cannot parse: %v: \"%v\"", err.Error(), string(buf.Bytes()));
+	}
+
+	units.CalculateTree()
+
+	return units, nil
+}
+

+ 2 - 0
sdApi4/sdApi4.go

@@ -10,6 +10,7 @@ var (
 	apiKey string
 )
 
+// example is here: https://devel.mephi.ru/dyokunev/cps-api/src/bf961a7c6732fd3bc146d260b22848d4263ebe3f/app/init.go#L302
 func SetApiKey(newApiKey string) {
 	apiKey = newApiKey
 }
@@ -39,6 +40,7 @@ func getPhoto(parameterName, parameterValue string) ([]byte, error) {
 	return buf.Bytes(), nil
 }
 
+// example is here: https://devel.mephi.ru/dyokunev/cps-api/src/bf961a7c6732fd3bc146d260b22848d4263ebe3f/app/controllers/people.go#L92
 func GetPhoto(personId int) ([]byte, error) {
 	return getPhoto("person_id", strconv.Itoa(personId))
 }