/* *Strapi Client *Package: gitlab.hexacode.org/go-libs/strapi *Maintainer: Azzis Arswendo * *Copyright (C) 2023 Hexacode Teknologi Indonesia *All Rights Reserved */ package strapi import ( "bytes" "fmt" "mime/multipart" "net/http" "strconv" "gitlab.hexacode.org/go-libs/hctypes" ) type StrapiClassic struct { url string token string } // NewStrapiWithToken creates a new instance of StrapiClassic with the given URL and token. // // Parameters: // url string - the URL for the Strapi instance. // token string - the token for authentication. // Returns: // *StrapiClassic - a pointer to the newly created StrapiClassic instance. func NewStrapiWithToken(url, token string) *StrapiClassic { strapi := StrapiClassic{ url: url, token: token, } return &strapi } // NewStrapiWithLogin initializes a new StrapiClassic instance with login credentials. // // Parameters: // url string - The URL of the Strapi instance. // identifier string - The identifier for login. // password string - The password for login. // Return: // *StrapiClassic - The initialized StrapiClassic instance. // error - An error, if any. func NewStrapiWithLogin(url, identifier, password string) (*StrapiClassic, error) { strapi := &StrapiClassic{ url: url, } status, data, err := strapi.Login(identifier, password) if err != nil { return nil, err } if status != http.StatusOK { if data.ContainsKey("error") { err2 := data["error"].(hctypes.Dict) if err2.ContainsKey("message") { return nil, fmt.Errorf(string(err2["message"].(hctypes.String))) } } return nil, fmt.Errorf("Internal server error") } if !data.ContainsKey("jwt") { return nil, fmt.Errorf("Internal server error") } strapi.token = string(data["jwt"].(hctypes.String)) return strapi, nil } func (strapi *StrapiClassic) GetToken() string { return strapi.token } // FindOne description of the Go function. // // path string, id int64 // int, hctypes.Dict, error func (strapi *StrapiClassic) FindOne(path string, id int64) (int, hctypes.Dict, error) { return strapi.base("GET", strapi.urlParam(path, id, nil), bytes.NewReader([]byte{})) } // Find finds the specified resource. // // path - the path of the resource. // params - a dictionary of parameters. // Returns int, Dict, error. func (strapi *StrapiClassic) Find(path string, params hctypes.Dict) (int, hctypes.Dict, error) { m_params := hctypes.Dict{} if params != nil { m_params = params.Copy() } return strapi.base("GET", strapi.urlParam(path, 0, m_params), bytes.NewReader([]byte{})) } // Create creates a new resource at the specified path. // // path string, data hctypes.Dict // int, hctypes.Dict, error func (strapi *StrapiClassic) Create(path string, data hctypes.Dict) (int, hctypes.Dict, error) { m_data := hctypes.Dict{} if data != nil { m_data = data.Copy() } return strapi.base("POST", strapi.urlParam(path, 0, nil), bytes.NewReader(([]byte)(hctypes.Dict{ "data": m_data, }.Copy().ToJson()))) } // CreateBase creates a new resource at the specified path with the given data. // // Parameters: // - path: the path of the resource. // - data: a dictionary of data to be created. // // Returns: // - int: the status code of the response. // - hctypes.Dict: the response data. // - error: an error, if any. func (strapi *StrapiClassic) CreateBase(path string, data hctypes.Dict) (int, hctypes.Dict, error) { m_data := hctypes.Dict{} if data != nil { m_data = data.Copy() } return strapi.base("POST", strapi.urlParam(path, 0, nil), bytes.NewReader(([]byte)(m_data.Copy().ToJson()))) } // Update updates the specified resource at the given path with the provided data. // // Parameters: // // path string - the path of the resource // id int64 - the id of the resource // data hctypes.Dict - the data to be updated // // Returns: // // int - the status code // hctypes.Dict - the updated data // error - an error, if any func (strapi *StrapiClassic) Update(path string, id int64, data hctypes.Dict) (int, hctypes.Dict, error) { m_data := hctypes.Dict{} if data != nil { m_data = data.Copy() } return strapi.base("PUT", strapi.urlParam(path, id, nil), bytes.NewReader(([]byte)(hctypes.Dict{ "data": m_data, }.Copy().ToJson()))) } // UpdateBase updates a resource at the specified path with the provided data. // // Parameters: // - path: the path of the resource to be updated. // - id: the ID of the resource to be updated. // - data: the data to update the resource with. // // Returns: // - int: the status code of the response. // - hctypes.Dict: the updated data. // - error: an error, if any. func (strapi *StrapiClassic) UpdateBase(path string, id int64, data hctypes.Dict) (int, hctypes.Dict, error) { m_data := hctypes.Dict{} if data != nil { m_data = data.Copy() } return strapi.base("PUT", strapi.urlParam(path, id, nil), bytes.NewReader(([]byte)(m_data.Copy().ToJson()))) } // Delete deletes an item at the specified path and ID. // // path: a string representing the path // id: an int64 representing the ID // Returns an int, hctypes.Dict, and an error func (strapi *StrapiClassic) Delete(path string, id int64) (int, hctypes.Dict, error) { return strapi.base("DELETE", strapi.urlParam(path, id, nil), bytes.NewReader([]byte{})) } // Login performs a login operation with the given identifier and password. // // Parameters: // identifier string - the identifier for the login // password string - the password for the login // Return type(s): // int - the status code of the response // hctypes.Dict - the response data // error - any error that occurred during the operation func (strapi *StrapiClassic) Login(identifier, password string) (int, hctypes.Dict, error) { return strapi.basePublic("POST", fmt.Sprintf("%s%s", strapi.url, "api/auth/local"), bytes.NewReader(([]byte)(hctypes.Dict{ "identifier": identifier, "password": password, }.Copy().ToJson()))) } // UsersCreate creates a new user with the provided username, email, password, confirmed status, blocked status, and role. // // Parameters: // - username string - the username of the user // - email string - the email of the user // - password string - the password of the user // - confirmed bool - the confirmation status of the user // - blocked bool - the block status of the user // - role int64 - the role of the user // Returns: // - int - the status code of the response // - hctypes.Dict - the response data // - error - an error, if any func (strapi *StrapiClassic) UsersCreate(username string, email string, password string, confirmed bool, blocked bool, role int64) (int, hctypes.Dict, error) { return strapi.base("POST", strapi.urlParam("users", 0, nil), bytes.NewReader(([]byte)(hctypes.Dict{ "username": username, "email": email, "password": password, "confirmed": confirmed, "blocked": blocked, "role": role, }.Copy().ToJson()))) } // UsersFindOne retrieves a single user by their ID. // // Parameters: // - id: the ID of the user to retrieve (int64) // // Returns: // - status code (int) // - user data (hctypes.Dict) // - error (error) func (strapi *StrapiClassic) UsersFindOne(id int64) (int, hctypes.Dict, error) { return strapi.base("GET", strapi.urlParam("users", id, hctypes.Dict{}), bytes.NewReader([]byte{})) } // UsersFind finds all users. // // Returns int, hctypes.List, error. func (strapi *StrapiClassic) UsersFind() (int, hctypes.List, error) { return strapi.baseList("GET", strapi.urlParam("users", 0, hctypes.Dict{}), bytes.NewReader([]byte{})) } // UsersCount retrieves the count of users from the Strapi API. // // It sends a GET request to the "users/count" endpoint and returns the status code, the count of users, and any error encountered. // The status code is an integer representing the HTTP status code of the response. // The count of users is an int64 representing the number of users. // The error is an error object, which is nil if there are no errors. func (strapi *StrapiClassic) UsersCount() (int, int64, error) { status, buf, err := strapi.baseBuffer("GET", strapi.urlParam("users/count", 0, hctypes.Dict{}), bytes.NewReader([]byte{})) if err != nil { return status, -1, err } if status != 200 { return status, -1, err } count, err := strconv.Atoi(string(buf)) if err != nil { return status, -1, err } return status, int64(count), nil } // UsersMe retrieves information about the current user. // // No parameters. // Returns int, hctypes.Dict, error. func (strapi *StrapiClassic) UsersMe() (int, hctypes.Dict, error) { return strapi.base("GET", strapi.urlParam("users/me", 0, hctypes.Dict{}), bytes.NewReader([]byte{})) } // UsersUpdate updates a user with the given ID in the Strapi API. // // Parameters: // - id: an int64 representing the ID of the user to be updated. // - data: a hctypes.Dict representing the data to update the user with. // // Returns: // - int: the status code of the response. // - hctypes.Dict: the updated user data. // - error: an error, if any. func (strapi *StrapiClassic) UsersUpdate(id int64, data hctypes.Dict) (int, hctypes.Dict, error) { m_data := hctypes.Dict{} if data != nil { m_data = data.Copy() } return strapi.base("PUT", strapi.urlParam("users", id, nil), bytes.NewReader(([]byte)(m_data.Copy().ToJson()))) } // UsersDelete deletes a user with the given ID in the Strapi API. // // Parameters: // - id: an int64 representing the ID of the user to be deleted. // // Returns: // - int: the status code of the response. // - hctypes.Dict: the response data. // - error: an error, if any. func (strapi *StrapiClassic) UsersDelete(id int64) (int, hctypes.Dict, error) { return strapi.base("DELETE", strapi.urlParam("users", id, nil), bytes.NewReader([]byte{})) } func (strapi *StrapiClassic) Upload(file_name string, buffer hctypes.Buffer) (int, hctypes.List, error) { data := bytes.NewBuffer([]byte{}) mp := multipart.NewWriter(data) file, err := mp.CreateFormFile("files", file_name) if err != nil { return 500, hctypes.List{ hctypes.Dict{ "error": hctypes.Dict{ "message": "Internal server error", "status": 500, }, }, }, err } file.Write(buffer) mp.Close() header_content_type := mp.FormDataContentType() return strapi.baseCustomHeaderList("POST", strapi.urlParam("upload", 0, nil), hctypes.Dict{ "Content-Type": header_content_type, }.Copy(), data) }