Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
vcraescu committed Jan 7, 2024
0 parents commit 06e69bd
Show file tree
Hide file tree
Showing 27 changed files with 1,753 additions and 0 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# This workflow will build a golang project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go

name: Go

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

jobs:

build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.21

- name: Test
run: make test
29 changes: 29 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
### Go template
# If you prefer the allow list template instead of the deny list, see community template:
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
#
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out
.env*
!.env.dist

# Dependency directories (remove the comment below to include it)
# vendor/

# Go workspace file
go.work
go.work.sum
creds/
data/
.serverless
bin/
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.PHONY: test

test:
go test ./...
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# OblioAPI ![](https://github.com/vcraescu/go-oblio-api/actions/workflows/go.yml/badge.svg)

### How to run the tests

`make test`
55 changes: 55 additions & 0 deletions api_auth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package oblio

import (
"context"
"fmt"
"net/http"
)

type generateAuthorizeTokenRequest struct {
ClientID string `json:"client_id,omitempty"`
ClientSecret string `json:"client_secret,omitempty"`
}

func (r *generateAuthorizeTokenRequest) Validate() error {
if r.ClientID == "" {
return fmt.Errorf("clientID is empty: %w", ErrInvalidArgument)
}

if r.ClientSecret == "" {
return fmt.Errorf("clientSecret is empty: %w", ErrInvalidArgument)
}

return nil
}

type GenerateAuthorizeTokenResponse struct {
AccessToken string `json:"access_token,omitempty"`
ExpiresIn string `json:"expires_in,omitempty"`
TokenType string `json:"token_type,omitempty"`
Scope string `json:"scope,omitempty"`
RequestTime string `json:"request_time,omitempty"`
}

func (c *Client) GenerateAuthorizeToken(ctx context.Context) (*GenerateAuthorizeTokenResponse, error) {
in := generateAuthorizeTokenRequest{
ClientID: c.clientID,
ClientSecret: c.clientSecret,
}

if err := in.Validate(); err != nil {
return nil, err
}

builder := c.requestBuilder.
WithMethod(http.MethodPost).
WithPath("/authorize/token").
WithBody(in)
out := &GenerateAuthorizeTokenResponse{}

if err := c.do(ctx, builder, out); err != nil {
return out, fmt.Errorf("do: %w", err)
}

return out, nil
}
59 changes: 59 additions & 0 deletions api_auth_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package oblio_test

import (
"context"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/vcraescu/go-oblio-api"
"testing"
)

func TestClient_GenerateAuthorizeToken(t *testing.T) {
t.Parallel()

type fields struct {
clientID string
clientSecret string
}

tests := []struct {
name string
fields fields
want *oblio.GenerateAuthorizeTokenResponse
wantErr assert.ErrorAssertionFunc
}{
{
name: "success",
fields: fields{
clientID: clientID,
clientSecret: clientSecret,
},
want: &oblio.GenerateAuthorizeTokenResponse{
AccessToken: accessToken,
},
},
}

for _, tt := range tests {
tt := tt

t.Run(tt.name, func(t *testing.T) {
t.Parallel()

var (
baseURL = StartServer(t, nil, nil)
client = oblio.NewClient(tt.fields.clientID, tt.fields.clientSecret, oblio.WithBaseURL(baseURL))
got, err = client.GenerateAuthorizeToken(context.Background())
)

if tt.wantErr != nil {
tt.wantErr(t, err)

return
}

require.NoError(t, err)
require.Equal(t, tt.want.AccessToken, got.AccessToken)
})
}
}
201 changes: 201 additions & 0 deletions api_nomenclature.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
package oblio

import (
"context"
"fmt"
"github.com/vcraescu/go-oblio-api/types"
"net/http"
)

type GetNomenclatureCompaniesRequest struct {
Authorization
}

type GetNomenclatureCompaniesResponse struct {
Status

Data []types.Company `json:"data"`
}

func (c *Client) GetNomenclatureCompanies(
ctx context.Context, in *GetNomenclatureCompaniesRequest,
) (*GetNomenclatureCompaniesResponse, error) {
builder := c.requestBuilder.
WithMethod(http.MethodGet).
WithPath("/nomenclature/companies")
out := &GetNomenclatureCompaniesResponse{}

if err := c.doAuthorized(ctx, builder, in.AccessToken, out); err != nil {
return out, fmt.Errorf("doAuthorized: %w", err)
}

return out, nil
}

