microsoft / microsoft/teams.net

Will all HTTP methods be added?

Open
#200 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
46
Forks
21
Avg merge
22h 25m
Merged PRs (30d)
16

Description

Hi! I just wanted to understand whether other Http methods with authentication, context transfer, and so on will be added, or is this library limited to Post requests due to the name "Function"?

Could you please tell me why it is limited to Post requests only?

I made a crutch what it might look like
```
public interface IEndpoint
{
void MapEndpoint(IEndpointRouteBuilder app);
}

public interface ITeamsFunction : IEndpoint
{
string FunctionName { get; }
}

public abstract class TeamsFunctionBase : ITeamsFunction
{
public abstract string FunctionName { get; }
protected virtual HttpMethod HTTPMethod => HttpMethod.Post;

public void MapEndpoint(IEndpointRouteBuilder app)
{
var handler = async (HttpContext httpContext) =>
{
httpContext.Request.EnableBuffering();
var teamsApp = httpContext.RequestServices.GetRequiredService();
var log = teamsApp.Logger.Child("functions").Child(FunctionName);

if (httpContext.Request.Headers.Authorization.FirstOrDefault() is null)
{
return Results.Unauthorized();
}

if (!httpContext.Request.Headers.TryGetValue("X-Teams-App-Session-Id", out var appSessionId))
{
return Results.Unauthorized();
}

if (!httpContext.Request.Headers.TryGetValue("X-Teams-Page-Id", out var pageId))
{
return Results.Unauthorized();
}

var authToken = httpContext.Request.Headers.Authorization
.FirstOrDefault()?
.Replace("bearer ", string.Empty)
.Replace("Bearer ", string.Empty);

var token = new JwtSecurityTokenHandler().ReadJwtToken(authToken);

var functionContext = new FunctionContext(teamsApp)
{
Api = new(teamsApp.Api),
Log = log,
AppSessionId = appSessionId,
TenantId = token.Claims.First(c => c.Type == "tid").Value,
UserId = token.Claims.First(c => c.Type == "oid").Value,
UserName = token.Claims.First(c => c.Type == "name").Value,
PageId = pageId,
AuthToken = authToken,
Data = default
};
if (HTTPMethod != HttpMethod.Get)
{
functionContext.Data = await httpContext.Request.ReadFromJsonAsync();
}

if (httpContext.Request.Headers.TryGetValue("X-Teams-Channel-Id", out var channelId))
{
functionContext.ChannelId = channelId;
}

if (httpContext.Request.Headers.TryGetValue("X-Teams-Chat-Id", out var chatId))
{
functionContext.ChatId = chatId;
}

if (httpContext.Request.Headers.TryGetValue("X-Teams-Meeting-Id", out var meetingId))
{
functionContext.MeetingId = meetingId;
}

if (httpContext.Request.Headers.TryGetValue("X-Teams-Message-Id", out var messageId))
{
functionContext.MessageId = messageId;
}

if (httpContext.Request.Headers.TryGetValue("X-Teams-Sub-Page-Id", out var subPageId))
{
functionContext.SubPageId = subPageId;
}

if (httpContext.Request.Headers.TryGetValue("X-Teams-Team-Id", out var teamId))
{
functionContext.TeamId = teamId;
}

log.Debug(functionContext.Data?.ToString());
var result = await HandleAsync(functionContext, httpContext.RequestServices);

return result;
};

var endpoint = $"{ApiEndpoints.FunctionsPath}/{FunctionName}";
var routeBuilder = HTTPMethod.Method switch
{
"GET" => app.MapGet(endpoint, handler),
"POST" => app.MapPost(endpoint, handler),
"PUT" => app.MapPut(endpoint, handler),
"DELETE" => app.MapDelete(endpoint, handler),
"PATCH" => app.MapPatch(endpoint, handler),
_ => throw new NotSupportedException($"HTTP method {HTTPMethod.Method} is not supported")
};

routeBuilder.RequireAuthorization(HostApplicationBuilderExtensions.EntraTokenAuthConstants.AuthorizationPolicy);
}

protected abstract Task HandleAsync(IFunctionContext context, IServiceProvider services);
}

public static class EndpointExtensions
{
public static IServiceCollection AddEndpoints(this IServiceCollection services, Assembly assembly)
{
var serviceDescriptors = assembly
.DefinedTypes
.Where(type => type is { IsAbstract: false, IsInterface: false } &&
type.IsAssignableTo(typeof(IEndpoint)))
.Select(type => ServiceDescriptor.Transient(typeof(IEndpoint), type))
.ToArray();

services.TryAddEnumerable(serviceDescriptors);

return services;
}

public static IApplicationBuilder MapEndpoints(
this WebApplication app,
RouteGroupBuilder? routeGroupBuilder = null)
{
var endpoints = app.Services.GetRequiredService>();

IEndpointRouteBuilder builder = routeGroupBuilder is null ? app : routeGroupBuilder;

foreach (var endpoint in endpoints)
{
endpoint.MapEndpoint(builder);
}

return app;
}
}
```

Contributor guide

Open the contributing guide

Research direction

Start by reading the proposed TeamsFunctionBase and EndpointExtensions code in the issue, then compare it with the library's existing endpoint registration and request handling. Clarify which HTTP methods, authentication behavior, and request-body semantics are intended; done requires an agreed scope before implementation can be evaluated.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
api, backend
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.