Skip to content

Commit e733fbc

Browse files
authored
Add BackgroundTask feature to libs (#930)
## Motivation This updates the libs to support fetching/listing BackgroundTasks. These endpoints were added a while ago, but the libs were never updated. ## Solution Update the libs.
2 parents 8f85f0a + 04b9e77 commit e733fbc

File tree

12 files changed

+458
-2
lines changed

12 files changed

+458
-2
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System.Collections.Generic;
2+
using System.Threading;
3+
using System.Threading.Tasks;
4+
using Svix.Model;
5+
using Svix.Models;
6+
7+
namespace Svix.Abstractions
8+
{
9+
public interface IBackgroundTask
10+
{
11+
BackgroundTaskOut Get(string taskId, string idempotencyKey = default);
12+
13+
Task<BackgroundTaskOut> GetAsync(string taskId, string idempotencyKey = default,
14+
CancellationToken cancellationToken = default);
15+
16+
List<BackgroundTaskOut> List(BackgroundTaskListOptions options = null, string idempotencyKey = default);
17+
18+
Task<List<BackgroundTaskOut>> ListAsync(BackgroundTaskListOptions options = null, string idempotencyKey = default, CancellationToken cancellationToken = default);
19+
}
20+
}

csharp/Svix/BackgroundTask.cs

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Net;
4+
using System.Threading;
5+
using System.Threading.Tasks;
6+
using Microsoft.Extensions.Logging;
7+
using Svix.Abstractions;
8+
using Svix.Api;
9+
using Svix.Client;
10+
using Svix.Model;
11+
using Svix.Models;
12+
13+
namespace Svix
14+
{
15+
public sealed class BackgroundTask : SvixResourceBase, IBackgroundTask
16+
{
17+
private readonly IBackgroundTasksApi _backgroundTaskApi;
18+
19+
public BackgroundTask(ISvixClient svixClient, IBackgroundTasksApi backgroundTaskApi)
20+
: base(svixClient)
21+
{
22+
_backgroundTaskApi = backgroundTaskApi ?? throw new ArgumentNullException(nameof(backgroundTaskApi));
23+
}
24+
25+
public BackgroundTaskOut Get(string taskId, string idempotencyKey = default)
26+
{
27+
try
28+
{
29+
var lBackgroundTask = _backgroundTaskApi.GetBackgroundTask(taskId);
30+
31+
return lBackgroundTask;
32+
}
33+
catch (ApiException e)
34+
{
35+
Logger?.LogError(e, $"{nameof(Get)} failed");
36+
37+
if (Throw)
38+
throw;
39+
40+
return null;
41+
}
42+
}
43+
44+
public async Task<BackgroundTaskOut> GetAsync(string taskId, string idempotencyKey = default, CancellationToken cancellationToken = default)
45+
{
46+
try
47+
{
48+
var lBackgroundTask = await _backgroundTaskApi.GetBackgroundTaskAsync(taskId);
49+
50+
return lBackgroundTask;
51+
}
52+
catch (ApiException e)
53+
{
54+
Logger?.LogError(e, $"{nameof(GetAsync)} failed");
55+
56+
if (Throw)
57+
throw;
58+
59+
return null;
60+
}
61+
}
62+
63+
public List<BackgroundTaskOut> List(BackgroundTaskListOptions options = null, string idempotencyKey = default)
64+
{
65+
try
66+
{
67+
var lResponse = _backgroundTaskApi.ListBackgroundTasks(
68+
options?.Status,
69+
options?.Task,
70+
options?.Limit,
71+
options?.Iterator,
72+
options?.Order);
73+
74+
return lResponse?.Data;
75+
}
76+
catch (ApiException e)
77+
{
78+
Logger?.LogError(e, $"{nameof(List)} failed");
79+
80+
if (Throw)
81+
throw;
82+
83+
return new List<BackgroundTaskOut>();
84+
}
85+
}
86+
87+
public async Task<List<BackgroundTaskOut>> ListAsync(BackgroundTaskListOptions options = null, string idempotencyKey = default, CancellationToken cancellationToken = default)
88+
{
89+
try
90+
{
91+
var lResponse = await _backgroundTaskApi.ListBackgroundTasksAsync(
92+
options?.Status,
93+
options?.Task,
94+
options?.Limit,
95+
options?.Iterator,
96+
options?.Order,
97+
cancellationToken);
98+
99+
return lResponse?.Data;
100+
}
101+
catch (ApiException e)
102+
{
103+
Logger?.LogError(e, $"{nameof(ListAsync)} failed");
104+
105+
if (Throw)
106+
throw;
107+
108+
return new List<BackgroundTaskOut>();
109+
}
110+
}
111+
}
112+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Svix.Model;
4+
5+
namespace Svix.Models
6+
{
7+
public sealed class BackgroundTaskListOptions : ListOptions
8+
{
9+
public BackgroundTaskStatus? Status { get; set; }
10+
11+
public BackgroundTaskType? Task { get; set; }
12+
}
13+
}

go/backgroundtask.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package svix
2+
3+
import (
4+
"context"
5+
6+
"github.com/svix/svix-webhooks/go/internal/openapi"
7+
)
8+
9+
type (
10+
ListResponseBackgroundTaskOut openapi.ListResponseBackgroundTaskOut
11+
BackgroundTaskOut openapi.BackgroundTaskOut
12+
)
13+
14+
type BackgroundTask struct {
15+
api *openapi.APIClient
16+
}
17+
18+
type BackgroundTaskListOptions struct {
19+
Iterator *string
20+
Limit *int32
21+
Order *Ordering
22+
Status *openapi.BackgroundTaskStatus
23+
Task *openapi.BackgroundTaskType
24+
}
25+
26+
func (a *BackgroundTask) List(ctx context.Context, options *BackgroundTaskListOptions) (*ListResponseBackgroundTaskOut, error) {
27+
req := a.api.BackgroundTasksApi.ListBackgroundTasks(ctx)
28+
if options != nil {
29+
if options.Iterator != nil {
30+
req = req.Iterator(*options.Iterator)
31+
}
32+
if options.Limit != nil {
33+
req = req.Limit(*options.Limit)
34+
}
35+
if options.Order != nil {
36+
req = req.Order(openapi.Ordering(*options.Order))
37+
}
38+
if options.Status != nil {
39+
req = req.Status(*options.Status)
40+
}
41+
if options.Task != nil {
42+
req = req.Task(*options.Task)
43+
}
44+
}
45+
resp, res, err := req.Execute()
46+
if err != nil {
47+
return nil, wrapError(err, res)
48+
}
49+
ret := ListResponseBackgroundTaskOut(resp)
50+
return &ret, nil
51+
}
52+
53+
func (a *BackgroundTask) Get(ctx context.Context, taskId string) (*BackgroundTaskOut, error) {
54+
req := a.api.BackgroundTasksApi.GetBackgroundTask(ctx, taskId)
55+
resp, res, err := req.Execute()
56+
if err != nil {
57+
return nil, wrapError(err, res)
58+
}
59+
ret := BackgroundTaskOut(resp)
60+
return &ret, nil
61+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.svix;
2+
3+
import com.svix.exceptions.ApiException;
4+
import com.svix.internal.api.BackgroundTasksApi;
5+
import com.svix.models.BackgroundTaskOut;
6+
import com.svix.models.ListResponseBackgroundTaskOut;
7+
8+
public final class BackgroundTask {
9+
private final BackgroundTasksApi api;
10+
11+
BackgroundTask() {
12+
api = new BackgroundTasksApi();
13+
}
14+
15+
public ListResponseBackgroundTaskOut list(final BackgroundTaskListOptions options) throws ApiException {
16+
try {
17+
return api.listBackgroundTasks(options.getStatus(), options.getTask(), options.getLimit(),
18+
options.getIterator(),
19+
options.getOrder());
20+
} catch (com.svix.internal.ApiException e) {
21+
throw Utils.wrapInternalApiException(e);
22+
}
23+
}
24+
25+
public BackgroundTaskOut get(final String taskId) throws ApiException {
26+
try {
27+
return api.getBackgroundTask(taskId);
28+
} catch (com.svix.internal.ApiException e) {
29+
throw Utils.wrapInternalApiException(e);
30+
}
31+
}
32+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.svix;
2+
3+
import com.svix.models.BackgroundTaskStatus;
4+
import com.svix.models.BackgroundTaskType;
5+
import com.svix.models.Ordering;
6+
7+
public class BackgroundTaskListOptions extends ListOptions {
8+
private BackgroundTaskStatus status;
9+
private BackgroundTaskType task;
10+
private Ordering order;
11+
12+
public void setOrder(final Ordering order) {
13+
this.order = order;
14+
}
15+
16+
public Ordering getOrder() {
17+
return this.order;
18+
}
19+
20+
public void setStatus(final BackgroundTaskStatus status) {
21+
this.status = status;
22+
}
23+
24+
public BackgroundTaskStatus getStatus() {
25+
return this.status;
26+
}
27+
28+
public void setTask(final BackgroundTaskType task) {
29+
this.task = task;
30+
}
31+
32+
public BackgroundTaskType getTask() {
33+
return this.task;
34+
}
35+
}

javascript/src/index.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ import {
5454
AppPortalAccessOut,
5555
AppPortalAccessIn,
5656
Ordering,
57+
BackgroundTaskStatus,
58+
BackgroundTaskType,
59+
BackgroundTaskOut,
60+
ListResponseBackgroundTaskOut,
61+
BackgroundTasksApi,
5762
} from "./openapi/index";
5863
export * from "./openapi/models/all";
5964
export * from "./openapi/apis/exception";
@@ -95,6 +100,7 @@ export class Svix {
95100
public readonly integration: Integration;
96101
public readonly message: Message;
97102
public readonly messageAttempt: MessageAttempt;
103+
public readonly backgroundTask: BackgroundTask;
98104

99105
public constructor(token: string, options: SvixOptions = {}) {
100106
const regionalUrl = REGIONS.find((x) => x.region === token.split(".")[1])?.url;
@@ -123,6 +129,7 @@ export class Svix {
123129
this.integration = new Integration(config);
124130
this.message = new Message(config);
125131
this.messageAttempt = new MessageAttempt(config);
132+
this.backgroundTask = new BackgroundTask(config);
126133
}
127134
}
128135
export interface PostOptions {
@@ -198,6 +205,11 @@ export interface MessageAttemptListOptions extends ListOptions {
198205
channel?: string;
199206
}
200207

208+
export interface BackgroundTaskListOptions extends ListOptions {
209+
status?: BackgroundTaskStatus;
210+
task?: BackgroundTaskType;
211+
}
212+
201213
class Application {
202214
private readonly api: ApplicationApi;
203215

@@ -659,6 +671,31 @@ class MessageAttempt {
659671
}
660672
}
661673

674+
class BackgroundTask {
675+
private readonly api: BackgroundTasksApi;
676+
677+
public constructor(config: Configuration) {
678+
this.api = new BackgroundTasksApi(config);
679+
}
680+
681+
public listByEndpoint(
682+
options?: BackgroundTaskListOptions
683+
): Promise<ListResponseBackgroundTaskOut> {
684+
return this.api.listBackgroundTasks({
685+
...options,
686+
});
687+
}
688+
689+
public get(
690+
taskId: string,
691+
): Promise<BackgroundTaskOut> {
692+
return this.api.getBackgroundTask({
693+
taskId
694+
});
695+
}
696+
697+
}
698+
662699
class ExtendableError extends Error {
663700
constructor(message: any) {
664701
super(message);
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.svix.kotlin
2+
3+
import com.svix.kotlin.exceptions.ApiException
4+
import com.svix.kotlin.internal.apis.BackgroundTasksApi
5+
import com.svix.kotlin.models.BackgroundTaskOut
6+
import com.svix.kotlin.models.ListResponseBackgroundTaskOut
7+
8+
class BackgroundTask internal constructor(token: String, options: SvixOptions) {
9+
private val api = BackgroundTasksApi(options.serverUrl)
10+
11+
init {
12+
api.accessToken = token
13+
api.userAgent = options.getUA()
14+
options.initialRetryDelayMillis?.let { api.initialRetryDelayMillis = it }
15+
options.numRetries?.let { api.numRetries = it }
16+
}
17+
18+
suspend fun list(options: BackgroundTaskListOptions = BackgroundTaskListOptions()): ListResponseBackgroundTaskOut {
19+
try {
20+
return api.listBackgroundTasks(options.status, options.task, options.limit, options.iterator, options.order)
21+
} catch (e: Exception) {
22+
throw ApiException.wrap(e)
23+
}
24+
}
25+
26+
suspend fun get(taskId: String): BackgroundTaskOut {
27+
try {
28+
return api.getBackgroundTask(taskId)
29+
} catch (e: Exception) {
30+
throw ApiException.wrap(e)
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)