Skip to content

Commit

Permalink
First Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
seriallink committed Oct 31, 2019
0 parents commit e548c83
Show file tree
Hide file tree
Showing 22 changed files with 778 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

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

# Output of the go coverage tool, specifically when used with LiteIDE
*.out
6 changes: 6 additions & 0 deletions .idea/dictionaries

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/nfeio.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 47 additions & 0 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# nfeio

###
32 changes: 32 additions & 0 deletions address.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package nfeio

func (c *Client) SearchAddressByPostalCode(postalCode string) (response Address, err error) {
response.State = &State{}
err = c.Get("addresses/"+postalCode, nil, nil, &response)
return
}

type Address struct {
StreetSuffix string `json:"streetSuffix,omitempty"`
Street string `json:"street,omitempty"`
Number string `json:"number,omitempty"`
NumberMin string `json:"numberMin,omitempty"`
NumberMax string `json:"numberMax,omitempty"`
AdditionalInformation string `json:"additionalInformation,omitempty"`
District string `json:"district,omitempty"`
State interface{} `json:"state,omitempty"`
City *City `json:"city,omitempty"`
PostalCode string `json:"postalCode,omitempty"`
Country string `json:"country,omitempty"`
}

type City struct {
Code string `json:"code,omitempty"`
Name string `json:"name,omitempty"`
}

type State struct {
Code string `json:"code,omitempty"`
Name string `json:"name,omitempty"`
Abbreviation string `json:"abbreviation"`
}
12 changes: 12 additions & 0 deletions address_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package nfeio

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestSearchAddressByPostalCode(t *testing.T) {
address, err := client.SearchAddressByPostalCode("03181010")
assert.NoError(t, err)
assert.Equal(t, address.PostalCode, "03181010")
}
32 changes: 32 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package nfeio

import "strings"

type Client struct {
ApiKey string
OpenKey string
}

func NewClient(apiKey string, openKey string) *Client {
return &Client{ApiKey: apiKey, OpenKey: openKey}
}

func (c *Client) GetAuthorization(path string) string {

if strings.Contains(path, "addresses") {
return c.OpenKey
}

return c.ApiKey

}

func (c *Client) GetEndpoint(path string) string {

if strings.Contains(path, "addresses") {
return addressUrl
}

return baseUrl

}
10 changes: 10 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package nfeio

var client *Client

func init() {
client = NewClient(
"",
"",
)
}
164 changes: 164 additions & 0 deletions company.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
package nfeio

import (
"encoding/json"
"time"
)

func (c *Client) GetCompany(idOrTaxNumber string) (response CompanyResponse, err error) {
err = c.Get("companies/"+idOrTaxNumber, nil, nil, &response)
return
}

func (c *Client) GetCompanies(pageCount, pageIndex int64) (response CompaniesResponse, err error) {
err = c.Get("companies", Params{"pageCount": pageCount, "pageIndex": pageIndex}, nil, &response)
return
}

func (c *Client) AddCompany(company *Company) (response CompanyResponse, err error) {
err = c.Post("companies", company, nil, &response)
return
}

func (c *Client) SetCompany(company *Company) (response CompanyResponse, err error) {
err = c.Put("companies/"+company.Id, company, nil, &response)
return
}

func (c *Client) DeleteCompany(id string) (err error) {
err = c.Delete("companies/"+id, nil, nil, nil)
return
}

