...

Source file src/gitlab.hexacode.org/go-libs/strapi/strapi_classic.go

Documentation: gitlab.hexacode.org/go-libs/strapi

     1  /*
     2   *Strapi Client
     3   *Package: gitlab.hexacode.org/go-libs/strapi
     4   *Maintainer: Azzis Arswendo <azzis@hexacode.org>
     5   *
     6   *Copyright (C) 2023 Hexacode Teknologi Indonesia
     7   *All Rights Reserved
     8   */
     9  
    10  package strapi
    11  
    12  import (
    13  	"bytes"
    14  	"fmt"
    15  	"mime/multipart"
    16  	"net/http"
    17  	"strconv"
    18  
    19  	"gitlab.hexacode.org/go-libs/hctypes"
    20  )
    21  
    22  type StrapiClassic struct {
    23  	url   string
    24  	token string
    25  }
    26  
    27  // NewStrapiWithToken creates a new instance of StrapiClassic with the given URL and token.
    28  //
    29  // Parameters:
    30  // url string - the URL for the Strapi instance.
    31  // token string - the token for authentication.
    32  // Returns:
    33  // *StrapiClassic - a pointer to the newly created StrapiClassic instance.
    34  func NewStrapiWithToken(url, token string) *StrapiClassic {
    35  	strapi := StrapiClassic{
    36  		url:   url,
    37  		token: token,
    38  	}
    39  
    40  	return &strapi
    41  }
    42  
    43  // NewStrapiWithLogin initializes a new StrapiClassic instance with login credentials.
    44  //
    45  // Parameters:
    46  // url string - The URL of the Strapi instance.
    47  // identifier string - The identifier for login.
    48  // password string - The password for login.
    49  // Return:
    50  // *StrapiClassic - The initialized StrapiClassic instance.
    51  // error - An error, if any.
    52  func NewStrapiWithLogin(url, identifier, password string) (*StrapiClassic, error) {
    53  	strapi := &StrapiClassic{
    54  		url: url,
    55  	}
    56  
    57  	status, data, err := strapi.Login(identifier, password)
    58  	if err != nil {
    59  		return nil, err
    60  	}
    61  
    62  	if status != http.StatusOK {
    63  		if data.ContainsKey("error") {
    64  			err2 := data["error"].(hctypes.Dict)
    65  			if err2.ContainsKey("message") {
    66  				return nil, fmt.Errorf(string(err2["message"].(hctypes.String)))
    67  			}
    68  		}
    69  
    70  		return nil, fmt.Errorf("Internal server error")
    71  	}
    72  
    73  	if !data.ContainsKey("jwt") {
    74  		return nil, fmt.Errorf("Internal server error")
    75  	}
    76  
    77  	strapi.token = string(data["jwt"].(hctypes.String))
    78  
    79  	return strapi, nil
    80  }
    81  
    82  func (strapi *StrapiClassic) GetToken() string {
    83  	return strapi.token
    84  }
    85  
    86  // FindOne description of the Go function.
    87  //
    88  // path string, id int64
    89  // int, hctypes.Dict, error
    90  func (strapi *StrapiClassic) FindOne(path string, id int64) (int, hctypes.Dict, error) {
    91  	return strapi.base("GET", strapi.urlParam(path, id, nil), bytes.NewReader([]byte{}))
    92  }
    93  
    94  // Find finds the specified resource.
    95  //
    96  // path - the path of the resource.
    97  // params - a dictionary of parameters.
    98  // Returns int, Dict, error.
    99  func (strapi *StrapiClassic) Find(path string, params hctypes.Dict) (int, hctypes.Dict, error) {
   100  	m_params := hctypes.Dict{}
   101  	if params != nil {
   102  		m_params = params.Copy()
   103  	}
   104  
   105  	return strapi.base("GET", strapi.urlParam(path, 0, m_params), bytes.NewReader([]byte{}))
   106  }
   107  
   108  // Create creates a new resource at the specified path.
   109  //
   110  // path string, data hctypes.Dict
   111  // int, hctypes.Dict, error
   112  func (strapi *StrapiClassic) Create(path string, data hctypes.Dict) (int, hctypes.Dict, error) {
   113  	m_data := hctypes.Dict{}
   114  	if data != nil {
   115  		m_data = data.Copy()
   116  	}
   117  
   118  	return strapi.base("POST", strapi.urlParam(path, 0, nil), bytes.NewReader(([]byte)(hctypes.Dict{
   119  		"data": m_data,
   120  	}.Copy().ToJson())))
   121  }
   122  
   123  // CreateBase creates a new resource at the specified path with the given data.
   124  //
   125  // Parameters:
   126  // - path: the path of the resource.
   127  // - data: a dictionary of data to be created.
   128  //
   129  // Returns:
   130  // - int: the status code of the response.
   131  // - hctypes.Dict: the response data.
   132  // - error: an error, if any.
   133  func (strapi *StrapiClassic) CreateBase(path string, data hctypes.Dict) (int, hctypes.Dict, error) {
   134  	m_data := hctypes.Dict{}
   135  	if data != nil {
   136  		m_data = data.Copy()
   137  	}
   138  
   139  	return strapi.base("POST", strapi.urlParam(path, 0, nil), bytes.NewReader(([]byte)(m_data.Copy().ToJson())))
   140  }
   141  
   142  // Update updates the specified resource at the given path with the provided data.
   143  //
   144  // Parameters:
   145  //
   146  //	path string - the path of the resource
   147  //	id int64 - the id of the resource
   148  //	data hctypes.Dict - the data to be updated
   149  //
   150  // Returns:
   151  //
   152  //	int - the status code
   153  //	hctypes.Dict - the updated data
   154  //	error - an error, if any
   155  func (strapi *StrapiClassic) Update(path string, id int64, data hctypes.Dict) (int, hctypes.Dict, error) {
   156  	m_data := hctypes.Dict{}
   157  	if data != nil {
   158  		m_data = data.Copy()
   159  	}
   160  
   161  	return strapi.base("PUT", strapi.urlParam(path, id, nil), bytes.NewReader(([]byte)(hctypes.Dict{
   162  		"data": m_data,
   163  	}.Copy().ToJson())))
   164  }
   165  
   166  // UpdateBase updates a resource at the specified path with the provided data.
   167  //
   168  // Parameters:
   169  // - path: the path of the resource to be updated.
   170  // - id: the ID of the resource to be updated.
   171  // - data: the data to update the resource with.
   172  //
   173  // Returns:
   174  // - int: the status code of the response.
   175  // - hctypes.Dict: the updated data.
   176  // - error: an error, if any.
   177  func (strapi *StrapiClassic) UpdateBase(path string, id int64, data hctypes.Dict) (int, hctypes.Dict, error) {
   178  	m_data := hctypes.Dict{}
   179  	if data != nil {
   180  		m_data = data.Copy()
   181  	}
   182  
   183  	return strapi.base("PUT", strapi.urlParam(path, id, nil), bytes.NewReader(([]byte)(m_data.Copy().ToJson())))
   184  }
   185  
   186  // Delete deletes an item at the specified path and ID.
   187  //
   188  // path: a string representing the path
   189  // id: an int64 representing the ID
   190  // Returns an int, hctypes.Dict, and an error
   191  func (strapi *StrapiClassic) Delete(path string, id int64) (int, hctypes.Dict, error) {
   192  	return strapi.base("DELETE", strapi.urlParam(path, id, nil), bytes.NewReader([]byte{}))
   193  }
   194  
   195  // Login performs a login operation with the given identifier and password.
   196  //
   197  // Parameters:
   198  // identifier string - the identifier for the login
   199  // password string - the password for the login
   200  // Return type(s):
   201  // int - the status code of the response
   202  // hctypes.Dict - the response data
   203  // error - any error that occurred during the operation
   204  func (strapi *StrapiClassic) Login(identifier, password string) (int, hctypes.Dict, error) {
   205  	return strapi.basePublic("POST", fmt.Sprintf("%s%s", strapi.url, "api/auth/local"), bytes.NewReader(([]byte)(hctypes.Dict{
   206  		"identifier": identifier,
   207  		"password":   password,
   208  	}.Copy().ToJson())))
   209  }
   210  
   211  // UsersCreate creates a new user with the provided username, email, password, confirmed status, blocked status, and role.
   212  //
   213  // Parameters:
   214  // - username string - the username of the user
   215  // - email string - the email of the user
   216  // - password string - the password of the user
   217  // - confirmed bool - the confirmation status of the user
   218  // - blocked bool - the block status of the user
   219  // - role int64 - the role of the user
   220  // Returns:
   221  // - int - the status code of the response
   222  // - hctypes.Dict - the response data
   223  // - error - an error, if any
   224  func (strapi *StrapiClassic) UsersCreate(username string, email string, password string, confirmed bool, blocked bool, role int64) (int, hctypes.Dict, error) {
   225  	return strapi.base("POST", strapi.urlParam("users", 0, nil), bytes.NewReader(([]byte)(hctypes.Dict{
   226  		"username":  username,
   227  		"email":     email,
   228  		"password":  password,
   229  		"confirmed": confirmed,
   230  		"blocked":   blocked,
   231  		"role":      role,
   232  	}.Copy().ToJson())))
   233  }
   234  
   235  // UsersFindOne retrieves a single user by their ID.
   236  //
   237  // Parameters:
   238  // - id: the ID of the user to retrieve (int64)
   239  //
   240  // Returns:
   241  // - status code (int)
   242  // - user data (hctypes.Dict)
   243  // - error (error)
   244  func (strapi *StrapiClassic) UsersFindOne(id int64) (int, hctypes.Dict, error) {
   245  	return strapi.base("GET", strapi.urlParam("users", id, hctypes.Dict{}), bytes.NewReader([]byte{}))
   246  }
   247  
   248  // UsersFind finds all users.
   249  //
   250  // Returns int, hctypes.List, error.
   251  func (strapi *StrapiClassic) UsersFind() (int, hctypes.List, error) {
   252  	return strapi.baseList("GET", strapi.urlParam("users", 0, hctypes.Dict{}), bytes.NewReader([]byte{}))
   253  }
   254  
   255  // UsersCount retrieves the count of users from the Strapi API.
   256  //
   257  // It sends a GET request to the "users/count" endpoint and returns the status code, the count of users, and any error encountered.
   258  // The status code is an integer representing the HTTP status code of the response.
   259  // The count of users is an int64 representing the number of users.
   260  // The error is an error object, which is nil if there are no errors.
   261  func (strapi *StrapiClassic) UsersCount() (int, int64, error) {
   262  	status, buf, err := strapi.baseBuffer("GET", strapi.urlParam("users/count", 0, hctypes.Dict{}), bytes.NewReader([]byte{}))
   263  
   264  	if err != nil {
   265  		return status, -1, err
   266  	}
   267  
   268  	if status != 200 {
   269  		return status, -1, err
   270  	}
   271  
   272  	count, err := strconv.Atoi(string(buf))
   273  	if err != nil {
   274  		return status, -1, err
   275  	}
   276  
   277  	return status, int64(count), nil
   278  }
   279  
   280  // UsersMe retrieves information about the current user.
   281  //
   282  // No parameters.
   283  // Returns int, hctypes.Dict, error.
   284  func (strapi *StrapiClassic) UsersMe() (int, hctypes.Dict, error) {
   285  	return strapi.base("GET", strapi.urlParam("users/me", 0, hctypes.Dict{}), bytes.NewReader([]byte{}))
   286  }
   287  
   288  // UsersUpdate updates a user with the given ID in the Strapi API.
   289  //
   290  // Parameters:
   291  // - id: an int64 representing the ID of the user to be updated.
   292  // - data: a hctypes.Dict representing the data to update the user with.
   293  //
   294  // Returns:
   295  // - int: the status code of the response.
   296  // - hctypes.Dict: the updated user data.
   297  // - error: an error, if any.
   298  func (strapi *StrapiClassic) UsersUpdate(id int64, data hctypes.Dict) (int, hctypes.Dict, error) {
   299  	m_data := hctypes.Dict{}
   300  	if data != nil {
   301  		m_data = data.Copy()
   302  	}
   303  
   304  	return strapi.base("PUT", strapi.urlParam("users", id, nil), bytes.NewReader(([]byte)(m_data.Copy().ToJson())))
   305  }
   306  
   307  // UsersDelete deletes a user with the given ID in the Strapi API.
   308  //
   309  // Parameters:
   310  // - id: an int64 representing the ID of the user to be deleted.
   311  //
   312  // Returns:
   313  // - int: the status code of the response.
   314  // - hctypes.Dict: the response data.
   315  // - error: an error, if any.
   316  func (strapi *StrapiClassic) UsersDelete(id int64) (int, hctypes.Dict, error) {
   317  	return strapi.base("DELETE", strapi.urlParam("users", id, nil), bytes.NewReader([]byte{}))
   318  }
   319  
   320  func (strapi *StrapiClassic) Upload(file_name string, buffer hctypes.Buffer) (int, hctypes.List, error) {
   321  	data := bytes.NewBuffer([]byte{})
   322  	mp := multipart.NewWriter(data)
   323  
   324  	file, err := mp.CreateFormFile("files", file_name)
   325  	if err != nil {
   326  		return 500, hctypes.List{
   327  			hctypes.Dict{
   328  				"error": hctypes.Dict{
   329  					"message": "Internal server error",
   330  					"status":  500,
   331  				},
   332  			},
   333  		}, err
   334  	}
   335  
   336  	file.Write(buffer)
   337  	mp.Close()
   338  
   339  	header_content_type := mp.FormDataContentType()
   340  
   341  	return strapi.baseCustomHeaderList("POST", strapi.urlParam("upload", 0, nil), hctypes.Dict{
   342  		"Content-Type": header_content_type,
   343  	}.Copy(), data)
   344  }
   345  

View as plain text