Giter Club home page Giter Club logo

instagram-nuget's Introduction

Instagram Library

The Instagram library provides a simple and easy-to-use client for interacting with the Instagram API. This library helps you handle common tasks such as obtaining access tokens, exchanging short-lived tokens for long-lived ones, refreshing tokens, and retrieving user profiles and media.

Installation

Install the package via NuGet:

dotnet add package Instagram

Configuration

Add your Instagram settings to the appsettings.json file:

{
  "InstagramSettings": {
    "ClientId": "your-client-id",
    "ClientSecret": "your-client-secret",
    "RedirectUri": "https://localhost:8081/api/instagram/callback"
  }
}

ASP.NET Core Setup

In your Program.cs or Startup.cs file, configure the InstagramClient and its dependencies:

// Instagram Client Configuration
builder.Services.Configure<InstagramSettings>(builder.Configuration.GetSection("InstagramSettings"));
builder.Services.AddHttpClient<IInstagramClient, InstagramClient>();

Usage

You can now inject and use the InstagramClient in your controllers or services. Here’s an example of how to use it in a controller:

public class InstagramController : Controller
{
    private readonly IInstagramClient _instagramClient;

    public InstagramController(IInstagramClient instagramClient)
    {
        _instagramClient = instagramClient;
    }

    public async Task<IActionResult> SignIn()
    {
        var url = _instagramClient.GetAuthorizationUrl();
        return Redirect(url);
    }

    public async Task<IActionResult> Callback(string code)
    {
        var token = await _instagramClient.GetAccessToken(code);
        var user = await _instagramClient.GetUserProfile(token.AccessToken);
        var media = await _instagramClient.GetUserMedia(token.AccessToken, user.Id);
        return Ok(media);
    }
}

Methods

The InstagramClient class provides the following methods:

string GetAuthorizationUrl()
Task<string> GetAccessTokenAsync(string code)
Task<string> ExchangeForLongLivedTokenAsync(string shortLivedToken)
Task<string> RefreshLongLivedTokenAsync(string longLivedToken)
Task<JsonElement> GetUserProfileAsync(string accessToken)
Task<JsonElement> GetUserMediaAsync(string accessToken)
Task<JsonElement> GetMediaAsync(string mediaId, string accessToken)
Task<JsonElement> GetMediaChildrenAsync(string mediaId, string accessToken)
Task<List<JsonElement>> GetAllUserMediaAsync(string accessToken)

License

See the LICENSE file for more details.

Contributing

Contributions are welcome! Please open an issue or submit a pull request.

instagram-nuget's People

Contributors

kiliczsh avatar

Stargazers

Mohammd Mansoori avatar

Watchers

 avatar

instagram-nuget's Issues

Setup CI/CD Pipeline with Checks and Automatic NuGet Publishing

We need to set up a CI/CD pipeline to automate the building, testing, and publishing of the InstagramClient package. This pipeline should include checks to ensure code quality and successful builds before publishing the package to NuGet.

  1. Create a GitHub Actions workflow file to automate the build and test process.
  2. Add steps to the workflow to build the project and run unit tests.
  3. Add a step to publish the package to NuGet if all checks pass.
  4. Ensure the workflow includes triggers for pull requests and pushes to the main branch.
  5. Securely store the NuGet API key in GitHub Secrets.


• A GitHub Actions workflow is created and placed in the .github/workflows directory.
• The workflow builds the project and runs unit tests.
• The workflow publishes the package to NuGet upon successful checks.
• The workflow is triggered on pull requests and pushes to the main branch.
• The NuGet API key is securely stored and used in the workflow.


Workflow File (Not Final):

name: CI/CD Pipeline

on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Setup .NET
        uses: actions/setup-dotnet@v2
        with:
          dotnet-version: '6.0.x'

      - name: Install dependencies
        run: dotnet restore

      - name: Build
        run: dotnet build --configuration Release --no-restore

      - name: Run tests
        run: dotnet test --no-restore --verbosity normal

  publish:
    needs: build
    runs-on: ubuntu-latest
    if: github.ref == 'refs/heads/main'

    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Setup .NET
        uses: actions/setup-dotnet@v2
        with:
          dotnet-version: '6.0.x'

      - name: Install dependencies
        run: dotnet restore

      - name: Build
        run: dotnet build --configuration Release --no-restore

      - name: Pack
        run: dotnet pack --configuration Release --no-restore

      - name: Publish to NuGet
        env:
          NUGET_API_KEY: ${{ secrets.NUGET_API_KEY }}
        run: dotnet nuget push bin/Release/*.nupkg --source https://api.nuget.org/v3/index.json --api-key $NUGET_API_KEY

Create Models for API Responses

To improve code readability and maintainability, we need to replace the use of JsonElement with strongly-typed models for the API responses. This will involve creating new model classes that represent the data structures returned by the Instagram API and updating the methods in Instagram to use these models.

  1. Identify API Response Structures:
    • Review the Instagram API documentation to identify the structure of the responses for the endpoints used in InstagramClient.
  2. Create Model Classes:
    • Create model classes for the following API responses:
    • Access Token Response
    • User Profile
    • User Media
    • Media
    • Media Children
  3. Update Methods in Instagram:
    • Update the methods in Instagram to deserialize responses into the new model classes instead of using JsonElement.


• Model classes are created for all relevant API responses.
• Methods in InstagramClient are updated to use the new model classes.
• Unit tests are updated or created to verify correct deserialization of API responses.
• The codebase no longer uses JsonElement for API responses.

Publish Instagram Package on NuGet

We need to publish the Instagram package to NuGet to make it available for public use. This involves several steps, including ensuring all necessary files are in place and configuring the package metadata.

  1. Ensure the README.md and LICENSE files are in the root directory of the project.
  2. Update the .nuspec file to include the readme and license references.
  3. Build the project in Release mode to generate the DLL.
  4. Create the NuGet package using nuget pack.
  5. Publish the package to NuGet using nuget push.

✅ The package is successfully built and published to NuGet.

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.