Code Monkey home page Code Monkey logo

twitcastingapi's Introduction

TwitCasting API v2 Client in Swift

Swift client library for TwitCasting API v2

offical document => https://apiv2-doc.twitcasting.tv/

Requirement

  • iOS 15.0+
  • macOS 12.0+

Installation

Swift Package Manager

  1. In Xcode, select File > Add Packages....
  2. Spacify the repository https://github.com/tosakakun/TwitCastingAPI.git
  3. Spacify options and then click Add Package.

Usage

You have to generate your client id and set Callback URL via the Developer page. Make sure to set the appropriate scope.

Authentication

Create an instance of TwitCastingAuth with your client ID and callback URL scheme.

import SwiftUI
import TwitCastingAPI

@main
struct TwitCastingAPIDevApp: App {
    
    @StateObject var auth = TwitCastingAuth(clientId: "YOUR_CLIENT_ID", callbackURLScheme: "YOUR_CALLBACK_URL_SCHEME")
    
    var body: some Scene {
        WindowGroup {
            
            if auth.token.isEmpty {
                LoginView(auth: auth)
            } else {
                APITestView()
                    .environmentObject(auth)
            }
            
        }
    }
}

Call the login method on the login screen.

import SwiftUI
import TwitCastingAPI

struct LoginView: View {
    
    @ObservedObject var auth: TwitCastingAuth

    var body: some View {
        
        Button {
            auth.login()
        } label: {
            Text("ツイキャスでログイン")
        }
        
    }
}

Getting Information

Get User Info

import Foundation
import TwitCastingAPI

class GetUserInfoViewModel: ObservableObject {
    
    @Published var userInfo: TCUserInfoResponse?
    
    private let api = TwitCastingAPI()
    
    func getUserInfo(token: String, id: String) async {
        
        do {
            
            let userInfo = try await api.getUserInfo(token: token, userId: id)
            
            DispatchQueue.main.async {
                self.userInfo = userInfo
            }
            
        } catch let error as TCError {
            print(error.localizedDescription)
        } catch {
            print(error.localizedDescription)
        }
        
    }

}
import SwiftUI
import TwitCastingAPI

struct GetUserInfoView: View {
    
    @EnvironmentObject var auth: TwitCastingAuth
    
    @StateObject var viewModel = GetUserInfoViewModel()
    
    @State private var id = ""

    var body: some View {
        
        VStack {
            TextField("id or screen_id を入力", text: $id)
                .textFieldStyle(.roundedBorder)
                .padding()
            Button {
                Task {
                    await viewModel.getUserInfo(token: auth.token, id: id)
                }
            } label: {
                Text("検索")
            }
            
            if let userInfo = viewModel.userInfo {
                
                AsyncImage(url: URL(string: userInfo.user.image))
                Text(userInfo.user.id)
                Text(userInfo.user.screenId)
                Text(userInfo.user.name)
                Text("\(userInfo.user.level)")
                Text(userInfo.user.profile)

            }

            Spacer()
            
        }
        .navigationTitle("Get User Info テスト")

    }
    
}

All TwitCasting API v2 can be used in the same way, except WebHook and Realtime API.

License

This library is released under the MIT License.

twitcastingapi's People

Contributors

tosakakun avatar

Stargazers

 avatar

Watchers

 avatar

twitcastingapi's Issues

TwitCastingAuth エラー処理の修正

  • エラー発生時に Published アトリビュートのついたプロパティへメッセージがバックグラウンドスレッドから代入されているので、メインスレッドから代入するように修正が必要。
  • エラー処理時にlogout() メソッドを呼んでいるため、同様にメインスレッドから代入が行われるように修正が必要。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.