Skip to content
This repository was archived by the owner on Oct 5, 2020. It is now read-only.

Commit 6417b8d

Browse files
committedJan 9, 2020
Add Sample MetaData to GET endpoints
1 parent fe0c82a commit 6417b8d

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed
 

‎api_mock/go.mod

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
module github.com/lkdevelopment/hetzner-cloud-api-mock
22

3-
go 1.12
3+
go 1.13
44

55
require (
66
github.com/bukalapak/snowboard v1.7.0
77
github.com/cweill/gotests v1.5.3 // indirect
88
github.com/gorilla/mux v1.7.3
99
github.com/gosimple/slug v1.4.2 // indirect
1010
github.com/rainycape/unidecode v0.0.0-20150907023854-cb7f23ec59be // indirect
11-
github.com/stretchr/testify v1.3.0 // indirect
12-
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
13-
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
1411
github.com/xeipuuv/gojsonschema v1.2.0
1512
golang.org/x/tools v0.0.0-20190407030857-0fdf0c73855b
1613
)

‎api_mock/model/transformer.go

+26-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package model
22

33
import (
4+
"encoding/json"
5+
"log"
46
"regexp"
57
"strings"
68

@@ -21,15 +23,37 @@ func (p *Transformer) mapIntoApiRoutes(resources []*api.Resource) (resp []APIRou
2123
Method: method.Method,
2224
Description: method.Title,
2325
Route: re.ReplaceAllString(method.Href.Path, ""),
24-
Response: method.Transactions[0].Response.Body.Body,
26+
Response: p.addSampleMetaDataToResponse(method.Transactions[0].Response.Body.Body, method.Method, re.ReplaceAllString(method.Href.Path, "")),
2527
ResponseCode: method.Transactions[0].Response.StatusCode,
2628
JsonSchema: method.Transactions[0].Request.Schema.Body,
2729
})
2830
}
2931
}
3032
return
3133
}
32-
34+
func (p *Transformer) addSampleMetaDataToResponse(body string, method string, path string) string {
35+
if method == "GET" && strings.Count(path, "/") == 1 {
36+
var result map[string]interface{}
37+
json.Unmarshal([]byte(body), &result)
38+
for key, value := range result {
39+
// Each value is an interface{} type, that is type asserted as a string
40+
log.Println(key, value)
41+
}
42+
samplePagination := make(map[string]string)
43+
samplePagination["page"] = "1"
44+
samplePagination["per_page"] = "25"
45+
samplePagination["previous_page"] = "1"
46+
samplePagination["next_page"] = "1"
47+
samplePagination["last_page"] = "1"
48+
samplePagination["total_entries"] = "100"
49+
meta := make(map[string]map[string]string)
50+
meta["pagination"] = samplePagination
51+
result["meta"] = meta
52+
data, _ := json.Marshal(result)
53+
return string(data)
54+
}
55+
return body
56+
}
3357
func (p *Transformer) groupIntoAPIHandler(routes []APIRoute) map[string]APIHandler {
3458
handler := make(map[string]APIHandler)
3559
for _, route := range routes {

0 commit comments

Comments
 (0)