@@ -7,7 +7,7 @@ A simple library-wrapper for NovaPost API.
77## Idea
88
99I was rather surprised by how sparse the availability of NovaPost API libraries for Go is, and so decided to make one of
10- my own which would be simple to use and try to stick to the original API reference as much as possible.
10+ my own which would be simple to use and tries to stick to the original API reference as much as possible.
1111
1212## Usage
1313
@@ -17,7 +17,7 @@ Just go get the libbo.
1717go get github.com/sirkostya009/go-novapost
1818```
1919
20- ### Code :
20+ ### Example :
2121
2222``` go
2323package main
@@ -47,35 +47,54 @@ client defaulting to JSON and no timeout.
4747
4848``` go
4949c := np.NewClient (os.Getenv (" NOVA_POST_API_KEY" ),
50- np.WithXML ,
51- np.WithJSON , // default
52- np.WithTimeout (5 * time.Second ),
53- np.WithMarshaler (json.Marshal ), // default for JSON
54- np.WithUnmarshaler (xml.Unmarshal ), // default for XML
55- np.WithURL (" https://api.novaposhta.ua/v2.0/json/" ), // set automatically by WithXML, WithJSON
50+ np.WithHTTPClient (&http.Client {}),
51+ np.WithMarshaler (xml.Marshal ),
52+ np.WithUnmarshaler (xml.Unmarshal ),
53+ np.WithURL (np.XMLUrl ),
5654)
5755```
5856
59- There's also an option for rawdogging requests, in case you wish to do something that is not supported by the library.
57+ You can also initialize the client after construction:
58+
59+ ``` go
60+ c := np.NewClient (os.Getenv (" NOVA_POST_API_KEY" ))
61+ c.Url = np.JSONUrl
62+ c.HTTPClient = http.DefaultClient
63+ c.Marshaler = fasterXmlMarshaler
64+ ```
65+
66+ As you can see, this library uses XML for request marshalling, due to the nature of data returned by API. You aren't
67+ stripped of the ability to use JSON, though. Do note that the implementation must be a custom one, as most models have
68+ ints and floats whereas JSON response from API for those fields is a string.
69+
70+ Alternatively, you are provided with an option for rawdogging requests, in case you wish to do something that is not
71+ supported by the library.
6072
6173``` go
6274type customProps struct {
63- Value string ` json:"value"`
75+ Value string
76+ }
77+
78+ type customResponse struct {
79+ Data []struct {
80+ Foo string
81+ } ` xml:"data->item"`
6482}
6583
66- res , err := c .RawRequest ( " Model" , " method" , customProps{Value: " foo" }))
84+ res , err := np .RawRequest [customResponse](c, " Model" , " method" , customProps{Value: " foo" }))
6785
6886for _ , m := range res.Data {
69- fmt.Println (m[ " foo " ]) // res.Data is a slice of maps
87+ fmt.Println (m. Foo )
7088}
7189```
7290
7391## TODO:
74- - [ ] Fix tests
75- - [ ] Fix XML
76- - [ ] Add constants for common strings, like "Sender" or "ThirdPerson"
92+ - [x] Fix tests
93+ - [x] Fix XML
94+ - [x] Add constants for common strings, like "Sender" or "ThirdPerson"
95+ - [ ] Add custom marshaler/unmarshaler for JSON
7796
7897## Contributing
7998
80- 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.
81- Make sure existing tests are passing and that you've added tests for your changes, too.
99+ If you wish to fix a bug or perhaps have a better idea of how to implement something , feel free to fork this repo and
100+ open a PR. Make sure existing tests are passing and that you've added tests for your changes, too.
0 commit comments