Files
SellyCloudSDK_demo/Example/SellyCloudSDK/Network/AVApiService.m
2026-04-07 18:20:16 +08:00

102 lines
3.9 KiB
Objective-C

//
// AVApiService.m
// SellyCloudSDK_Example
//
#import "AVApiService.h"
#import "AVLiveStreamModel.h"
#import <AFNetworking/AFNetworking.h>
#import <YYModel/YYModel.h>
static NSString * const kBaseURL = @"http://rtmp.sellycloud.io:8089";
@interface AVApiService ()
@property (nonatomic, strong) AFHTTPSessionManager *manager;
@end
@implementation AVApiService
+ (instancetype)shared {
static AVApiService *instance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[AVApiService alloc] init];
});
return instance;
}
- (instancetype)init {
self = [super init];
if (self) {
_manager = [AFHTTPSessionManager manager];
_manager.requestSerializer = [AFJSONRequestSerializer serializer];
_manager.responseSerializer = [AFJSONResponseSerializer serializer];
_manager.requestSerializer.timeoutInterval = 10;
}
return self;
}
- (void)fetchLiveStreams:(void(^)(NSArray<AVLiveStreamModel *> *streams))success
failure:(nullable AVApiFailure)failure {
NSString *url = [NSString stringWithFormat:@"%@/live/sdk/alive-list", kBaseURL];
[self.manager GET:url parameters:nil headers:nil progress:nil
success:^(NSURLSessionDataTask *task, id responseObject) {
NSLog(@"%@",responseObject);
NSArray *streams = [NSArray yy_modelArrayWithClass:AVLiveStreamModel.class json:responseObject[@"list"]];
if (success) success(streams ?: @[]);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(@"[AVApiService] fetchLiveStreams failed: %@", error.localizedDescription);
if (failure) failure(error);
}];
}
- (void)reportXorKeyWithVhost:(NSString *)vhost
app:(NSString *)app
stream:(NSString *)stream
xorKey:(nullable NSString *)xorKey
success:(nullable AVApiSuccess)success
failure:(nullable AVApiFailure)failure {
NSDictionary *params = @{
@"vhost": vhost ?: @"",
@"app": app ?: @"",
@"stream": stream ?: @"",
@"xor_key": xorKey ?: @""
};
NSString *url = [NSString stringWithFormat:@"%@/live/sdk/demo/stream-xor", kBaseURL];
[self.manager POST:url parameters:params headers:nil progress:nil
success:^(NSURLSessionDataTask *task, id responseObject) {
NSLog(@"[AVApiService] reportXorKey success: %@", responseObject);
if (success) success(responseObject);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(@"[AVApiService] reportXorKey failed: %@", error.localizedDescription);
if (failure) failure(error);
}];
}
- (void)loginWithUsername:(NSString *)username
password:(NSString *)password
success:(void(^)(id responseObject))success
failure:(void(^)(NSError *error, NSString * _Nullable serverMessage))failure {
NSDictionary *params = @{
@"username": username ?: @"",
@"password": password ?: @""
};
NSString *url = [NSString stringWithFormat:@"%@/live/sdk/demo/login", kBaseURL];
[self.manager POST:url parameters:params headers:nil progress:nil
success:^(NSURLSessionDataTask *task, id responseObject) {
NSLog(@"[AVApiService] login success");
if (success) success(responseObject);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(@"[AVApiService] login failed: %@", error.localizedDescription);
NSString *serverMessage = nil;
NSData *errorData = error.userInfo[AFNetworkingOperationFailingURLResponseDataErrorKey];
if (errorData) {
NSDictionary *errorDict = [NSJSONSerialization JSONObjectWithData:errorData options:0 error:nil];
serverMessage = errorDict[@"message"] ?: errorDict[@"error"] ?: errorDict[@"msg"];
}
if (failure) failure(error, serverMessage);
}];
}
@end