type GetNomenclatureVATRatesRequest struct {
Authorization

CIF string `json:"cif,omitempty"`
}

func (r *GetNomenclatureVATRatesRequest) Validate() error {
if r.CIF == "" {
return fmt.Errorf("cif is empty: %w", ErrInvalidArgument)
}

return nil
}

type GetNomenclatureVATRatesResponse struct {
Status

Data []types.VATRate `json:"data"`
}

func (c *Client) GetNomenclatureVATRates(
ctx context.Context, in *GetNomenclatureVATRatesRequest,
) (*GetNomenclatureVATRatesResponse, error) {
if err := in.Validate(); err != nil {
return nil, err
}

builder := c.requestBuilder.
WithMethod(http.MethodGet).
WithPath("/nomenclature/vat_rates").
WithParams(in)
out := &GetNomenclatureVATRatesResponse{}

if err := c.doAuthorized(ctx, builder, in.AccessToken, out); err != nil {
return out, fmt.Errorf("doAuthorized: %w", err)
}

return out, nil
}

type GetNomenclatureClientsRequest struct {
Authorization

CIF string `json:"cif,omitempty"`
Name string `json:"name,omitempty"`
ClientCIF string `json:"clientCif,omitempty"`
Offset int `json:"offset,omitempty"`
}

func (r *GetNomenclatureClientsRequest) Validate() error {
if r.CIF == "" {
return fmt.Errorf("cif is empty: %w", ErrInvalidArgument)
}

return nil
}

type GetNomenclatureClientsResponse struct {
Status

Data []types.Client `json:"data"`
}

func (c *Client) GetNomenclatureClients(
ctx context.Context, in *GetNomenclatureClientsRequest,
) (*GetNomenclatureClientsResponse, error) {
if err := in.Validate(); err != nil {
return nil, err
}

builder := c.requestBuilder.
WithMethod(http.MethodGet).
WithPath("/nomenclature/clients").
WithParams(in)
out := &GetNomenclatureClientsResponse{}

if err := c.doAuthorized(ctx, builder, in.AccessToken, out); err != nil {
return out, fmt.Errorf("doAuthorized: %w", err)
}

return out, nil
}

type GetNomenclatureProductsRequest struct {
Authorization

CIF string `json:"cif,omitempty"`
Name string `json:"name,omitempty"`
Code string `json:"code,omitempty"`
Management string `json:"management,omitempty"`
WorkStation string `json:"workStation,omitempty"`
Offset int `json:"offset,omitempty"`
}

func (r *GetNomenclatureProductsRequest) Validate() error {
if r.CIF == "" {
return fmt.Errorf("cif is empty: %w", ErrInvalidArgument)
}

return nil
}

type GetNomenclatureProductsResponse struct {
Status

Data []types.Product `json:"data"`
}

func (c *Client) GetNomenclatureProducts(
ctx context.Context, in *GetNomenclatureProductsRequest,
) (*GetNomenclatureProductsResponse, error) {
if err := in.Validate(); err != nil {
return nil, err
}

builder := c.requestBuilder.
WithMethod(http.MethodGet).
WithPath("/nomenclature/products").
WithParams(in)
out := &GetNomenclatureProductsResponse{}

if err := c.doAuthorized(ctx, builder, in.AccessToken, out); err != nil {
return out, fmt.Errorf("doAuthorized: %w", err)
}

return out, nil
}

type GetNomenclatureSeriesRequest struct {
Authorization

CIF string `json:"cif,omitempty"`
}

func (r *GetNomenclatureSeriesRequest) Validate() error {
if r.CIF == "" {
return fmt.Errorf("cif is empty: %w", ErrInvalidArgument)
}

return nil
}

type GetNomenclatureSeriesResponse struct {
Status

Data []types.Series `json:"data"`
}

func (c *Client) GetNomenclatureSeries(
ctx context.Context, in *GetNomenclatureSeriesRequest,
) (*GetNomenclatureSeriesResponse, error) {
if err := in.Validate(); err != nil {
return nil, err
}

builder := c.requestBuilder.
WithMethod(http.MethodGet).
WithPath("/nomenclature/series").
WithParams(in)
out := &GetNomenclatureSeriesResponse{}

if err := c.doAuthorized(ctx, builder, in.AccessToken, out); err != nil {
return out, fmt.Errorf("doAuthorized: %w", err)
}

return out, nil
}
Loading

0 comments on commit 06e69bd

Please sign in to comment.