/* *Strapi Client *Package: gitlab.hexacode.org/go-libs/strapi *Maintainer: Azzis Arswendo * *Copyright (C) 2023 Hexacode Teknologi Indonesia *All Rights Reserved */ package strapi import ( "errors" "fmt" "io" "net/http" "gitlab.hexacode.org/go-libs/hctypes" ) func (strapi *StrapiClassic) base(method, url string, body io.Reader) (int, hctypes.Dict, error) { req, err := http.NewRequest(method, url, body) if err != nil { return 500, hctypes.Dict{ "error": hctypes.Dict{ "message": "Internal server error", "status": 500, }, }, err } req.Header.Set("Accept", "*/*") req.Header.Set("Content-Type", "application/json") req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", strapi.token)) client := &http.Client{} resp, err := client.Do(req) if err != nil { return 500, hctypes.Dict{ "error": hctypes.Dict{ "message": "Internal server error", "status": 500, }, }, err } defer resp.Body.Close() bytes, err := io.ReadAll(resp.Body) if err != nil { return 500, hctypes.Dict{ "error": hctypes.Dict{ "message": "Internal server error", "status": 500, }, }, err } data := hctypes.NewDictFromJson(hctypes.Buffer(bytes)) return resp.StatusCode, data, nil } func (strapi *StrapiClassic) baseBuffer(method, url string, body io.Reader) (int, hctypes.Buffer, error) { req, err := http.NewRequest(method, url, body) if err != nil { return 500, hctypes.Dict{ "error": hctypes.Dict{ "message": "Internal server error", "status": 500, }, }.ToJson(), err } req.Header.Set("Accept", "*/*") req.Header.Set("Content-Type", "application/json") req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", strapi.token)) client := &http.Client{} resp, err := client.Do(req) if err != nil { return 500, hctypes.Dict{ "error": hctypes.Dict{ "message": "Internal server error", "status": 500, }, }.ToJson(), err } defer resp.Body.Close() bytes, err := io.ReadAll(resp.Body) if err != nil { return 500, hctypes.Dict{ "error": hctypes.Dict{ "message": "Internal server error", "status": 500, }, }.ToJson(), err } return resp.StatusCode, hctypes.Buffer(bytes), nil } func (strapi *StrapiClassic) baseList(method, url string, body io.Reader) (int, hctypes.List, error) { req, err := http.NewRequest(method, url, body) if err != nil { return 500, hctypes.List{ hctypes.Dict{ "error": hctypes.Dict{ "message": "Internal server error", "status": 500, }, }, }, err } req.Header.Set("Accept", "*/*") req.Header.Set("Content-Type", "application/json") req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", strapi.token)) client := &http.Client{} resp, err := client.Do(req) if err != nil { return 500, hctypes.List{ hctypes.Dict{ "error": hctypes.Dict{ "message": "Internal server error", "status": 500, }, }, }, err } defer resp.Body.Close() bytes, err := io.ReadAll(resp.Body) if err != nil { return 500, hctypes.List{ hctypes.Dict{ "error": hctypes.Dict{ "message": "Internal server error", "status": 500, }, }, }, err } data := hctypes.NewListFromJson(hctypes.Buffer(bytes)) return resp.StatusCode, data, nil } func (strapi *StrapiClassic) basePublic(method, url string, body io.Reader) (int, hctypes.Dict, error) { req, err := http.NewRequest(method, url, body) if err != nil { return 500, hctypes.Dict{ "error": hctypes.Dict{ "message": "Internal server error", "status": 500, }, }, err } req.Header.Set("Accept", "*/*") req.Header.Set("Content-Type", "application/json") client := &http.Client{} resp, err := client.Do(req) if err != nil { return 500, hctypes.Dict{ "error": hctypes.Dict{ "message": "Internal server error", "status": 500, }, }, err } defer resp.Body.Close() bytes, err := io.ReadAll(resp.Body) if err != nil { return 500, hctypes.Dict{ "error": hctypes.Dict{ "message": "Internal server error", "status": 500, }, }, err } data := hctypes.NewDictFromJson(hctypes.Buffer(bytes)) return resp.StatusCode, data, nil } func (strapi *StrapiClassic) basePublicBuffer(method, url string, body io.Reader) (int, hctypes.Buffer, error) { req, err := http.NewRequest(method, url, body) if err != nil { return 500, hctypes.Dict{ "error": hctypes.Dict{ "message": "Internal server error", "status": 500, }, }.ToJson(), err } req.Header.Set("Accept", "*/*") req.Header.Set("Content-Type", "application/json") client := &http.Client{} resp, err := client.Do(req) if err != nil { return 500, hctypes.Dict{ "error": hctypes.Dict{ "message": "Internal server error", "status": 500, }, }.ToJson(), err } defer resp.Body.Close() bytes, err := io.ReadAll(resp.Body) if err != nil { return 500, hctypes.Dict{ "error": hctypes.Dict{ "message": "Internal server error", "status": 500, }, }.ToJson(), err } return resp.StatusCode, hctypes.Buffer(bytes), nil } func (strapi *StrapiClassic) basePublicList(method, url string, body io.Reader) (int, hctypes.List, error) { req, err := http.NewRequest(method, url, body) if err != nil { return 500, hctypes.List{ hctypes.Dict{ "error": hctypes.Dict{ "message": "Internal server error", "status": 500, }, }, }, err } req.Header.Set("Accept", "*/*") req.Header.Set("Content-Type", "application/json") client := &http.Client{} resp, err := client.Do(req) if err != nil { return 500, hctypes.List{ hctypes.Dict{ "error": hctypes.Dict{ "message": "Internal server error", "status": 500, }, }, }, err } defer resp.Body.Close() bytes, err := io.ReadAll(resp.Body) if err != nil { return 500, hctypes.List{ hctypes.Dict{ "error": hctypes.Dict{ "message": "Internal server error", "status": 500, }, }, }, err } data := hctypes.NewListFromJson(hctypes.Buffer(bytes)) return resp.StatusCode, data, nil } func (strapi *StrapiClassic) baseCustomHeader(method, url string, headers hctypes.Dict, body io.Reader) (int, hctypes.Dict, error) { req, err := http.NewRequest(method, url, body) if err != nil { return 500, hctypes.Dict{ "error": hctypes.Dict{ "message": "Internal server error", "status": 500, }, }, err } req.Header.Set("Accept", "*/*") req.Header.Set("Content-Type", "application/json") req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", strapi.token)) for key, _ := range headers { value, ok := headers.GetString(key) if !ok { return 500, hctypes.Dict{ "error": hctypes.Dict{ "message": "Internal server error", "status": 500, }, }, errors.New("header value must string") } req.Header.Set(key, string(value)) } client := &http.Client{} resp, err := client.Do(req) if err != nil { return 500, hctypes.Dict{ "error": hctypes.Dict{ "message": "Internal server error", "status": 500, }, }, err } defer resp.Body.Close() bytes, err := io.ReadAll(resp.Body) if err != nil { return 500, hctypes.Dict{ "error": hctypes.Dict{ "message": "Internal server error", "status": 500, }, }, err } data := hctypes.NewDictFromJson(hctypes.Buffer(bytes)) return resp.StatusCode, data, nil } func (strapi *StrapiClassic) baseCustomHeaderList(method, url string, headers hctypes.Dict, body io.Reader) (int, hctypes.List, error) { req, err := http.NewRequest(method, url, body) if err != nil { return 500, hctypes.List{ hctypes.Dict{ "error": hctypes.Dict{ "message": "Internal server error", "status": 500, }, }, }, err } req.Header.Set("Accept", "*/*") req.Header.Set("Content-Type", "application/json") req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", strapi.token)) for key, _ := range headers { value, ok := headers.GetString(key) if !ok { return 500, hctypes.List{ hctypes.Dict{ "error": hctypes.Dict{ "message": "Internal server error", "status": 500, }, }, }, errors.New("header value must string") } req.Header.Set(key, string(value)) } client := &http.Client{} resp, err := client.Do(req) if err != nil { return 500, hctypes.List{ hctypes.Dict{ "error": hctypes.Dict{ "message": "Internal server error", "status": 500, }, }, }, err } defer resp.Body.Close() bytes, err := io.ReadAll(resp.Body) if err != nil { return 500, hctypes.List{ hctypes.Dict{ "error": hctypes.Dict{ "message": "Internal server error", "status": 500, }, }, }, err } data := hctypes.NewListFromJson(hctypes.Buffer(bytes)) return resp.StatusCode, data, nil }