import "github.com/Uffe-Code/go-odata"
go-odata is a library that desires to generate a fully functional OData API Client for Go.
To setup the library, add a go file to be executed on it's own. It should look something like this.
package main
import (
"fmt"
"github.com/Uffe-Code/go-odata/modelGenerator"
"path/filepath"
)
func main() {
directoryPath, err := filepath.Abs("dataModel/")
if err != nil {
fmt.Printf("error while looking up folder: %s", err.Error())
return
}
generator := modelGenerator.Generator{
ApiUrl: "https://services.odata.org/TripPinRESTierService/(S(c0y0kjlx4yjoxry4otnmoxf4))/",
DirectoryPath: directoryPath,
}
err = generator.GenerateCode()
if err != nil {
fmt.Printf("error while generating code: %s", err.Error())
return
}
fmt.Printf("code generated successfully")
}
When executed, it will generate a file in the specified directory with all model definitions. You can then use the client.
client := odataClient.New("https://services.odata.org/TripPinRESTierService/(S(c0y0kjlx4yjoxry4otnmoxf4))/")
If you want to implement your own logic around the client, implement the Wrapper interface by adding a method called "ODataClient()" that will return the raw client. If you do, then the wrapper can be used to fetch data sets.
client := odataClient.New("https://services.odata.org/TripPinRESTierService/(S(c0y0kjlx4yjoxry4otnmoxf4))/")
peopleCollection := dataModel.NewPeopleCollection(client)
dataSet := peopleCollection.DataSet()
data, errs := dataSet.List(odataClient.ODataFilter{
filter: "Name eq 'Foo'"
})
for err := range errs {
fmt.Printf("%s", err.Error())
return
}
for model := range data {
fmt.Println(model.Name)
}
person := dataModel.Person{
Name: "Foo",
}
insertedPerson, err := dataSet.Insert(person)
fmt.Printf("%d", insertedPerson.PersonId)
person := dataModel.Person{
Name: "Foo",
}
updatedPerson, err := dataSet.Update(5, person)
fmt.Printf("%d", updatedPerson.PersonId) // 5