Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
sirkostya009 committed Jan 30, 2024
0 parents commit b2bf736
Show file tree
Hide file tree
Showing 22 changed files with 2,627 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea
.vscode
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Constantine Tovstyk

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
82 changes: 82 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# go-novapost

A simple library-wrapper for NovaPost API.

[![](https://godoc.org/github.com/sirkostya009/go-novapost?status.svg)](https://godoc.org/github.com/sirkostya009/go-novapost)

## Idea

I was rather surprised by how sparse the availability of NovaPost API libraries for Go is, and so decided to make one of
my own which would be simple to use and try to stick to the original API reference as much as possible.

## Usage

Just go get the libbo.

```bash
go get github.com/sirkostya009/go-novapost
```

### Code:

```go
package main

import (
"fmt"
np "github.com/sirkostya009/go-novapost"
"os"
)

func main() {
c := np.NewClient(os.Getenv("NOVA_POST_API_KEY"))

citiesRes, _ := c.GetCities(np.CityRequest{
FindByString: "Київ",
Limit: 150,
Page: 1,
})
for _, city := range citiesRes.Data {
fmt.Println(city.Description) // prints "Київ" and "Київець"
}
}
```

You can also customize your client by passing options to it. Note, however, that none of them are required, with
client defaulting to JSON and no timeout.

```go
c := np.NewClient(os.Getenv("NOVA_POST_API_KEY"),
WithXML,
WithJSON, // default
np.WithTimeout(5 * time.Second),
np.WithURL("https://api.novaposhta.ua/v2.0/json/"), // set automatically by WithXML, WithJSON
)
```

There's also an option for rawdogging requests, in case something you wish to do is not supported by the library.

```go
res, err := c.RawRequest(np.Request{
// we don't need to pass an api key, it will be set inside RawRequest method
ModelName: "Model",
CalledMethod: "method",
MethodProperties: map[string]any{
"foo": "bar",
},
})

for _, m := range res.Data {
fmt.Println(m["foo"]) // res.Data is a slice of maps
}
```

## TODO:
- Fix tests
- Fix XML
- Add constants for common strings, like "Sender" or "ThirdPerson"

## Contributing

If you wish to fix a bug or perhaps have a better idea of how to implement x, feel free to fork this repo and open a PR.
Make sure existing tests are passing and that you've added tests for your changes, too.
270 changes: 270 additions & 0 deletions additional_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,270 @@
package novapost

type PossibilityCreateReturn struct {
NonCash bool `json:"NonCash"`
City string `json:"City"`
Counterparty string `json:"Counterparty"`
ContactPerson string `json:"ContactPerson"`
Address string `json:"Address"`
Phone string `json:"Phone"`
Ref string `json:"Ref"`
}

// CheckPossibilityCreateReturn Перевірка можливості створення заявки на повернення
//
// Метод «CheckPossibilityCreateReturn», працює в моделі «AdditionalService», цей метод дозволяє перевірити можливість
// створення заявки на повернення. Метод доступний лише клієнтам відправників. У разі успіху повертаються блоки адрес,
// які можна використовуватиме створення заявки.
func (c Client) CheckPossibilityCreateReturn(req Number) (Response[PossibilityCreateReturn], error) {
return request[PossibilityCreateReturn](c, AdditionalServiceModel, "CheckPossibilityCreateReturn", req)
}

// GetReturnReasons Отримання списку причин повернення
//
// Метод «getReturnReasons», працює в моделі «AdditionalService», цей метод дозволяє отримати список доступних причин
// повернення.
func (c Client) GetReturnReasons() (Response[RefDescription], error) {
return request[RefDescription](c, AdditionalServiceModel, "getReturnReasons", nil)
}

type (
ReasonRef struct {
ReasonRef string `json:"ReasonRef"`
}

ReturnReasonSubtype struct {
Ref string `json:"Ref"`
Description string `json:"Description"`
ReasonRef string `json:"ReasonRef"`
}
)

// GetReturnReasonSubtypes Отримання списку підтипів причини повернення
//
// Метод «getReturnReasonsSubtypes», працює в моделі «AdditionalService», цей метод дозволяє отримати список підтипів
// доступних причин повернення.
func (c Client) GetReturnReasonSubtypes(req ReasonRef) (Response[ReturnReasonSubtype], error) {
return request[ReturnReasonSubtype](c, AdditionalServiceModel, "getReturnReasonSubtypes", req)
}

type NewReturnOrderRequest struct {
IntDocNumber string `json:"IntDocNumber"`
PaymentMethod string `json:"PaymentMethod"`
Reason string `json:"Reason"`
SubtypeReason string `json:"SubtypeReason"`
Note string `json:"Note"`
OrderType string `json:"OrderType"`
SenderContactName string `json:"SenderContactName"`
SenderPhone string `json:"SenderPhone"`
Recipient string `json:"Recipient"`
RecipientContactName string `json:"RecipientContactName"`
RecipientPhone string `json:"RecipientPhone"`
PayerType string `json:"PayerType"`
Customer string `json:"Customer"`
ServiceType string `json:"ServiceType"`
RecipientSettlement string `json:"RecipientSettlement"`
RecipientSettlementStreet string `json:"RecipientSettlementStreet"`
RecipientWarehouse string `json:"RecipientWarehouse"`
BuildingNumber string `json:"BuildingNumber"`
NoteAddressRecipient string `json:"NoteAddressRecipient"`
ReturnAddressRef string `json:"ReturnAddressRef"`
}

// CreateNewReturn Створення заявки на повернення
//
// Використовується для створення заявки на повернення а також зміни даних. Метод доступний лише клієнтам відправників.
func (c Client) CreateNewReturn(req NewReturnOrderRequest) (Response[RefNumber], error) {
return request[RefNumber](c, AdditionalServiceModel, "save", req)
}

type (
ReturnOrderRequest struct {
Number string `json:"Number"`
Ref string `json:"Ref"`
BeginDate string `json:"BeginDate"`
EndDate string `json:"EndDate"`
Page int `json:"Page"`
Limit int `json:"Limit"`
}

ReturnOrder struct {
OrderRef string `json:"OrderRef"`
OrderNumber string `json:"OrderNumber"`
OrderStatus string `json:"OrderStatus"`
DocumentNumber string `json:"DocumentNumber"`
CounterpartyRecipient string `json:"CounterpartyRecipient"`
ContactPersonRecipient string `json:"ContactPersonRecipient"`
AddressRecipient string `json:"AddressRecipient"`
DeliveryCost string `json:"DeliveryCost"`
EstimatedDeliveryDate string `json:"EstimatedDeliveryDate"`
ExpressWaybillNumber string `json:"ExpressWaybillNumber"`
ExpressWaybillStatus string `json:"ExpressWaybillStatus"`
}
)

// GetReturnOrdersList Отримання списку заявок на повернення
//
// Метод «getReturnOrdersList», працює в моделі «AdditionalService», цей метод дозволяє отримати список заявок на
// повернення.
func (c Client) GetReturnOrdersList(req ReturnOrderRequest) (Response[ReturnOrder], error) {
return request[ReturnOrder](c, AdditionalServiceModel, "getReturnOrdersList", req)
}

// DeleteReturnOrder Видалення заявки
//
// Метод «delete», працює в моделі «AdditionalService», цей метод дозволяє видалити:
//
// - заявку на повернення;
//
// - заявку щодо зміни даних (можна видалити заявку лише зі статусом «Прийнято»);
//
// - заявку переадресації.
func (c Client) DeleteReturnOrder(req Ref) (Response[ReturnOrder], error) {
return request[ReturnOrder](c, AdditionalServiceModel, "delete", req)
}

type (
PossibilityChangeEW struct {
CanChangeSender bool `json:"CanChangeSender"`
CanChangeRecipient bool `json:"CanChangeRecipient"`
CanChangePayerTypeOrPaymentMethod bool `json:"CanChangePayerTypeOrPaymentMethod"`
CanChangeBackwardDeliveryDocuments bool `json:"CanChangeBackwardDeliveryDocuments"`
CanChangeBackwardDeliveryMoney bool `json:"CanChangeBackwardDeliveryMoney"`
CanChangeCash2Card bool `json:"CanChangeCash2Card"`
CanChangeBackwardDeliveryOther bool `json:"CanChangeBackwardDeliveryOther"`
CanChangeAfterpaymentType bool `json:"CanChangeAfterpaymentType"`
CanChangeLiftingOnFloor bool `json:"CanChangeLiftingOnFloor"`
CanChangeLiftingOnFloorWithElevator bool `json:"CanChangeLiftingOnFloorWithElevator"`
CanChangeFillingWarranty bool `json:"CanChangeFillingWarranty"`
SenderCounterparty string `json:"SenderCounterparty"`
ContactPersonSender string `json:"ContactPersonSender"`
SenderPhone string `json:"SenderPhone"`
RecipientCounterparty string `json:"RecipientCounterparty"`
ContactPersonRecipient string `json:"ContactPersonRecipient"`
RecipientPhone string `json:"RecipientPhone"`
PayerType string `json:"PayerType"`
PaymentMethod string `json:"PaymentMethod"`
}

IntDocNumber struct {
IntDocNumber string `json:"IntDocNumber"`
}
)

// CheckPossibilityChangeEW Перевірка можливості створення заявки зі зміни даних
//
// Метод «CheckPossibilityChangeEW», працює в моделі «AdditionalService», цей метод дозволяє перевірити можливість
// створення заявки щодо зміни даних. У разі успіху повертаються необхідні параметри створення заявки.
func (c Client) CheckPossibilityChangeEW(req IntDocNumber) (Response[PossibilityChangeEW], error) {
return request[PossibilityChangeEW](c, AdditionalServiceModel, "CheckPossibilityChangeEW", req)
}

type (
ChangeEWRequest struct {
Number string `json:"Number"`
Ref string `json:"Ref"`
BeginDate string `json:"BeginDate"`
EndDate string `json:"EndDate"`
Page int `json:"Page"`
Limit int `json:"Limit"`
}

ChangeEW struct {
OrderRef string `json:"OrderRef"`
OrderNumber string `json:"OrderNumber"`
OrderStatus string `json:"OrderStatus"`
DocumentNumber string `json:"DocumentNumber"`
DateTime string `json:"DateTime"`
BeforeChangeSenderCounterparty string `json:"BeforeChangeSenderCounterparty"`
AfterChangeChangeSenderCounterparty string `json:"AfterChangeChangeSenderCounterparty"`
Cost string `json:"Cost"`
BeforeChangeSenderPhone string `json:"BeforeChangeSenderPhone"`
AfterChangeSenderPhone string `json:"AfterChangeSenderPhone"`
}
)

// GetChangeEWOrdersList Отримання списку заявок
//
// Метод «getChangeEWOrdersList», працює в моделі «AdditionalService», цей метод дозволяє отримати список заявок щодо
// зміни даних.
func (c Client) GetChangeEWOrdersList(req ChangeEWRequest) (Response[ChangeEW], error) {
return request[ChangeEW](c, AdditionalServiceModel, "getChangeEWOrdersList", req)
}

type (
Number struct {
Number string `json:"Number"`
}

PossibilityRedirecting struct {
Ref string `json:"Ref"`
Number string `json:"Number"`
PayerType string `json:"PayerType"`
PaymentMethod string `json:"PaymentMethod"`
WarehouseRef string `json:"WarehouseRef"`
WarehouseDescription string `json:"WarehouseDescription"`
AddressDescription string `json:"AddressDescription"`
StreetDescription string `json:"StreetDescription"`
BuildingNumber string `json:"BuildingNumber"`
CityRecipient string `json:"CityRecipient"`
CityRecipientDescription string `json:"CityRecipientDescription"`
SettlementRecipient string `json:"SettlementRecipient"`
SettlementRecipientDescription string `json:"SettlementRecipientDescription"`
SettlementType string `json:"SettlementType"`
CounterpartyRecipientRef string `json:"CounterpartyRecipientRef"`
CounterpartyRecipientDescription string `json:"CounterpartyRecipientDescription"`
RecipientName string `json:"RecipientName"`
PhoneSender string `json:"PhoneSender"`
PhoneRecipient string `json:"PhoneRecipient"`
DocumentWeight string `json:"DocumentWeight"`
}
)

// CheckPossibilityForRedirecting Перевірка можливості створення заявки на переадресацію відправлення
//
// Метод «checkPossibilityForRedirecting», працює в моделі «AdditionalServiceGeneral», цей метод дозволяє перевірити
// можливість створення заявки на переадресацію відправлення.
//
// Метод доступний лише клієнтам відправників.
//
// У разі успіху повертаються необхідні параметри створення заявки.
func (c Client) CheckPossibilityForRedirecting(req Number) (Response[PossibilityRedirecting], error) {
return request[PossibilityRedirecting](c, AdditionalServiceModel, "CheckPossibilityForRedirecting", req)
}

type (
Redirection struct {
OrderRef string `json:"OrderRef"`
OrderNumber string `json:"OrderNumber"`
DateTime string `json:"DateTime"`
DocumentNumber string `json:"DocumentNumber"`
Note string `json:"Note"`
CityRecipient string `json:"CityRecipient"`
RecipientAddress string `json:"RecipientAddress"`
CounterpartyRecipient string `json:"CounterpartyRecipient"`
RecipientName string `json:"RecipientName"`
PhoneRecipient string `json:"PhoneRecipient"`
PayerType string `json:"PayerType"`
DeliveryCost string `json:"DeliveryCost"`
EstimatedDeliveryDate string `json:"EstimatedDeliveryDate"`
ExpressWaybillNumber string `json:"ExpressWaybillNumber"`
ExpressWaybillStatus string `json:"ExpressWaybillStatus"`
}

RedirectionRequest struct {
Number string `json:"Number"`
Ref string `json:"Ref"`
BeginDate string `json:"BeginDate"`
EndDate string `json:"EndDate"`
Page int `json:"Page"`
Limit int `json:"Limit"`
}
)

// GetRedirectionOrdersList Отримання списку заявок
//
// Метод «getRedirectionOrdersList», працює в моделі «AdditionalService», цей метод дозволяє отримати список заявок
// переадресації відправлень.
func (c Client) GetRedirectionOrdersList(req RedirectionRequest) (Response[Redirection], error) {
return request[Redirection](c, AdditionalServiceModel, "getRedirectionOrdersList", req)
}
Loading

0 comments on commit b2bf736

Please sign in to comment.