Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API Client implementation in Go. #821

Merged
merged 7 commits into from Jan 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
114 changes: 114 additions & 0 deletions docs/development/api-client-go.md
@@ -0,0 +1,114 @@
# API Client implementation in Go

- [API Key authentication](#using-api-key-authentication)
- [Basic authentication](#using-basic-authentication)
- [Making a list GET request](#making-a-list-get-request)
- [Making a read GET request](#making-a-read-get-request)
- [Making a create POST request](#making-a-create-post-request)
- [Making an update PUT request](#making-an-update-put-request)
- [Making a delete DELETE request](#making-a-delete-delete-request)

For the source code, refer to [GitHub](https://github.com/definitepotato/espocrm).

## Using API Key Authentication

```go
import "github.com/definitepotato/espocrm"

client := espocrm.NewApiClient(
"https://espocrm.example.com",
espocrm.WithApiKeyAuth("Your API Key here"),
)
```

## Using Basic Authentication

(**this is highly discouraged**)

```go
import "github.com/definitepotato/espocrm"

client := espocrm.NewApiClient(
"https://espocrm.example.com",
espocrm.WithBasicAuth("username", "password"),
)
```

## Making a list GET request

```go
import "github.com/definitepotato/espocrm"

parameters := espocrm.NewParameters(
espocrm.SetWhere([]espocrm.Where{
{
Type: espocrm.Equals,
Attribute: "myAttribute",
Value: "myValue",
},
}),
)

client := espocrm.NewApiClient(
"https://espocrm.example.com",
espocrm.WithApiKeyAuth("Your API Key here"),
)

contacts, err := client.List("Contact", parameters)
```

## Making a read GET request

```go
import "github.com/definitepotato/espocrm"

client := espocrm.NewApiClient(
"https://espocrm.example.com",
espocrm.WithApiKeyAuth("Your API Key here"),
)

contact, err := client.Read("Contact", "78abc123def456")
```

## Making a create POST request

```go
import "github.com/definitepotato/espocrm"

newContact := `"{ "name": "Test", "assignedUserId": "1" }"`

client := espocrm.NewApiClient(
"https://espocrm.example.com",
espocrm.WithApiKeyAuth("Your API Key here"),
)

attributes, err := client.Create("Contact", newContact)
```

## Making an update PUT request

```go
import "github.com/definitepotato/espocrm"

updatePayload := `"{ assignedUserId": "1" }"`

client := espocrm.NewApiClient(
"https://espocrm.example.com",
espocrm.WithApiKeyAuth("Your API Key here"),
)

attributes, err := client.Update("Contact", updatePayload)
```

## Making a delete DELETE request

```go
import "github.com/definitepotato/espocrm"

client := espocrm.NewApiClient(
"https://espocrm.example.com",
espocrm.WithApiKeyAuth("Your API Key here"),
)

status, err := client.Delete("Contact", "78abc123def456")
```