//func (c *Client) UploadCertificate(id, file, password string) error {
//
// var (
// header []string
// body bytes.Buffer
// erm ErrMessage
// )
//
// // init curl
// easy := curl.EasyInit()
//
// // close curl
// defer easy.Cleanup()
//
// // set curl opt
// _ = easy.Setopt(curl.OPT_URL, fmt.Sprintf("%scompanies/%s/certificate", baseUrl, id))
// _ = easy.Setopt(curl.OPT_SSL_VERIFYPEER, false)
//
// // enable on debug mode
// if os.Getenv("debug") == "true" {
// _ = easy.Setopt(curl.OPT_VERBOSE, true)
// }
//
// // set header
// _ = easy.Setopt(curl.OPT_HTTPHEADER, []string{
// "Authorization: " + c.ApiKey,
// "Content-Type: multipart/form-data",
// "Accept: application/json",
// })
//
// // init form
// form := curl.NewForm()
//
// // add form fields
// _ = form.AddFile("file", file)
// _ = form.Add("password", password)
//
// // define http method
// _ = easy.Setopt(curl.OPT_HTTPPOST, form)
//
// // print upload progress
// //_ = easy.Setopt(curl.OPT_NOPROGRESS, false)
// //_ = easy.Setopt(curl.OPT_PROGRESSFUNCTION, func(dltotal, dlnow, ultotal, ulnow float64, _ interface{}) bool {
// // fmt.Printf("Download %3.2f%%, Uploading %3.2f%%\r", dlnow/dltotal*100, ulnow/ultotal*100)
// // return true
// //})
//
// // get response body (callback)
// _ = easy.Setopt(curl.OPT_WRITEFUNCTION, func(buf []byte, data interface{}) bool {
// body.Write(buf)
// return true
// })
//
// // get response header (callback)
// _ = easy.Setopt(curl.OPT_HEADERFUNCTION, func(buf []byte, data interface{}) bool {
// header = append(header, strings.Replace(string(buf), "\r\n", "", -1))
// return true
// })
//
// // post certificate
// if err := easy.Perform(); err != nil {
// return err
// }
//
// // check error message
// if err := json.NewDecoder(&body).Decode(&erm); err == nil && erm.Message != "" {
// return erm
// }
//
// // check response status code
// if len(header) < 1 || !strings.Contains(header[0], "200") {
//
// // get status code
// data := strings.Split(strings.Trim(header[0], " "), " ")
//
// // parse to int
// code, _ := strconv.Atoi(data[1])
//
// // return error code
// return errors.New(http.StatusText(code))
//
// }
//
// return nil
//
//}

type CompanyResponse struct {
Data Company `json:"companies"`
}

type CompaniesResponse struct {
Data []Company `json:"companies"`
pagination
}

type Company struct {
Id string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
TradeName string `json:"tradeName,omitempty"`
Email string `json:"email,omitempty"`
FederalTaxNumber json.Number `json:"federalTaxNumber,omitempty"`
RegionalTaxNumber json.Number `json:"regionalTaxNumber,omitempty"`
MunicipalTaxNumber json.Number `json:"municipalTaxNumber,omitempty"`
CompanyRegistryNumber json.Number `json:"companyRegistryNumber,omitempty"`
TaxRegime interface{} `json:"taxRegime,omitempty"`
SpecialTaxRegime interface{} `json:"specialTaxRegime,omitempty"`
LegalNature interface{} `json:"legalNature,omitempty"`
RpsSerialNumber interface{} `json:"rpsSerialNumber,omitempty"`
RpsNumber interface{} `json:"rpsNumber,omitempty"`
IssRate float64 `json:"issRate,omitempty"`
Environment string `json:"environment,omitempty"`
FiscalStatus string `json:"fiscalStatus,omitempty"`
Address *Address `json:"address,omitempty"`
Certificate *Certificate `json:"certificate,omitempty"`
EconomicActivities []EconomicActivity `json:"economicActivities,omitempty"`
OpenningDate *time.Time `json:"openningDate,omitempty"`
CreatedOn *time.Time `json:"createdOn,omitempty"`
ModifiedOn *time.Time `json:"modifiedOn,omitempty"`
}

type EconomicActivity struct {
Type string `json:"type,omitempty"`
Code json.Number `json:"code,omitempty"`
}

type Certificate struct {
Thumbprint string `json:"thumbprint,omitempty"`
Status string `json:"status,omitempty"`
ModifiedOn time.Time `json:"modifiedOn,omitempty"`
ExpiresOn time.Time `json:"expiresOn,omitempty"`
}
Loading

0 comments on commit e548c83

Please sign in to comment.